What is object instantiation?
Means object creation.
What is object Initialization?
Process of assigning data to the fields of an object, is called as object initialization.
What is the use of constructor?
Constructors are used to initialize object during object creation.
What is a constructor?
It is a special method, which has same name as class name and it doesn’t contain return type.
Types of constructors
There are 2 types of constructors
1. Parameterized constructors
2. Non-parameterized constructors
Example on parameterized constructor
class Emp {
int eno;
String ename;
float sal;
Emp(int no, String name, float sa) {
// no=2, name="Harshan",sa=300000.00f
eno = no;
ename = name;
sal = sa;
}
// i have written display method to display object state...
void display() // e2=2002
{
System.out.println("Object state");
System.out.println("Eno:\t" + eno);
System.out.println("Ename:\t" + ename);
System.out.println("Sal:\t" + sal);
}
}
public class ClassObject1 {
public static void main(String[] args) {
Emp e1 = new Emp(1, "Vishwa", 200000.00f);
Emp e2 = new Emp(2, "Harshan", 300000.00f);
e1.display();// method calling statement
e2.display();
}
}
Output:
Object state
Eno: 1
Ename: Vishwa
Sal: 200000.0
Object state
Eno: 2
Ename: Harshan
Sal: 300000.0
Who creates a default constructor for a class?
Java compiler creates a default constructor for a class during compilation time. If that class doesn’t
contain any constructors.
This keyword
1. this is a keyword which represents preset object
2. We can use this keyword to call same class constructor in another constructor.
Note: we have to use this keyword to call same class constructor in another constructor except at the
time of object creation.
Ex:
One()
{
}
One(int a,int b){
this(); //calling non parameter constructo..
}
At the time of object creation we have to call constructor by using it’s name
Ex: new One();
Note: constructor calling statement must be the first statement in another constructor.
First example on this keyword usage
class One {
int a, b;
One(int a, int b) {
// a=100
// b=200
this.a = a;
this.b = b;
}
void display() {
System.out.println("Object state...");
System.out.println("a:\t" + a);
System.out.println("b:\t" + b);
}
}
public class DefaultConstructor {
public static void main(String[] args) {
One o1 = new One(100, 200);
One o2 = new One(1000, 2000);
o1.display();
o2.display();
}
}
Calling constructor by using this keyword
class One {
int a, b;
One() {
System.out.println("Non parameterized....");
}
One(int a, int b) {
this();
System.out.println("Parameterized....");
this.a = a;
this.b = b;
}
void display() {
System.out.println("Object state...");
System.out.println("a:\t" + a);
System.out.println("b:\t" + b);
}
}
public class DefaultConstructor {
public static void main(String[] args) {
One o1 = new One(100, 200);
One o2 = new One(1000, 2000);
o1.display();
o2.display();
}
}
Output:
Non parameterized....
Parameterized....
Non parameterized....
Parameterized....
Object state...
a: 100
b: 200
Object state...
a: 1000
b: 2000
Encapsulation
Wrapping up of data (fields) and methods into a single unit is called as encapsulation.
Data Abstraction
Process of hiding un-necessary details and providing necessary details to the end-user is called as data
abstraction.
Example on Data Abstraction
class Student {
int m1, m2, m3;
Student(int m1, int m2, int m3) {
this.m1 = m1;
this.m2 = m2;
this.m3 = m3;
}
private int getTotal() {
return m1 + m2 + m3;
}
private int getAvg() {
return getTotal() / 3;
}
public boolean isPass() {
if (m1 >= 40 && m2 >= 40 && m3 >= 40)
return true;
else
return false;
public String getGrade() {
if (isPass()) {
if (getAvg() >= 90) {
return "A+";
}
if (getAvg() >= 80) {
return "A";
} else {
return "B";
}
} else {
return "Fail: No-Grade";
}
}
}
public class DataAbstraction {
public static void main(String[] args) {
Student s1 = new Student(99, 98, 100);
System.out.println("Is Pass:\t" + s1.isPass());
System.out.println("Grade:\t" + s1.getGrade());
}
}
Polymorphism
Method overloading
We can write more than one method with same name with different signature in a class. it is called as
method overloading.
Method signature
Method name along with parameter count, type and order is called as method signature
What is constructor overloading?
If we write more than one constructor in a class with different signature it is called as constructor
overloading.