OOPS Concepts in Java with Realtime Examples

This post shows 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 concepts with a real-world example.

1. Object

The Object is the real-time entity having some state and behavior. In Java, 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 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 which has 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 a 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. The first example, 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. The second example, 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 internal details about ATM.
  3. The third example, we never buy a "device", but always buy something more specific: iPhone, GSII, Nokia 3310, etc Here, iPhone, GSII, and N3310 are concrete things, the device is abstract.
Read more about Abstraction in Java with examples at Abstraction in Java with Example.

4. Encapsulation

Encapsulation is a process of wrapping of 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

Real-world examples

  1. Capsule, it is wrapped with different medicines. In a capsule, all medicine is encapsulated inside a 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

The 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. Below figure shows a partial inheritance hierarchy from a 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 let us 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 which behaves differently in a different programmatic context.
  4. Polymorphism means many forms.

A real-world example of polymorphism

Suppose if you are in a classroom 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 which are used in Object-Oriented design:
  • Association
  • Composition
  • Aggregation
  • Delegation
  • Coupling
  • Cohesion

Association

Intent

  • 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 which establishes 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 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

Composition

Intent

Composition is an association 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 on Composition in Java with Example

Aggregation

Intent

  • Aggregation is an association 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 their own lifecycle but there is ownership. This represents “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 Teacher is a part of multiple departments. But if we delete a Department, Teacher Object will not destroy.
Read more on Aggregation in Java with Example

Delegation

Intent

  • 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 behaviour to an associated object
Applicability
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

Intent

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 of 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 they 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

Coupling

Intent

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.

Explanation

  • 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 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

Conclusion

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

References

Comments

Post a Comment