Loading Image

Monday, August 17, 2020

OOP Basics

OOP is a design philosophy. It stands for Object-Oriented Programming. Object-Oriented Programming (OOP) uses a different set of programming languages than old procedural programming languages.

Object

An object can be considered a "thing" that can perform a set of related activities. OOP terms of an object is an instance of a class. 
My name is Khalid, and I am an example/object of class men. When we say, being human, male or female, we, you, your friend, to me we are forms of this class. A class is simply a logical definition when we have a physical existence. We are objects.

Class

A class is simply a representation of a type of object. It is the blueprint, or plan, or template, that describes the details of an object. A class is a blueprint from which the individual objects are created. Class is composed of three things: a name, attributes, and operations.

OOP has 4 key concepts

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


1. Encapsulation:

The encapsulation is the inclusion-within a program object-of all the resources needed for the object to function, basically, the methods and the data.  It also enables the reusability of an instant of an already implemented class within a new class while hiding & protecting.  Those variables that are declared in private that have to direct access from outer. These variables are seen as hidden from the outer class, This situation is known as data hiding

To Acces in those variables, there are need some public method as Getter() and Setter(), by using these two methods privately declared variables can be accessed. 

Benefits:
  • Data hiding
  • Classes reusability
  •  Code Can be modified without breaking the main code.

2. Inheritance

Inheritance is a way of organizing classes. The term comes from the inheritance of traits like eye color, hair color, and so on. Classes with properties in common can be grouped so that their common properties are only defined once in the parent class. 
Superclass: Inherit its attributes & methods to the subclass. Subclass:  can inherit all its superclass attributes & methods besides having its own unique attributes & methods.





3. Abstraction

Data Abstraction is one of the essential and important features of object-oriented programming in C ++. Abstraction means simply showing the necessary information and hiding the details. Data abstraction refers to providing only the necessary information about data to the outside world by hiding the details or implementation of the background.

For example, Humans can talk, walk, listen, eat, but the details are hidden from the outside world. We can take our skin as the cause of abstraction in our case, hiding the internal process.


4. polymorphism

The word polymorphism means many forms. Simply, we can define polymorphism as the ability to display messages in multiple forms.

At the same time, a person may have different characteristics. A man has a father, a husband, an employee at the same time. So the same person is entitled to different behaviors in different situations. This is called polymorphism.

If we walk using our hands, and not legs, here we will change the parts used to perform something. Hence this is called Overloading.

And if there is a defined way of walking, but I wish to walk differently, but using my legs, like everyone else. Then I can walk like I want, this will be called Overriding.