OOPS Concepts in Java with Examples

Overview

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.
In this post, I listed out important OOPS Fundamental concepts.

1. Abstraction

Intent

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 to the driver only those details that are relevant to the interaction of the driver with the car.
  • For example phone call, we don't know the internal processing. In java, we use abstract class and interface to achieve abstraction.
  • 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.

2. Encapsulation

Intent

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. Java bean is the fully encapsulated class because all the data members are private here.

3. Inheritance

Intent

Inheritance - IS-A relationship between a superclass and its subclasses.
The process where one object acquires the members of another; plus can have its own.

Explanation

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, such 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.

4. Polymorphism

Intent

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

5. 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

6. 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 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.

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

8. 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.

9. 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 create an object of second class.

10. Cohesion

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.

Related OOPS Posts

Source code of this post is available on GitHub : Object Oriented Design  Guide

Comments