Over a million developers have joined DZone.

10 OOPS Concepts in Java

DZone's Guide to

10 OOPS Concepts in Java

Check out this post to learn more about the top 10 object-oriented programming concepts in Java, including abstraction, coupling, and more!

· Java Zone ·
Free Resource

Java-based (JDBC) data connectivity to SaaS, NoSQL, and Big Data. Download Now.

As you probably 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 because it simplifies software development and maintenance.

In this post, I would like to explain 10 important OOPS concepts with examples. The following diagram shows a list of OOPS concepts in Java that we will be taking a look at in this article.Image title

1. Abstraction


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

Real-World Example

  • A car abstracts the internal details and exposes the driver to only details that are relevant to the interaction of the driver with the car.
  • For example, when we have a phone call, we don't know the internal processing. In Java, we use the abstract class and interface to achieve abstraction.
  • We never buy a "device," but we always buy something more specific: iPhone, GSII, Nokia 3310, etc. Here, the iPhone, GSII, and N3310 are concrete things; the device is abstract.

Read more in-depth information with a class diagram and source code examples here: Abstraction in Java with Example.

2. Encapsulation


Encapsulation refers to combining data and associated functions as a single unit. In OOP, data and functions operating on that data are combined together to form a single unit, which is referred to as a class.

Real-world example:

  • Capsule — it is wrapped with different medicines.
  • A Java class is the example of encapsulation. A Java bean is the fully encapsulated class because all the data members are private here.

Read more in-depth info from this class diagram and source code on Encapsulation in Java with Example

3. Inheritance


Inheritance — this is a relationship between a superclass and its subclasses.

Accordingly, this is a process where one object acquires the members of another; plus, it can have its own.


Inheritance is a reusability mechanism in object-oriented programming in which the common properties of various objects are exploited to form relationships with each other. The abstract and common properties are provided in the superclass, which is available to the more specialized subclasses.

When we say that class B is inherited from another class A, then class B is referred to as a derived class (or subclass), and class A is called as a base class (or superclass). By inheritance, the derived class receives the behavior of the base class so that all the visible member methods and variables of the base class are available in the derived class. Apart from the inherited behavior, the derived class specializes its behavior by adding to or overriding base class behavior.

Read more here with a diagram and source code examples on Inheritance in Java with Example

4. Polymorphism


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

Read more on Polymorphism in Java with Example here.

5. Association


  • 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 is established through their objects. Association can be one-to-one, one-to-many, many-to-one, and many-to-many.
  • In object-oriented programming, an object communicates to another object 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 here.

6. Composition


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 the 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 in-depth examples on Composition in Java with Example here.

7. Aggregation


  • Aggregation is an association that represents 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 objects have their 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 Department and Teacher. A Teacher may belong to multiple departments. Hence, the Teacher is part of multiple departments. But if we delete a Department, the Teacher object will not be destroyed.

Read more about Aggregation in Java and code examples here!

8. Delegation


  • 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, it is the 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 here on Delegation in Java with Example.

9. Coupling


Coupling refers to the degree in which one class knows about another class. If one class uses another class, that is coupling. This includes low dependencies between “artifacts” (classes, modules, components).There shouldn’t be too much of a 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 is calling another class logic, then it is called collaboration.
  • When one class is collaborating with another class, then there exists a tight-coupling between the two classes.
  • If one class wants to call the logic of a second class, then the first class needs an object of a second class. It means the first class creates an object of a second class.

Read more here on Coupling in Java with Example.

10. Cohesion


The term cohesion is used to indicate the degree in 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 about 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 cohesiveness of that class is more.
  • The advantages of high cohesion are 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 in this post.

Learn complete Java programming at  Java Tutorial | Learn Java Programming with Examples

Further Readings — SOLID OOPS Principles

>> Single Responsibility Principle

>>  Open Closed Principle

>> Liskov's Substitution Principle

>> Interface Segregation Principle

>> Dependency Inversion Principle

Connect any Java based application to your SaaS data.  Over 100+ Java-based data source connectors.

java ,object-oriented ,oops ,coupling ,cohesion ,abstraction ,inheritance ,examples ,composition

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}