Complete Object Oriented Programming (OOP) concept in Java


A class is a collection of methods and variables. It is a blueprint that defines the data and behaviour of a type or kind. So, a class is a template for objects, and an object is an instance of a class


An object is a instance of Class. An object is nothing but a self-contained component which consists of methods and properties to make a particular type of data. You can also tell it as a collection of methods and variables, the structure of that object will be defined by the class.


Encapsulation — private instance variable and public accessor methods. Encapsulation is the method to restricting some data access as private and retrieving those data using some public methods
For example, we are hiding the name and dob attributes of person class in the below code snippet.


Inheritance enables the use of attributes and methods from one class to another class.

  • SubClass – The class inherits from another class.
  • SuperClass – The class being inherited from another class.

To inherit from another class you have to use “extends” keyword to inherit it.

Final Keyword

If you do not want a class to be inherited from another class then you have to use “final” keyword before the class name.


Abstraction is a process to hiding some details and showing only essentials information to the user. You can not call an abstract class directly to create an object. Instead you have to inherit it.

Abstract Class

Abstract class is a restricted class that can not be called directly to create an object. You have to inherit it to get it’s variables and methods.

Abstract Method

Abstract method can be called from a different class, which inherited the respective abstract class. Abstract method do not have a body inside the abstract class. The body of the abstract method will be declared from another class.

To access the abstract class you have to inherit it from another class, like the example below.


Java interface is another way to achieve abstraction. Interface contains some methods without bodies. Interface must be implemented by another class with “implements” keyword and the bodies of the interface methods implemented by the implemented class.

Example of interface and implements of interface using a class

Important notes about interface

  • Like abstract classes, interfaces cannot be used to create objects (in the example above, it is not possible to create an “Animal” object in the MyMainClass)
  • Interface methods do not have a body – the body is provided by the “implement” class
    On implementation of an interface, you must override all of its methods
  • Interface methods are by default abstract and public
  • Interface attributes are by default public, static and final
  • An interface cannot contain a constructor (as it cannot be used to create objects)

Why And When To Use Interfaces?

  • To achieve security – hide certain details and only show the important details of an object (interface).
  • Java does not support “multiple inheritance” (a class can only inherit from one superclass). However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma (see example below).


It means one name many forms. It is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Static and dynamic are the two kind of polymorphism.

  • Static polymorphism is achieved using method overloading.
  • Dynamic polymorphism using method overriding.

Any Java object that can pass more than one IS-A test is considered to be polymorphic.

Now, the Deer class is considered to be polymorphic since this has multiple inheritance. Following are true for the above examples −

  • A Deer IS-A Animal
  • A Deer IS-A Vegetarian
  • A Deer IS-A Deer
  • A Deer IS-A Object

All the reference variables d, a, v, o refer to the same Deer object in the heap.