Object-Oriented Programming Basic

        This article is for people who want to learn basic before entering into programming life or who want to learn the foundation for programming. Any language  can be chosen only if you are strong in the concept before writing your code and automatically your code will be more powerful and reusable. Below are my examples which I  used for C# Language.

What is Oops?

Oops is a paradigm that is used while designing the application and computer programs. C# also provides full support for , Oops.

Basic Oops Concepts:

    • Classes and Object
    • Data Abstraction and Encapsulation
    • Inheritance
    • Polymorphism

Classes and Object

Class:

Class is a type of object or you can call it as a blueprint for the object instance, as how blueprint is created before building the house.

bluePrint

Example:

Object

Objects are usable instance of classes. It’s a building made from the blueprint called class. Its not logical rather it’s a physical existence that you can enter inside home and use all the properties that belong to it.

home

Example:

myHome is the instance of class type DreamHome. So myHome is built based on DreamHome blueprint.

Note:

class is a logical and object is a physical existence.

Member of Class:

  • Properties and Fields (To describe class data)
  • Methods(To describe class behavior)
  • Events(To Provide communication b/w different class and objects)

Data Abstraction and Encapsulation

Data Abstraction:

It’s a technique that is designed to separate interface and  implementation, i.e, providing essential information to outside world and hiding the background implementations.

Interface

       In the above picture, smart TV  is controlled by remote, so remote is the interface for remote user and TV. Here actual implementation is hidden from user and thereby user takes control of TV with limited facility that is available on remote, but user can’t control TV’s hardware which is present inside TV.

Encapsulation :

It’s a group of related properties, methods and other members that acts as a single unit or object.

Capsule

Interface Example: 

It’s a communication point to the outside world.

 

In the above example we have interface called IGuest, that was implemented in Home class. When guest comes to our home they have only limited access to our home. We will not allow them to access private areas(bed room) and only limited facility is exposed for them. From above example we have more methods but for the guest, they have access  only to CallingBell, OpenMainGate, EnterHall. So guest can’t enter bedroom.

Inheritance

Inheritance enables us to create new classes that can be reusable, extend, and modify the behavior that is defined in the base classes.

Example:

In the above example DreamHome class is inherited from the OldHome. So all the properties and members that belong to OldHome are accessible for the DreamHome. For example, we are re-using the fridge that we already used in our old home to our new DreamHome, which directly means that there is no requirement to buy a new one.

Types of Inheritance

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance
  4. Hybrid Inheritance
  5. Multiple Interface

Polymorphism

Polymorphism is a third pillar of object-oriented programming, after encapsulation and inheritance. It gives ability of an object or reference to take many different forms at different instances.It has two category: compile time and run time

Compile Time Polymorphism:

The decision will be made at compile time.

Example:

Method overloading:

In the example we have two methods with same name, but the parameter is different so compiler will decide which method has to invoke in the compile time itself based on the input method param.

Run Time Plymorphism:

The decision made at Run time.

In the above example “Watch TV” method will be invoked in run time based on the instance created. If you enter DreamHome you can watch the TV Program in new LED Tv or if you enter Old home you can watch the program in old black and white TV. It’s the same method we implement here. But based on instance the decision will be made at application run time.

2 thoughts on “Object-Oriented Programming Basic”

Leave a Reply

Your email address will not be published. Required fields are marked *