Object Oriented Concepts | OOPS Concepts

Object Oriented Concepts

Object Oriented Concepts is a programming style which is associated with the concepts like class, object, Inheritance, Encapsulation, Abstraction, Polymorphism. Most popular programming languages like Java, C++, C#, Ruby, etc. follow an object-oriented programming paradigm.

Inheritance

  • A class that is derived from an already existing class is called as a Derived Class/Subclass.
  • The class from which other classes are derived is called the Base class/Superclass
  • A subclass not only inherits attributes and behaviors from its superclass, but also adds its own unique attributes and behaviors giving it its unique identity.

Generalization/ Specialization

  • Superclasses and subclasses help implement the Generalization/Specialization mechanism that is so very typical and characteristic of a hierarchy.
  • Increasing levels of generalization are observed as we ascend the hierarchy.
  • Increasing levels of specialization are observed as we come down the hierarchy.

Abstract Classes

  • Classes at the very top of a class hierarchy are pure abstractions, instances of which do not exist in the real world.
  • They generally have one or more behaviors that cannot be implemented.
  • But their subclasses can have concrete instances, which provide a concrete implementation of abstract behaviors defined in abstract classes.

Overriding Behaviours

  • There are times when a subclass may choose to provide for its own specific implementation of an inherited behavior.
  • In such a scenario, the subclass chooses to keep the same nomenclature of the behavior as defined in the superclass, but chooses to keep the semantics different specific to its own needs.
  • This is a case where a subclass is said to have overridden a behavior inherited from its superclass.
  • Overridden behaviors across subclasses in a class hierarchy lead to the definition of a generic behavior in a super class that is inherited and overridden by each of the subclasses.
  • Overridden functions lay the groundwork for the “Single Interface, Multiple Implementations” paradigm that is the very essence of Polymorphism.

Polymorphism

  • Polymorphism literally means “anything that is capable of existing in multiple forms. (the root words “Poly” and “Morph” are Greek words that stand for “many” and “forms” respectively.
  • Polymorphism in OO environments is typically associated with overridden behaviors across subclasses in a class hierarchy, each of which has the same name as that in its super class, but chooses to keep its implementation specific to its own needs.

Abstraction

  • Unable to comprehend complexity of real-world objects while attempting to classify them, we look for what is essential relative to our perspective or understanding. This is Abstraction.
  • Abstraction leads to the definition of a well-defined set of public interfaces using which the external world can interact with the object.
  • By interacting with these interfaces, the external world can draw out the behaviors of the object, while choosing to ignore the internal implementation of the object.

Encapsulation

  • Encapsulation is the process of hiding the implementation-level details of an object.
  • Implementation-level details refer to the object’s attributes as well as to the implementation of the object’s behaviors.
  • The internal implementation can only be accessed through the object’s public interfaces.
  • Keeping attributes and related behaviors together is another way of implementing encapsulation.

Abstraction Vs. Encapsulation

  • Abstraction and Encapsulation are two sides of the same coin.
  • If you start abstracting by providing a set of well-defined public interfaces for an object, using which the external world can interact with the object, you start hiding or encapsulating the implementation-level details of the object.
  • Or if you start encapsulating the implementation-level details of an object, you invariably end up defining a set of well-defined interfaces for accessing the internal implementation of the object.
  • Therefore, the conclusion is that if you start abstracting you end up encapsulating and vice versa.