OOPS concepts in Java

In this blog, we are going to learn about the basics of OOPs concept. Object-oriented programming is a model that provides different types of concepts, such as inheritance, abstraction, polymorphism, etc. These concepts aim to implement real-world entities in programs. They create working methods and variables to reuse them without compromising the security. This emphasizes on data rather than the functions. Many of the most widely used and significant object-oriented programming languages include java, C++, C#, JavaScript, Python, Ruby, Perl, Smalltalk etc.

What is OOPs?

Object-oriented programming is a method used for designing a program using classes and objects. Object-oriented programming is also called the core of java. Object-oriented programming organizes a program around objects and well-defined interfaces. This can also be characterized as data controlling for accessing the code. In this type of approach, programmers define the data type of a data structure and the operations that are applied to the data structure. This implies software development and maintenance by using some of the concepts:

  • Object 
  • Class
  • Abstraction
  • Inheritance 
  • Polymorphism
  • Encapsulation

What are the Concept of oops in JAVA ?


Objects are always called as instances of a class. Objects are created from class in java or any other languages. Objects are those that have state and behaviour. Objects are abstract data types (i.e., objects behaviour is defined by a set of values and operations).

These objects always correspond to things found in the real world, i.e., real entities. So, they are also called a run time entity of the world. These are self–contained which consists of methods and properties which makes data useful. Objects can be both physical and logical data. It contains addresses and takes up some space in memory. Some of the examples of objects are a dog, chair, tree etc. 

When we treat animals as objects, it has states like colour, name, breed etc., and behaviours such as eating, wagging the tail etc.

Suppose, we have created a class called My book, we specify the class name followed by the object name, and we use the keyword new.

Example 1:

Public class Mybook {
int x=10;
Public static void main (String args []) {
Mybook Myobj= new Mybook ();

In the above example, a new object is created, and it returns the value of x which may be the number of books.

Mybook Myobj= new Mybook ();

 This is the statement used for creating objects.


This statement is used to return the value of x of an object.

We can also create multiple objects in the same class and we can create in one class and access it in another class. This method is used for better organization of classes and always remember that name of the java file and the class name remains the same. 

Example 2:

The below example shows how multiple objects are created in the same class and how they are accessed from another class.

  • Mybook.java
Public class Mybook {
int x=10;
int y=8;

  • Count.java
Class Count {
Public static void main (String [] args)
Mybook myobj1 = new myobj1();
          Mybook myobj2 = new myobj2();
           System.out.println (myobj1.x);
System.out.println (myobj2.y);

When this program is compiled, it gives the result as 10, 8 respectively.


Classes are like an object constructor for creating objects. Collection of objects is said to be a class. Classes are said to be logical quantities. Classes don’t consume any space in the memory. Class is also called a template of an object. Classes have members which can be fields, methods and constructors. A class has both static and instance initializers.

A class declaration consists of:

  • Modifiers 
  • Class name 
  • Keywords
  • The class body within { } brackets.

A class  keyword is used to create a class. A simplified general form of class definition is given below:

class classname {
type instance variable 1;
type instance variable 2;
type instance variable n;
type methodname 1 (parameter list) {
// body od method 
type methodname 2 (parameter list) {
// body od method 
type methodnamen (parameter list) {
// body od method 

The variables or data defined within a class are called as instance variables. Code is always contained in the methods. Therefore, the methods and variables defined within a class are called members of the class. All the methods have the same form as main () these methods are not specified as static or public. 


Abstraction is a process which displays only the information needed and hides the unnecessary information. We can say that the main purpose of abstraction is data hiding. Abstraction means selecting data from a large number of data to show the information needed, which helps in reducing programming complexity and efforts.  

There are also abstract class and abstract methods. An abstract class is a type of class that declares one or more abstract methods. An abstract method is a method that has a method definition but not implementation. Once we have modelled our object using data abstraction, the same sets of data can also be used in different applications—abstract classes, generic types of behaviours and object-oriented programming hierarchy. Abstract methods are used when two or more subclasses do the same task in different ways and through different implementations. An abstract class can have both the methods, i.e., abstract methods and regular methods.

Now let us see an example related to abstraction.

Suppose we want to create a student application and ask to collect the information about the student.

We collect the following information.  

  •  Name 
  • Class
  • Address
  • Dob
  • Fathers name
  • Mothers name and so on. 

We may not require every information that we have collected to fill the application. So, we select the data that is required to fill the application. Hence, we have fetched, removed, and selected the data, the student information from large data. This process is known as abstraction in oops concept.

Abstract class example:

//abstract parent class 
    	Abstract class animal {
    	 //abstract method 
   	  public abstract void sound ( ) ;
   	 Public class lion extends animal {
  	  Public void sound ( ) {
System.out.println (“ roar “ );
public Static void main ( String args [ ] ) {
 animal obj = new lion ( );
obj. sound ();



Inheritance is a method in which one object acquires/inherits another object’s properties, and inheritance also supports hierarchical classification. The idea behind this is that we can create new classes built on existing classes, i.e., when you inherit from an existing class, we can reuse methods and fields of the parent class. Inheritance represents the parent-child relationship.

For example, a whale is a part of the classification of marine animals, which is part of class mammal, which is under that class of animal. We use hierarchical classification, i.e., top-down classification. If we want to describe a more specific class of animals such as mammals, they would have more specific attributes such as teeth; cold-blooded, warm-blooded, etc. This comes under the subclass of animals where animals come under superclass. The subclass is a class which inherits properties of the superclass. This is also called a derived class. A superclass is a base class or parental class from which subclass inherits properties.

We use inheritance mainly for method overriding and R:

To inherit a class, we use the extend keyword. 

There are five types of inheritance single, multilevel, multiple, hybrid and hierarchical. 

  • Single level  

In this one class i.e., derived class inherits properties from its parental class.  This enables code reusability and also adds new features to the code. Example: class b inherits properties from class a.

Class A is base or parental class and class b is derived class.


Class a {
Class b extends class a {

  • Multilevel

This one class is derived from another class which is also derived from another class i.e., this class has more than one parental class, hence it is called multilevel inheritance.


Class a {
Class b extends class a {
Class c extends class b {

  • Hierarchical level 

In this one parental class has two or more derived classes or we can say that two or more child classes has one parental class.


Class a {
Class b extends class a {
Class c extends class a {

  • Hybrid inheritance

This is the combination of multiple and multilevel inheritance and in java multiple inheritance is not supported as it leads to ambiguity and this type of inheritance can only be achieved through interfaces.

Consider that class a is the parental or base class of class b and class c and in turn class b and class c are parental or base class of class d. Class b and class c are derived classes from class a and class d is derived class from class b and class c.

Following program creates a super class called add and a subclass called sub, uses extend keyword to create a subclass add.

// a simple example of inheritance 
//create a superclass
Class Add {
int my;
int by;
void setmyby (int xy, int hy) {
/create a sub class
class b extends add {
int total;
void sum () {
public Static void main (String args [ ] ) {
b subOb= new b ( );
subOb. Setmyby (10, 12);
subOb. Sum ( ) ;
System.out.println(“total =” + subOb. Total);

It gives output as – total = 22


Polymorphism refers to many forms, or it is a process that performs a single action in different ways. It occurs when we have many classes related to each other by inheritance. Polymorphism is of two different types, i.e., compile-time polymorphism and runtime polymorphism. One of the examples in Compile time polymorphism is that when we overload a static method in java. Run time polymorphism is also called a dynamic method dispatch is a method in which a call to an overridden method is resolved at run time rather than compile time. In this method, the overridden method is always called through the reference variable. By using method overloading and method overriding, we can perform polymorphism. Generally, the concept of polymorphism is often expressed as one interface, multiple methods.  This reduces complexity by allowing the same interface to be used as a general class of action. 


public class Bird {
Public void sound ( ) {
System.out.println ( “ birds sounds “ );
public class pigeon extends Bird {
public void sound ( ) {
System.out.println( “ cooing ” ) ;
public class sparrow extends Bird ( ) {
Public void sound ( ){
System.out.println( “ chip ” ) ;

In the above example we can see common action sound () but there are different ways to do the same action. This is one of the examples which shows polymorphism.


Encapsulation is one of the concepts in OOPs concepts; it is the process that binds together the data and code into a single unit and keeps both from being safe from outside interference and misuse. In this process, the data is hidden from other classes and can be accessed only through the current class’s methods. Hence, it is also known as data hiding. Encapsulation acts as a protective wrapper that prevents the code and data from being accessed by outsiders. These are controlled through a well-defined interface. 

Encapsulation is achieved by declaring the variables as private and providing public setter and getter methods to modify and view the variable values. In encapsulation, the fields of a class are made read-only or write-only. This method also improves the re-usability. Encapsulated code is also easy to test for unit testing.


class animal {
// private field 
private int age;
//getter method 
Public int getage ( ) {
return age;
//setter method 
public void setAge ( int age ) {
this. Age = age;
class Main {
public static void main (String args []);
//create an object of person 
Animal a1= new Animal ();
//change age using setter 
A1. setAge (12);
// access age using getter 
System.out.println(“ animal age is ” + a1. getage ( ) );

Output: Animal age is 12

In this example, we declared a private field called age that cannot be accessed outside of the class.

To access age, we used public methods. These methods are called getter and setter methods. Making age private allows us to restrict unauthorized access from outside the class. Hence this is called data hiding. 

Can Polymorphism, Encapsulation and Inheritance work together?

When we combine inheritance, polymorphism and encapsulation to produce a programming environment, this environment supports the development of far more robust and scalable programs that do the program-oriented model. A well designed or model of the hierarchy of classes are the basis for reusing the code in which we have spent our time and effort developing and testing.  Encapsulation allows us to migrate our implementations over time without breaking that code which depends on our classes’ public interfaces. Polymorphism allows us to create a readable, clean, sensible code.

As we know, it is through the applications of encapsulation, polymorphism and inheritance that individual parts are transformed into an object; for example, it may be a car, mobile phone etc. This is true in case of computer programs. Through object-oriented principles, the various parts of complex programs are brought together to form a cohesive, robust, maintainable whole.

Many of the features supplied by java are part of its built-in class libraries which do use encapsulation, polymorphism, and inheritance extensively. 

Let us consider a real-world example. Humans are a form of inheritance at one standpoint, whereas cars are more like programs we write. All drivers rely on inheritance to drive different types of vehicles. People interface with the features on cars of all types as we have many different types of vehicles, and some have differences. The implementation of engines, brakes etc., comes under encapsulation and finally comes to polymorphism. We get a wide area of options on the same vehicle as to the anti-lock braking system, traditional braking system or power braking system. The same vehicle as many forms of the braking system is called polymorphism. This example shows us how encapsulation, inheritance and polymorphism are combined.   

Advantages of OOPs Concept 

OOPs concepts are one of the core development approaches which is widely accepted. some of the advantages are:

  • Re-usability

When we say re-usability, it means that “write once, use it multiple times” i.e., reusing some facilities rather than building it again and again, which can be achieved by using class. We can use it n number of times whenever required.

  • Data redundancy 

It is one of the greatest advantages in oops. This is the condition which is created at the data storage when the same piece of data is held at two different places. If we want to use a similar functionality in multiple classes, we can just write common class definitions for the similar functionalities by inheriting them.

  • Code maintenance

It is easy to modify or maintain existing code as new objects which can be created with small differences for the existing ones. It also helps users from doing rework many times. It is time saving as we modify the existing codes incorporating new changes to it.

  • Security

Data hiding and abstraction are used to filter out limited exposure which means we are providing only necessary data to view as we maintain security.

  • Design benefits 

The designers will have a longer and extensive design phase, which results in better designs. At a point of time when the program has reached critical limits, it will be easier to program all non oops one separately.

  • Easy troubleshooting

Using encapsulation objects are self-constrained. So, if developers face any problem easily it can be solved. And there will be no possibility of code duplicity. 

  • Flexibility 
  • Problem solving

Disadvantages of OOPs Concept 

  • Effort – Lot of work is put into creating these programs.
  • Speed – These programs are slower compared to other programs.
  • Size – OOPs programs are bigger when compared to other programs.

Differences between Object-Oriented Programming, Procedural Oriented Programming, Structured Programming? 

Object-oriented programming

  • It is object oriented.
  • It follows a bottom-up approach.
  • These are divided into small parts called objects.
  • These have specifiers like public, private, protected.
  • Adding new functions or data is easy.
  • It provides data hiding and it is more secure.
  • Overloading is possible.
  • Examples are c++, java, python etc.

Procedure oriented programming

  • It is structured oriented.
  • It is divided into small parts called functions.
  • It follows a top-down approach.
  • There are no access specifiers.
  • Adding new data and functions is not easy.
  • This is less secure.
  • Overloading is not possible.
  • Examples FORTRAN, Cobol etc.

Structured programming 

  • This divides a program into functions and modules, using this function and modules it makes the program more understandable and readable. It gives importance to functions rather than data and focuses on development on large software applications.
  • Example c, pascal.


  • OOPs is used to design a program using classes and objects.
  • OOPs is the core of java.
  • Objects are instances of classes and also real entities of the world.
  • Classes are collections of objects; they use keyword class whenever written or named.
  • Polymorphism means many forms that can be overloaded or overridden.
  • Abstraction refers to the data hiding, it displays only the information required by the user.
  • Inheritance is the process of acquiring or inheriting properties of one class to the other.
  • Encapsulation means wrapping of the data, they use get and set methods.
  • Oops concepts have re-usability, code maintenance, code redundancy, security etc. 

This brings us to the end of the blog on OOPS concepts in Java. If you found this helpful and wish to learn more such concepts, you can check out the free courses available on Great Learning Academy.



Please enter your comment!
Please enter your name here

nineteen − 7 =