[1] Intro to Object-oriented programming - classes, objects, constructors, keywords
🥍

[1] Intro to Object-oriented programming - classes, objects, constructors, keywords

Date
Nov 30, 2022
Tags
OOP
programming
JAVA

NOTES

Following are my notes with my own examples from the video lecture below:
Although the instructor explicitly mentions that you don’t need to take notes and he provides it for you, I want to document the important points for myself below.

Classes and objects

A class creates a new data type that can be used to create objects.
An object is an instance of a class.
A class is a logical construct. An object has physical reality. (which occupies space in memory.)
Objects are characterized by three essential properties:
  1. state: value from its data type,
  1. identity: identity of an object distinguishes one object from another,
  1. behavior: behavior of an object is the effect of data-type operations
 
Example:
Let’s say we create a class called “IdolGroup”
Instances of the class “IdolGroup” would be different idol groups such as bts, blackpink, aespa, enhypen, etc
 
public class IdolGroup { String groupName; int numOfMembers; }
public static void main(String[] args) { // Initializing the IdolGroup objects IdolGroup bts = new IdolGroup(); // IdolGroup 1 IdolGroup enhypen = new IdolGroup(); // IdolGroup 2 }
The “new” keyword dynamically allocates (allocates memory at run time) memory for an object & returns a reference to it. In the above example, the reference is “bts” or “enhypen”
In Java, all class objects must be dynamically allocated to the heap memory.
But Java’s primitive types are not implemented as objects
 
JAVA’s primitive types:
Data Type
Size
Description
byte
1 byte
Stores whole numbers from -128 to 127
short
2 bytes
Stores whole numbers from -32,768 to 32,767
int
4 bytes
Stores whole numbers from -2,147,483,648 to 2,147,483,647
long
8 bytes
Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float
4 bytes
Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double
8 bytes
Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean
1 bit
Stores true or false values
char
2 bytes
Stores a single character/letter or ASCII values
Non-primitive types are created by the programmer and is not defined by Java (except for String).
Examples of non-primitive types are StringsArraysClasses, Interface.
In Java, you cannot manipulate references as you can actual pointers in C/C++.
(Personal note: The first time I ever studied OOP was using JAVA in high school, the second time was in my first year of uni using C++)
So I want to draw a parallel between C/C++ pointers vs C/C++ reference vs Java reference.

C/C++ pointers vs Java reference

Reference: A reference is a variable that refers to something else and can be used as an alias for that something else.
Pointer: A pointer is a variable that stores a memory address, for the purpose of acting as an alias to what is stored at that address.
Java doesn’t support pointer explicitly,  But java uses pointer implicitly
Pointers can do arithmetic, References can’t.
// A simple C++ program to illustrate // the concept of pointers and // their manipulations in C/C++ #include <iostream> using namespace std; int main() { int number = 111; int * pointerNumber; // assign the address of the variable number // to pointer pointerNumber pointerNumber = &number; // Print content of pNumber cout << "1." << pointerNumber << endl; // Print address of number cout << "2." << &number << endl; // Print value pointed to by pNumber cout << "3." << *pointerNumber << endl; // Print value of number cout << "4." << number << endl; // Re-assign value pointed to by pNumber *pointerNumber = 99; cout << "5." << pointerNumber << endl; cout << "6." << &number << endl; cout << "7." << *pointerNumber << endl; cout << "8." << number << endl; }
 
OUTPUT:
1. 0x7fff1ae7ca94 2. 0x7fff1ae7ca94 3. 111 4. 111 5. 0x7fff1ae7ca94 // same memory address but value changed 6. 0x7fff1ae7ca94 7. 0x7fff1ae7ca94 8. 88 9. 88
 
// A simple Java program // to illustrate the concept of // references class Enhypen { double height; } class EnhypenDemo { public static void main(String args[]) { // jungwon is reference variable which contain // the address of Actual Enhypen Object. Enhypen jungwon = new Enhypen(); // heeseung is another reference variable // heeseung is initialized with jungwon means: // heeseung and jungwon both are referring same object // thus it does not create duplicate object // nor does it allocate extra memory. Enhypen heeseung = jungwon; jungwon.height = 170; heeseung.length = 180; System.out.println("Value of Jungwon's height : " + jungwon.height); System.out.println("Value of Heeseung's height : " + heeseung.height); } }
OUTPUT:
Value of Jungwon's height : 180.0 Value of Heeseung's height : 180.0
Because they were pointing to the same object!
 

Constructors and keywords

The constructor is automatically called when the object is created.
Implicit return type of a class’ constructor is the class type itself.
public class VintageCar { int modelYear; String modelName; // constructor public VintageCar(int modelYear, String modelName) { // "this" keyword is always a reference to the object on // which the method was invoked. this.modelYear = modelYear; this.modelName = modelName; } public static void main(String[] args) { VintageCar myCar = new VintageCar(1969, "Mustang"); System.out.println(myCar.modelYear + " " + myCar.modelName); // This will call default constructor // Any class will have a default constructor, // does not matter if we declare it in the class or not VintageCar randomCar = new VintageCar(); System.out.println(randomCar.modelYear + " " + randomCar.modelName); } }
OUTPUT:
1969 Mustang 0 null
 

final keyword:

when a variable is declared as final, java prevents its contents from being modified, making it a constant.
This means that you must initialize a final field when it is declared.
“final” keyword guarantees immutability only when instance variables are primitive types, not reference types.
It is a common coding convention to choose all uppercase identifiers for final fields:
final int CROSSFACTOR = 2;
 

the finalize() method

It is the method which is called when an object is destroyed.
You can define specific actions that will occur when an object is just about to be reclaimed by the garbage collector by using finalize() method!
protected void finalize( ) { // finalization code here }
 

References: