Object and Classes
Object and Classes
Object means a real-world entity such as a pen, chair, table, computer, watch, etc. Object-
Oriented Programming is a methodology or paradigm to design a program using classes and
objects. It simplifies software development and maintenance by providing some concepts:
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
Object
Class
Collection of objects is called class. It is a logical entity.A class can also be defined as a blueprint
from which you can create an individual object. Class doesn't consume any space.
Inheritance
When one object acquires all the properties and behaviors of a parent object, it is known as
inheritance. It provides code reusability. It is used to achieve runtime polymorphism.
Polymorphism
Hiding internal details and showing functionality is known as abstraction. For example phone
call, we don't know the internal processing. In Java, we use abstract class and interface to
achieve abstraction.
Encapsulation
Binding (or wrapping) code and data together into a single unit are known as encapsulation. For
example, a capsule, it is wrapped with different medicines. A java class is the example of
encapsulation.
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 Definitions:
Fields
Methods
Constructors
Blocks
Nested class and interface
Example 1
class Student
{
int id=100;//field or data member or instance variable
String name="ravi";
Example 2
class Student
{
int id=101;
String name="ravi";
}
class TestStudent1
{
public static void main(String args[])
{
Student s1=new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}
}
Example 3
class Student
{
introllno;
String name;
voidinsertRecord(int r, String n)
{
rollno=r;
name=n;
}
voiddisplayInformation()
{
System.out.println(rollno+" "+name);
}
}
class TestStudent4
{
public static void main(String args[])
{
Student s1=new Student();
Student s2=new Student();
s1.insertRecord(111,"Karan");
s2.insertRecord(222,"Aryan");
s1.displayInformation();
s2.displayInformation();
}
}
Constructors
In Java, 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.
Default Constructor
A constructor is called "Default Constructor" when it doesn't have any parameter.
Example 4
class Bike1
{
//creating a default constructor
Bike1()
{
System.out.println("Class Bike is created");
}
public static void main(String args[])
{
//calling a default constructor
Bike1 b=new Bike1();
}
}
Parameterized Constructor
Example 5
class Student
{
int id;
String name;
//creating a parameterized constructor
Student(inti,String n)
{
id = i;
name = n;
}
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
s1.display();
s2.display();
}
}
Example 6
class Student
{
int id;
String name;
//constructor to initialize integer and string
Student(inti,String n)
{
id = i;
name = n;
}
//constructor to initialize another object
Student(Student s)
{
id = s.id;
name =s.name;
}
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
Student s1 = new Student(111,"Karan");
Student s2 = new Student(s1);
s1.display();
s2.display();
}
}
In Java, a constructor is just like a method but without return type. It can also be overloaded like
Java methods.
Example 7
class Student
{
int id;
String name;
int age;
//creating two arg constructor
Student(inti,String n)
{
id = i;
name = n;
}
//creating three arg constructor
Student(inti,Stringn,int a)
{
id = i;
name = n;
age=a;
}
void display()
{
System.out.println(id+" "+name+" "+age);
}
public static void main(String args[])
{
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan",25);
s1.display();
s2.display();
}
}
The static keyword in Java is used for memory management mainly. We can apply java static
keyword with variables, methods, blocks and nested class. The static keyword belongs to the
class than an instance of the class.
The static variable can be used to refer to the common property of all objects (which is
not unique for each object), for example, the company name of employees, college name
of students, etc.
The static variable gets memory only once in the class area at the time of class loading.
Example 8
class Student
{
introllno;//instance variable
String name;
static String college ="CCSIT";//static variable
Student(int r, String n)
{
rollno = r;
name = n;
}
void display()
{
System.out.println(rollno+" "+name+" "+college);
}
}
public class TestStaticVariable
{
public static void main(String args[])
{
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
s1.display();
s2.display();
}
}
Example 9
class Counter
{
staticint count=0;
Counter()
{
count++;
System.out.println(count);
}
public static void main(String args[])
{
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
}
}
A static method belongs to the class rather than the object of a class.
A static method can be invoked without the need for creating an instance of a class.
A static method can access static data member and can change the value of it.
Example 10
class Student
{
introllno;
String name;
static String college = "CCSIT";
static void change()
{
college = "COE";
}
Student(int r, String n)
{
rollno = r;
name = n;
}
void display()
{
System.out.println(rollno+" "+name+" "+college);
}
}
public class TestStaticMethod
{
public static void main(String args[])
{
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
Student.change();
Student s3 = new Student(333,"Sonu");
s1.display();
s2.display();
s3.display();
}
}
In this example, we have created two methods, first add() method performs addition of two
numbers and second add method performs addition of three numbers.
Example 11
class Adder
{
staticint add(inta,int b)
{
returna+b;
}
staticint add(inta,intb,int c)
{
returna+b+c;
}
}
class TestOverloading1
{
public static void main(String[] args)
{
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}
}
Example 12
class Adder
{
staticint add(int a, int b)
{
returna+b;
}
static double add(double a, double b)
{
returna+b;
}
}
class TestOverloading2
{
public static void main(String[] args)
{
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.3,12.6));
}
}
The this keyword can be used to refer current class instance variable. If there is ambiguity
between the instance variables and parameters, this keyword resolves the problem of ambiguity.
class Student
{
introllno;
String name;
float fee;
Student(introllno,String name,float fee)
{
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display()
{
System.out.println(rollno+" "+name+" "+fee);
}
}
classTestThis
{
public static void main(String args[])
{
Student s1=new Student(111,"ankit",5000);
Student s2=new Student(112,"sumit",6000);
s1.display();
s2.display();
}
}