When we talking about, Object Oriented Programming, first name comes into our mind is -> “encapsulation”.
Encapsulation: In object-oriented programming (OOP), encapsulation refers to the bundling of data with the methods that operate on that data, or the restricting of direct access to some of an object’s components. Encapsulation is used to hide the values or state of a structured data object inside a class, preventing direct access to them by clients in a way that could expose hidden implementation details or violate state invariance maintained by the methods.
In simple words, encapsulation means encapsulating objects and classes together. In Java, a class encapsulates the fields, which hold the state of an object, and the methods, which define the actions of the object. Encapsulation enables you to write reusable programs. It also enables you to restrict access only to those features of an object that are declared public. All other fields and methods are private and can be used for internal object processing.
Let’s understand the concepts of classes and objects.
Class – A Java class file is a file (with the .class filename extension) containing Java byte code that can be executed on the Java Virtual Machine (JVM). A Java class file is usually produced by a Java compiler from Java programming language source files (.java files) containing Java classes (alternatively, other JVM languages can also be used to create class files). If a source file has more than one class, each class is compiled into a separate class file.
Let’s understand through below code…
class simple{ public static void main(String[] args){ System.out.println(“this is ‘simple’ class example”); } } |

When we compile above ‘simple.java’ file it generate a byte-code file ‘simple.class’. This file then executed further.
Objects – A typical Java program creates many objects, which as you know, interact by invoking methods. Through these object interactions, a program can carry out various tasks, such as implementing a GUI, running an animation, or sending and receiving information over a network. Once an object has completed the work for which it was created, its resources are recycled for use by other objects.
Let’s understand relation between classes and objects.
Ex – ‘tiger’,’lion’,’cheetah’ all are animals. So here, class = animal , objects = tiger, lion, cheetah
Same as, ‘parrot’,’crow’,’owl’ all birds. So here, class = bird , objects = parrot, crow, owl
Let’s see below code…
class bird{ } public class main1{ public static void main(String[] args){ // parrot,crow,owl objects of class->bird bird parrot = new bird(); bird crow = new bird(); bird owl = new bird(); } } |
Note: An object is an instance of class, and the process of creating an object is called ‘instantiation’.
‘new’ keyword – ‘new’ keyword is used to create objects. When a ‘new’ keyword creates a new object, it also allocates memory for it. When an object is no longer needed, then it does not get space into memory. This work done by the ‘garbage collector’.
Let’s understand by the below code…
class bird{ } public class main1{ public static void main(String[] args){ bird parrot = new bird(); } } |
From the upper code, ‘parrot’ is the object of class ‘bird’.
This object ‘parrot’ creates by the ‘new’ keyword.
Let’s see another code…
class animal{ } public class main2{ public static void main(String[] args){ animal dog = new animal(); } } |
From the upper code, object ‘dog’ belongs to class ‘animal’. This object also created by the ‘new’ keyword.
Note: when an object is created by the ‘new’ keyword, it allocates some memory. It also destroyed when it is no longer needed by the ‘garbage collector’, that time finalize() method get called. finalize() method does not accept any parameter.
Constructor – A constructor is used in the creation of an object that is an instance of a class. Typically it performs operations required to initialize the class before methods are invoked or fields are accessed. Constructors are never inherited.
In class-based, object-oriented programming, a constructor is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.
In Java, we have 3 type of constructors
- No-argument Constructor
- Parameterized Constructor
- Default Constructor
** No-argument constructor – in this constructor any type of arguments does not passed. Let’s see below code…
class OS{ public OS(){ System.out.println(“OS are -> windows,linux,mac,android”); } } class noArg{ public static void main(String[] args){ // object ‘o1’ of class ‘OS’ OS o1 = new OS(); } } |

After new keyword, OS() is the constructor.
** Parameterized constructor – here we passes arguments. Let’s understand through below code…
class OS{ public OS(String nm,int n){ System.out.println(“we loves: “+nm); System.out.println(“current version is: “+n); } } class param{ public static void main(String[] args){ // object ‘o1’ of class ‘param’ OS o1 = new OS(“windows”,11); } } |

Here arguments are = “windows” and 11.
** Default constructor – The default constructor is also called the Empty Constructor. This constructor is inserted by the Java compiler into the class code where there is no constructor implemented by the programmer.
public class defCon { String firstName; String lastName; int age; public static void main(String args[]) { defCon myStudent = new defCon(); myStudent.firstName = “abdul”; myStudent.lastName = “kalam”; myStudent.age = 78; System.out.println(myStudent.age); //100 System.out.println(myStudent.firstName); //Ihechikara } } |
