OOPS Concepts in Java with Realtime Examples

In this article, we will learn important OOPS concepts in Java with real-world examples.

As we all know Object Oriented Programming Concepts are very important. Without having an idea about OOPS concepts, you will not be able to design systems in the object-oriented programming model. It simplifies software development and maintenance.

The core OOPs concepts:
  1. Object
  2. Class
  3. Abstraction
  4. Encapsulation
  5. Inheritance
  6. Polymorphism
Let's discuss above each OOPS concept with a real-world example.

1. Object

The Object is the real-time entity having some state and behavior. In Java, an Object is an instance of the class having the instance variables like the state of the object and the methods as the behavior of the object. The object of a class can be created by using the new keyword in the Java Programming language.
A class is a template or blueprint from which objects are created. So, an object is the instance(result) of a class.
I found various Object Definitions:
1. An object is a real-world entity.
2. An object is a runtime entity.
3. The object is an entity that has a state and behavior.
4. The object is an instance of a class.

Real-world examples

  • Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Chair, Bike, Marker, Pen, Table, Car, Book, Apple, Bag, etc. It can be physical or logical (tangible and intangible).
  • Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes).

Read more about Objects in Java with examples at What Is Object in Java with Programming Examples.

2. Class

A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. In short, a class is the specification or template of an object.
Read more about Class in java with examples at What is Class in Java with Programming Examples.
A real-world example is Circle. Let’s look at an example of a class and analyze its various parts in the below diagram. This example declares the class Circle, which has the member variables x, y, and radius of type Integer and the two member-methods, area()and fillColor().

A class is a template for creating objects

Below diagram shows a Circle class which is a template to create three objects:

3. Abstraction

Abstraction means hiding lower-level details and exposing only the essential and relevant details to the users.

Real-world examples

  1. Example 1: Let's consider a Car, which abstracts the internal details and exposes to the driver only those details that are relevant to the interaction of the driver with the Car.

  2. Example 2: Consider an ATM Machine; All are performing operations on the ATM machine like cash withdrawal, money transfer, retrieve mini-statement…etc. but we can't know the internal details about ATM.
Read more about Abstraction in Java with examples at Abstraction in Java with Example.

4. Encapsulation

Encapsulation is a process of wrapping data and methods in a single unit is called encapsulation.
In OOP, data and methods operating on that data are combined together to form a single unit, which is referred to as a Class

Encapsulation is the mechanism that binds together code and the data it manipulates and keeps both safes from outside interference and misuse. One way to think about encapsulation is as a protective wrapper that prevents the code and data from being arbitrarily accessed by other code defined outside the wrapper.

Real-world examples

  1. The capsule, it is wrapped with different medicines. In a capsule, all medicine is encapsulated inside capsule.
  2. A Java class is an example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.
    Read more at Encapsulation in Java with Example

5. Inheritance

Inheritance is a process of obtaining the data members and methods from one class to another class, plus can have its own is known as inheritance. It is one of the fundamental features of object-oriented programming.

Inheritance - IS-A relationship between a superclass and its subclasses.

Super Class: The class whose features are inherited is known as a superclass (or a base class or a parent class).
Sub Class: The class that inherits the other class is known as a subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.

Real-world example

  1. The real-life example of inheritance is child and parents, all the properties of a father are inherited by his son.
  2. In the Java library, you can see extensive use of inheritance. The below figure shows a partial inheritance hierarchy from java.lang library. The Number class abstracts various numerical (reference) types such as ByteIntegerFloatDoubleShort, and BigDecimal.
Read more about Inheritance at Inheritance in Java with Example.

6. Polymorphism

The process of representing one form in multiple forms is known as Polymorphism.

Different definitions of Polymorphism are:

  1. Polymorphism allows us to perform a single action in different ways.
  2. Polymorphism allows you to define one interface and have multiple implementations
  3. We can create functions or reference variables that behave differently in a different programmatic context.
  4. Polymorphism means many forms.

A real-world example of polymorphism

Suppose if you are in a classroom at that time you behave like a student, when you are in the market at that time you behave like a customer when you at your home at that time you behave like a son or daughter, Here one person present in different-different behaviors.

Read more at Polymorphism in Java with Example
Apart from these core OOPS concepts, there are some other terms that are used in Object-Oriented design:
  • Association
  • Composition
  • Aggregation
  • Delegation
  • Coupling
  • Cohesion



  • It represents a relationship between two or more objects where all objects have their own lifecycle and there is no owner. The name of an association specifies the nature of the relationship between objects.
  • Association is a relation between two separate classes that establish through their Objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many.
  • In Object-Oriented programming, an Object communicates to other Objects to use functionality and services provided by that object.
There are two forms of association
  • Composition
  • Aggregation
Read more on Association in Java with Example



Composition is an association that represents a part of a whole relationship where a part cannot exist without a whole. If a whole is deleted then all parts are deleted. It has a stronger relationship. Key Points
  • It represents a part-of-relationship.
  • In composition, both entities are dependent on each other.
  • When there is a composition between two entities, the composed object cannot exist without the other entity.
  • For example, if order HAS-A line-items, then an order is a whole, and line items are parts. If an order is deleted then all corresponding line items for that order should be deleted.
  • Favor Composition over Inheritance.
Read more on Composition in Java with Example



  • Aggregation is an association that represents a part of a whole relationship where a part can exist without a whole. It has a weaker relationship.
  • It is a specialized form of Association where all object has its own lifecycle but there is ownership. This represents a “whole-part or a-part-of” relationship.
  • Let’s take an example of the relationship between the Department and the Teacher. A Teacher may belong to multiple departments. Hence Teacher is a part of multiple departments. But if we delete a Department, the Teacher Object will not destroy.
Read more on Aggregation in Java with Example



  • Hand over the responsibility for a particular task to another class or method.
  • It is a technique where an object expresses certain behavior to the outside but in reality delegates responsibility for implementing that behavior to an associated object
Use the Delegation in order to achieve the following
  • Reduce the coupling of methods to their class
  • Components that behave identically, but realize that this situation can change in the future.
  • If you need to use functionality in another class but you do not want to change that functionality then use delegation instead of inheritance.
Read more on Delegation in Java with Example



Coupling refers to the degree to which one class knows about another class. If one class uses another class, that is coupling. Low dependencies between “artifacts” (classes, modules, components). There shouldn’t be too much dependency between the modules, even if there is a dependency it should be via the interfaces and should be minimal.
Key Points
  • While creating a complex application in java, the logic of one class will call the logic of another class to provide the same service to the clients.
  • If one class calling another class logic then it is called collaboration.
  • When one class is collaborating with another class then there exists tight-coupling between the two classes.
  • If one class wants to call the logic of a second class then the first-class need an object of second class it means the first class creates an object of second class.
Read more on Coupling in Java with Example



The term cohesion is used to indicate the degree to which a class has a single, well-focused responsibility. Cohesion is a measure of how the methods of a class or a module are meaningfully and strongly related and how focused they are in providing a well-defined purpose to the system.


  • In object-oriented design, cohesion refers all to how a single class is designed. Cohesion is the Object-Oriented principle most closely associated with making sure that a class is designed with a single, well-focused purpose.
  • The more focused a class is, the more cohesiveness of that class is more.
  • The advantage of high cohesion is that such classes are much easier to maintain (and less frequently changed) than classes with low cohesion. Another benefit of high cohesion is that classes with a well-focused purpose tend to be more reusable than other classes.
Read more on Cohesion in Java with Example


We have discussed below OOPS concepts with real-world examples:
  • Object
  • Class
  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism
  • Association
  • Composition
  • Aggregation
  • Delegation
  • Coupling
  • Cohesion 



  1. I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful.
    Data Science Training Institute in Bangalore

  2. I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
    Best Data Science Courses in Bangalore

  3. Thumbs up guys your doing a really good job. It is the intent to provide valuable information and best practices, including an understanding of the regulatory process.
    Cyber Security Course in Bangalore

  4. Wow! Such an amazing and helpful post this is. I really really love it. I hope that you continue to do your work like this in the future also.
    Ethical Hacking Training in Bangalore

  5. I am impressed by the information that you have on this blog. Thanks for Sharing
    Ethical Hacking in Bangalore

  6. Here at this site really the fastidious material collection so that everybody can enjoy a lot.

    Data Science Course

  7. Your work is very good and I appreciate you and hopping for some more informative posts.

    Data Science Training

  8. Looks like heading for Cohesion is written by mistake to "coupling"

  9. The blog which you have shared is more creative... Waiting for your upcoming data...
    Java platform independent
    Why Java is Platform Independent


Post a Comment