Classes and Objects
Java is an object-oriented language.
Objects
In Java, everything is an object. It is the root of Java class hierarchy.
For example, look at the below image, it is hierarchy of classes to deal with input, output stream, it has the root of Object
class.
Object has some characteristics:
- an
Object
reference can reference an instance of any class - every class has characteristics of
Object
Object[] stuff = new Object[3];
stuff[0] = new Student(123);
stuff[1] = new MathEquation();
stuff[2] = "a string";
Object o = "string";
o = new House(1);
void doWork(Object o) { }
Memory
When create object, Java allocates it in heap memory. But not only that, Java returns a reference to the object (object's address). And usually, we assign that to a variable.
So unlike Primitives (contain a value), Object variables contain a reference to the object.
We never interact with the object directly but through reference.
Object Class Methods
Method | Description |
---|---|
clone | Create a new object instance that duplicates the current instance |
hashCode | Get a hash code for current instance |
getClass | Return type information for the current instance |
finialize | Handle special resource cleanup scenarios |
toString | Return a string value representing the current instance |
equals | Compare another object to the current instance for equality |
One application of this is to build equals
method:
public class Person {
private int age;
public boolean equals(Object o) {
Person person = (Person) o;
return age == person.age;
}
}
But if we use equals
with classes that aren't Person
, it can crash the program. To prevent that, use instanceof
:
public class Person {
private int age;
public boolean equals(Object o) {
if (!(o instanceof Person))
return false;
Person person = (Person) o;
return age == person.age;
}
}
Classes
Class is a template for creating objects. While object is an instance of a class.
Classes are reference types, means class variable simply hold a reference.
Student tu = new Student(); // age: 0
Student huy = new Student(); // age: 0
tu.age += 10;
// this is reference type (because both tu and huy are not primitive types)
// both variables can reference to the same instance
huy = tu;
huy.age // 10
huy.age += 2
tu.age // 12
Declare
Class has fields & methods.
// Name.java // same name of class
class Name {
int attribute;
Name() { // constructor
atrribute = 0;
}
void Method() {}
}
Final fields need to be initialized when create objects.
// this will get error from compiler
class Person {
final int age;
Person() { } // to fix, we need to init it in constructor
}
Usage
Declare a variable of type of the class doesn't create the object, it just holds the reference.
To create object, use new
keyword.
Student huytu = new Student();
// it's actually doing 2 step
Student huytu; // create variable hold the reference
huytu = new Student(); // create new object instance & reference
Access Modifier
Access modifier is used to achieve encapsulation. It controls class/member visibility.
Modifier | Visibility | Usable on Classes | Usable on Members |
---|---|---|---|
No access modifier | Only within its own package (aslo called package private) | ✅ | ✅ |
public | Everywhere | ✅ | ✅ |
private | Only within the declaring class | ❌* | ❌ |
protected | Only within its package and all subclasses |
**In general, classes cannot be private
. There is an exception, that is nested classes*.
Non-access Modifier
static
final
abstract
strictfp
synchronized