Object Oriented Concepts (OOC)

What are the Object Oriented Concepts?

OOC is the basic and foundation of Object Oriented Languages such as Java, C# and C++. Primary purpose of this OOC is to increase the flexibility and maintainability of your code. It brings together data and its behaviors (methods) in a single location.

Before going through these concepts we have to get clear idea about the Classes and Objects.

A class is a blueprint, prototype or a template that defines the variables and the methods common to all objects.

An object is a member or an instance of a class.

Lets see what are the Object Oriented Concepts.

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

1. Abstraction:

Abstraction is the process where you show only, ‘relevant’ data and ‘hide’ unnecessary details from the user.

  • Car is a single object, which can be managed by the help of other sub systems such as, engine, gearing system and lighting system. But most of these inner systems are unknown to users. That is never effected to the usage of a car. Simply that is the Abstraction.

2. Encapsulation: (AKA Data hiding)

Binding the data with the code that manipulate it. It keeps the data and the code safe from external interfaces.

Everyone knows how to access it. It can be easily used regardless of implementation details. The special thing is, there shouldn’t any side effects of the code, to the rest of application.

  • Public: can be accessed in anywhere in your program.
  • Default: can be accessed only the declared package.
  • Protected: Can be accessed only the classes of the same package and the subclasses present in any package.
  • Private: Can be accessed only the class.
  • Every one knows we can turn a vehicle by using the its steering wheel. But, is it the only one system in the car? No, never, there are lots of other systems. But the thing is, those subsystems are never effected to the “Car turning function”. As well as these function also never affected to the other functions. Simply that is the Encapsulation.

3. Inheritance:

This is the mechanism by which an object acquires the properties of another object.

The aim is to provide reusability of the code. So that a class has to write only the unique features and rest of the common properties and functionalities can be extended from the another class.

Inheritance is known as IS-A relationship between Parent class and Child class.

  • Parent class/ Super class/ Base class: The class whose properties and functionalities are used (inherited) by another class.
  • Child class/ Sub class/ derived class: The class that extends the feature of another class.
  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance
  • In here, there are few kinds of cars, but each and every one has a some unique feature for it. So we can get a Super class as a ‘Car’ with common features and functionalities. And we can get Sub classes as ‘Audi’, ‘GTR’, ‘Dodger’ and few others and they can extends the Super class called ‘Car’. Simply that is the Inheritance.

4. Polymorphism:

It means to process objects differently based on their data types.

One method with multiple implementations for a certain class of action and which implementation to be used is decided at runtime depending upon the situation.

Polymorphism is the capability of a method to do different things based on the object that it is act on.

  • Method Overloading (Compile time polymorphism/ Static polymorphism)
  • Method Overriding (Runtime polymorphism/ Dynamic Polymorphism)

This allows to have more than one method having the same name.

There are three ways to overload a method:

  • Number of parameters:
public void add (int num1, int num2){...}
public void add (int num1, int num2, int num3){...}
  • Data types of parameters:
public void add (int num1, int num2){...}
public void add (float num1, float num2){...}
  • Sequence of data types:
public void add (float num1, int num2){...}
public void add (int num1, float num2){...}

But we cannot to change the return type of methods.

public int add(int num1, int num2){...}
public float add(float num1, float num2){...}
//this code generate a compilation error.

Declaring a method in child class which is already present in parent class. Child class can give its own implementation to which is already provided in parent class.

In this case, the method which is in parent class called as ‘Overridden method’ and the method which is in child class called as ‘Overriding method’.

//Parent class
class Vehicle {
public void startVehical();
//Child class
public class Car extends Vehicle {
public void startVehical(){
//implementation of normal car starting mechanism
//Child class
public class ElectricCar extends Vehicle {
public void startVehical(){
*/implementation of Electric car starting mechanism. that is not similar to normal car starting mechanism./*
  • In this picture there are two type cars. first on is normal engine car and second one is Tesla’s Cybertruck. before to run both cars it must start, So that starting is common function to both classes. but the implementation of the two functions are not same. Simply that is the Polymorphism.

That’s all for the basic of the Object Oriented Concepts. I hope you all enjoy my blog. Thank you for reading. Stay safe.