Today we are going to discuss about a very important topic without which we cannot imagine the modern day programming or rather say coding.

Now before understanding about the Object Oriented Programming (OOP) we might have some questions which needs to be answered, i.e.

Why OOP (Object Oriented Programming is important?

Sometimes while writing a big program it happens that we have to create so many functions and methods which in the long run become difficult to memorize, it becomes tedious job for writing a code and the purpose for which we are creating the program or the application gets side tracked and we only think about the methods & functions.

Therefore if we wanted to make programs or applications which would be used in real time world, we would have to think in that way. So in order to make a good real time application it was necessary to map the real world objects to the computer program. Thus came into existence a software designing technique which was called as Object Oriented Programming. This technique helped us to think in terms of real world objects.As the name says object oriented programming, which means that we are coding or programming with respect to the objects in the real world.

For example, we have customers and we have sellers and then we have different types of customers & sellers. So these customers & sellers have different properties, behavior, etc. Therefore these objects and their properties can be mapped into the programming world where if we are creating an application where customers and sellers are important then because of these mapping it will be easier to do the coding. And as the objects change in real world we can also simultaneously change the objects in our coding.

Now customers & sellers can be called as Classes and then if we have discount customer or second hand seller then these are called as Objects.We can create multiple objects in the same class and these objects can be individual objects or they can inherit properties from other objects by creating a relationship between the objects. Sometimes while writing the code we can create a class that doesn’t need to be changed when a new type of object is added.

So now the question arises what is Class & Objects??

There are many different definitions and many different explanations for the understanding of the concept of Class and Object. But here we will take a very simple and standard definition which is:

Class:-

Class is a collection of Templates. Or we can say that Class is an instance of Real World.

Object:-

We can say that Object is an instance of Class.

For the better understanding of this we have a sample code below which will make things clear.

Public class Customer

{
       Customer.obj = new customer();

        Obj.customer1 = “xyz”;

return void();
}

Now here we see that a class named customer is created and in that class there is an object customer1 with “xyz” property is created. Here Customer is the main class and inside this class many objects can be created like customer1, customer2, etc.

In this way if we create class named Train then its objects will be Passengers, Ticket checker, etc. So now we have a clear idea about class and object.

As these concepts are cleared we will now discuss the important properties on which object oriented programming is mainly based.

There are 4 main important properties of OOP which are as follows:

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

Now we will discuss these properties one by one. The first is:

Abstraction:

First of all what do we mean abstraction in OOP?

Abstraction means that it will show only those things or properties or attributes which are necessary.

Now for example in a certain application we need output as mobile numbers so we can create an abstract as (0-9) numbers and by using different combinations of 0-9 we can get any 10 digit we want. So we don’t remember each and every 10 digit mobile number because we have abstract as (0-9) numbers. So here we are showing what is necessary that is 0-9 numbers instead of showing each and every mobile number.

Encapsulation:

As the name itself says encapsulation, which means that covering important things in a cover from the outside environment. We often see that medicine is encapsulated in a capsule so that its medicinal properties are not affected. In the same way encapsulation in OOP can be defined as hiding of all the complexes processes from the outside world and make the objects simple.

For example in railway there is an automatic ticket vending machine where we see the menu and take our tickets. So now on the machine screen we only see the station names and buttons for printing the ticket, but we do not see the complex process of selection of the station name, then going to the server, getting a reply, authentication, calculating the fare etc. This is because these complex processes are encapsulated so that the end user can see only simplified objects and use the application.

Now this encapsulation property of OOP can be implemented by using different access modifiers or we can say keywords. They are as follows:

  1. Private:Only members of class have access to the variables.
  2. Protected: All the members in the current class and in the derived classes can access the variables.
  3. Friend (internal in C#): Only members in the current project have access to the elements.
  4. Protected friend(protected internal in C#): All the members in the current project and all members in the derived class can access the variables.
  5. Public: All the members have access in all classes & projects.

But now as referring to both the definitions of abstraction an encapsulation we kind of feel that both are one and the same that is abstraction says that show what is necessary whereas encapsulation say hide complex process. It is like touching your nose directly or touching it in an indirect way which is one and the same in the end. So is there really any difference between them?

Yes there is a difference between them.

Here encapsulation is implementing abstraction. The main difference is that abstraction is done during a design process or design phase whereas encapsulation is done during the coding phase or the execution phase using access modifiers.

The next property is:

Inheritance:

Here also as the name says, inheritance means taking some properties or behavior from the source. We always say that the child is inheriting all the good things from his/her parents. In the same way inheritance in OOP means defining the parent-child relationship between classes.

In OOP the child class can inherit the properties from the parent class. To understand this we have a sample code below:

It is implemented in coding by using the symbol “:”.

class College

{

public string collegeName;

}

classJuniorCollege: College;

{

public string Grade;

}

Here we have “College” class as parent class and then we have created child class called as “JuniorCollege” which inherits all the properties of the parent class and add a “Grade” property of its own.

Polymorphism:

It can be defined as the property of the object to behave differently under different circumstances or conditions. To understand this we can take a simple example where we consider a simple man who is an employee at the office, a friend at a party, a child to his mother, a husband, a father, etc.

There are 2 types of polymorphism:

1)Static polymorphism: This type of polymorphism is implemented at compile time by using the method overloading. Below is a small sample code to understand this:

//Multiply method with 2 inputs.

Objmaths.multiply(3, 4);

//Multiply method with 3 inputs.

Objmaths.multiply(3, 4, 6);

Here depending on the number of inputs the multiplication logic is executed.

2) Dynamic Polymorphism: This type of polymorphism is implemented using “override” and “virtual” keywords. Below is a small sample code to understand this:

Class Customer

{

     Public string customerName;

     Public string customerCode;

     Public virtual string Type ()

         {

return 0;

          }

}

Class Goldcustomer: Customer

{

    Public override string Type ()

            {

return  goldcustomer;

             }

 }

Now here the parent class can point to its child class and invoke the child class’s function. Here parent class “customer” has all the functions except the type function, so whenever we call the type function in parent class it will return to zero. And child class is “Goldcustomer” which has all the properties inherited from the parent class with an additional property of Type function. Therefore the “virtual” keyword defines the Type function of parent class and then it is overridden by the child class with Goldcustomer because parent class’s Type function will return to zero and hence the overriding happens.

Customer obj ;

obj = new Goldcustomer ();

In this way many child classes such as Goldcustomer, SilverCustomer, Diamondcustomer, etc. can be created and in the same way as above the dynamic polymorphism can be implemented.

As we have discussed the important properties of OOP, we will now discuss some other concepts which are used in OOP.

Abstract Class:

They are known as half defined parent class. Their full implementation is done by child classes. To understand this in simple way we have small sample code below.

Here “Transport” is a half – defined parent class and its strong child classes which fully define parent class are “Roadways” and “Railways”. For defining abstract class we need “abstract” keyword.

public abstract class Transport

{

}

public class Roadways: Transport

{

}

Public class Railways: Transport

Interface:

It is defined as the contract that defines the signature of functionality. In other words it looks like a class but has no implementation. It provides forced implementation.

To understand this we have simple sample code below:

interfaceDatabaseimp

{

voidExecSql () ;

}

Public class SQLServer: Databaseimp

{

      Public void ExecSql ()

}

Here we have a simple interface as “Databaseimp”. Now if any classes which implements this interface also has to provide implementation for “ExecSql”. This is known as forced implementation which is the main property of Interface.

 

 

 

Advertisements