OOPS Basic Concept - gpawade/gpawade.github.io GitHub Wiki

Inheritance, encapsulation, abstraction and polymorphism are four of the fundamental concepts of object-oriented programming.

Encapsulation

  • Is hiding unwanted/un-expected/propriety implementation details from the actual users of object.
  • Encapsulation means-hiding data like using getter and setter etc.
  • Information hiding.

Abstraction

  • Is a way of providing generalization and hence a common way to work with objects of vast diversity.
  • Abstraction means- hiding implementation using abstract class and interfaces etc.
  • Abstraction on the other hand is concept of generalizing so that the underneath complex logic is not exposed to the user.
  • Implementation hiding.

Inheritance

  • Inheritance is the mechanism by which an object acquires the some/all properties of another object.
  • It supports the concept of hierarchical classification.

Polymorphism

  • Polymorphism means to process objects differently based on their data type.
  • In other words it means, one method with multiple implementation, for a certain class of action.

Example-

  • Overloading

Overloading in simple words means two methods having same method name but takes different input parameters.

  • Overriding

Overriding means a derived class is implementing a method of its super class.

Type of Polymorphism

  • Runtime/Late binding - Method overriding is perfect example of Runtime polymorphism, reference of class X can hold object of class X or an object of any sub classes of class X.
  • Compile time / early binding - Compile time polymorphism is nothing but the method overloading.

UML Specification

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 relationship between objects.
  • This is represented by a solid line.
  • Let’s take an example of relationship between Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers.
  • Both can be created and deleted independently.

Aggregation

  • It is a specialized form of Association where all object have their own lifecycle but there is ownership.
  • This represents “whole-part or a-part-of” relationship. This is represented by a hollow diamond followed by a line.
  • Let’s take an example of relationship between Department and Teacher. A Teacher may belongs to multiple departments. Hence Teacher is a part of multiple departments. But if we delete a Department, Teacher Object will not destroy.

Composition

  • It is a specialized form of Aggregation. It is a strong type of Aggregation. In this relationship child objects does not have their lifecycle without Parent object. If a parent object is deleted, all its child objects will also be deleted. This represents “death” relationship. This is represented by a solid diamond followed by a line.
  • Let’s take an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room cannot belongs to two different house if we delete the house room will automatically delete.

Dependency

It represents a relationship between two or more objects where an object is dependent on another object(s) for its specification or implementation. This is represented by a dashed arrow.

one class is dependent on another class.

Generalization

Generalization is the process of extracting shared characteristics from two or more classes, and combining them into a generalized superclass.

"is-a-kind-of" relationship.

Specialization

In contrast to generalization, specialization means creating new subclasses from an existing class.

⚠️ **GitHub.com Fallback** ⚠️