Object-Oriented Programming Concepts

If you start programming in Java, you must familiar with Object Oriented Programming Concept. Without knowledge about OOP concepts, you will not be able (elegantly) to design systems in object oriented programming model.


Core Object Oriented Programming Concepts are:

  1. Inheritance
  2. Polymorphism
  3. Encapsulation
  4. Abstraction

And object relation concepts:

  1. Association
  2. Aggregation
  3. Composition

Let’s look into these concepts one by one:

Inheritance

Inheritance is a mechanism wherein a new class is derived from an existing class. In Java, classes may inherit or acquire the properties and methods of other classes.

A class derived from another class is called a subclass, whereas the class from which a subclass is derived is called a superclass. A subclass can have only one superclass, whereas a superclass may have one or more subclasses.

OOP UML Inheritance & Polymorphism.png

Inheritance and Polymorphism

Polymorphism

Polymorphism refers to a programming language's ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes.

There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism. We can perform polymorphism in java by method overloading and method overriding.

More in Polymorphism in Java

Encapsulation

Encapsulation is the process of hiding information details and protecting data and behavior of an object from misuse by other objects. In Java, encapsulation is done using access modifiers (public, protected, private) with classes, interfaces, setters, getters.

More in Encapsulation in Java

OOP UML Interface & Abstract.png

Payable (Interface) and Staff (Abstract)

Abstraction

Abstraction is a process of hiding the implementation details from the user. Оnly the functionality will be provided to the user. In Java, abstraction is achieved using abstract classes and interfaces.

More in Abstraction in Java.

Association

In association, relation between two separate classes establishes through their objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many.

OOP UML Association

Association

Let’s take an example of Customer and Order. One customer can associate with multiple orders. It's represent one-to-many relationship.

Composition and Aggregation are the two forms of association.

Aggregation

OOP UML Aggregation

Aggregation

Aggregation is a specialized form of Association where all objects have their own life cycle, where the child can exist independently of the parent. Aggregation is also called a “Has-a” relationship.

Let’s take an example of Class (parent) and Student (child). Delete the Class, Student objects will not be destroyed and still exists.

Composition

OOP UML Composition

Composition

Composition is a restricted form of Aggregation in which two classes are highly dependent on each other. There's a strong life-cycle dependency between the parent and child. The parent cannot exist without the child, and the child has no meaning without the parent.

Let’s take an example of Car (parent) and Engine (child). A car needs engine to run. Although engine may exist as separate parts, detached from a specific car, but it's no use without the parent (car).

More in Association, Aggregation, and Composition in Java.