UNIT-I
JAVA PROGRAMMING
Dr. Himani Maheshwari
CLASS, OBJECTS AND
CONSTRUCTORS
DR. HIMANI MAHESHWARI
ASSISTANT PROFESSOR
SOC
Object in Java
An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen, table,
car, etc. It can be physical or logical (tangible and intangible). The example of an intangible
object is the banking system.
An object has three characteristics:
•State: represents the data (value) of an object.
•Behavior: represents the behavior (functionality) of an object such as deposit, withdraw, etc.
•Identity: An object identity is typically implemented via a unique ID. The value of the ID is not
visible to the external user. However, it is used internally by the JVM to identify each object
uniquely.
For Example, Pen is an object. Its name is Reynolds; color is white, known as its state. It is used
to write, so writing is its behavior.
An object is an instance of a class. A class is a template or blueprint from which objects
are created. So, an object is the instance(result) of a class.
Object in Java
Class in Java
A class is a group of objects which have common properties. It is a template or
blueprint from which objects are created. It is a logical entity. It can't be physical.
A class in Java can contain:
•Fields
•Methods
•Constructors
•Blocks
•Nested class and interface
A class is declared by use of the class keyword.
Class in Java
• The data, or variables,
defined within a class are
called instance variables.
• The code is contained
within methods.
• Collectively, the methods
and variables defined
within a class are called
members of the class.
Class in Java
class Student{
int id;//instance variable
String name;
public static void main(String args[]){
Student s1=new Student();//creating an object of Student
System.out.println(s1.id);//accessing member through reference variable
System.out.println(s1.name);
}
}
OUTPUT:
0
null
Creating class and objects in Java
class Student{
int id=101;//instance variable
String name="AMAN VERMA";
public static void main(String args[]){
Student s1=new Student();//creating an object of Student
System.out.println(s1.id);//accessing member through reference variable
System.out.println(s1.name);
}
}
OUTPUT:
101
AMAN VERMA
Creating multiple objects
System.out.println(s1.id);//accessing
class Student{ member through reference variable
int id;//instance variable System.out.println(s1.name);
String name; System.out.println("Values of second
object:");
public static void main(String args[]){
System.out.println(s2.id);//accessing
Student s1=new Student();//creating first
member through reference variable
object of Student
System.out.println(s2.name);
Student s2=new Student();//creating
second object of Student } }
s1.id=101; s2.id=102; OUTPUT:
s1.name="Aman"; s2.name="Naman"; 101
AMAN
System.out.println("Values of first object:");
102
Naman
Using Multiple Classes
Save As: Student.java
class Test {
Compile by: javac Student.java
int x = 5;
Run by: java Student
}
OUTPUT:
class Student {
5
public static void main(String[] args) {
Test myObj = new Test();
System.out.println(myObj.x);
}
}
Object and Class Example:
Initialization through s1.insertRecord(111,"Karan");
method
class Student{ s2.insertRecord(222,"Aryan");
int rollno; s1.displayInformation();
String name; s2.displayInformation();
void insertRecord(int rno, String nam){ }
rollno=rno; }
name=nam;
} Save As: Test.java
void displayInformation() Compile by: javac Test.java
{ Run by: java Test
System.out.println(rollno+" "+name);
OUTPUT:
}
} 111 Karan
222 Aryan
class Test{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
Object and Class Example:
Initialization through method
As you can see in
the above figure,
object gets the
memory in heap
memory area. The
reference variable
refers to the object
allocated in the
heap memory area.
Here, s1 and s2
both are reference
variables that refer
to the objects
allocated in
memory.
Anonymous object
Anonymous simply means nameless. An
object which has no reference is known as an System.out.println("factorial is "+fact);
anonymous object. It can be used at the time }
of object creation only. public static void main(String args[]){
If you have to use an object only once, an new Calculation().fact(5);//calling method
anonymous object is a good approach. For with anonymous object
example: }
class Calculation{ }
void fact(int n){
int fact=1; Save As: Calculation.java
for(int i=1;i<=n;i++){ Compile by: javac Calculation.java
fact=fact*i; Run by: java Calculation
} OUTPUT:
Factorial is 120
Creating multiple objects by one type only
We can create multiple objects by one type only as class Test{
we do in case of primitives. Initialization of public static void main(String args[]){
primitive variables: int a=10, b=20; Student s1=new Student(), Student s2=new
Initialization of reference variables: Student(); //creating two objects
class Student{ s1.insertRecord(111,"Karan");
int rollno; s2.insertRecord(222,"Aryan");
String name; s1.displayInformation();
void insertRecord(int rno, String nam){ s2.displayInformation();
rollno=rno; }
name=nam; }
} Save As: Test.java
void displayInformation() Compile by: javac Test.java
{ Run by: java Test
OUTPUT:
System.out.println(rollno+" "+name);
111 Karan
} 222 Aryan
}
Constructor
• A constructor is a block of codes similar to the method. It is called when an instance of the class is
created. At the time of calling constructor, memory for the object is allocated in the memory.
• It is a special type of method which is used to initialize the object.
• Every time an object is created using the new() keyword, at least one
constructor is called.
• It calls a default constructor if there is no constructor available in the class. In
such case, Java compiler provides a default constructor by default.
• Note: It is called constructor because it constructs the values at the time of
object creation. It is not necessary to write a constructor for a class. It is
because java compiler creates a default constructor if your class
doesn't have any.
• Constructor name must be the same as its class name
• A Constructor must have no explicit return type
• A Java constructor cannot be abstract, static, final, and synchronized
Constructor vs Method
Types of Java constructors
There are two types of constructors in Java:
1. Default constructor (no-arg constructor)
2. Parameterized constructor
Default Constructor
A constructor is called "Default Constructor" when it doesn't have any parameter.
Syntax of default constructor: <class_name>(){}
Example:
class Test{
Test(){
System.out.println(" Test is created");
}
public static void main(String args[]){
Test t1=new Test();
}
}
OUTPUT:
Test is created
Purpose of a Default Constructor
The default constructor is used to provide the default //displaying values of the object
values to the object like 0, null, etc., depending on s1.display();
the type.
}
class Student3{
}
int id;
OUTPUT:
String name;
0 null
//method to display the value of id and name
In the above class, you are not creating any
void display(){System.out.println(id+" "+name);} constructor so compiler provides you a default
public static void main(String args[]){ constructor. Here, 0 and null values are provided
by default constructor.
//creating objects
Student3 s1=new Student3();
Parameterized Constructor
A constructor which has a specific number of void display()
parameters is called a parameterized constructor. {System.out.println(id+" "+name);}
The parameterized constructor is used to provide public static void main(String args[]){
different values to distinct objects. However, you can
provide the same values also. Student s1 = new Student(101,“Aman");
Example: Student s2 = new Student(102,“Karan");
class Student{ s1.display();
int id; s2.display();
String name; }
Student(int i, String n){ }
id = i; OUTPUT:
name = n; 101 Aman
} 102 Karan
Constructor Overloading
• In Java, a constructor is just like a method but without return type. It can also be
overloaded like Java methods.
• Constructor overloading in Java is a technique of having more than one constructor
with different parameter lists.
• They are arranged in a way that each constructor performs a different task. They are
differentiated by the compiler by the number of parameters in the list and their
types.
Constructor Overloading
class Student{ }
int rollno; void display(){System.out.println(rollno+"
"+name+" "+age);}
String name;
public static void main(String args[]){
int age;
Student s1 = new Student(101,"Aman");
Student(int rno, String nam){
Student s2 = new Student(202,"Atul",21);
rollno = rno;
s1.display();
name = nam;
s2.display();
}
}
Student(int rno,String nam,int a){
}
rollno = rno;
OUTPUT:
name = nam;
101 Aman 0
age=a;
202 Atul 21