In this blog, We will see Object Oriented Programming Principal in simple and easy way.
What is Object Oriented Programming Concept?
Object oriented is an approach to do programming in C#, Java etc. Before object oriented programming structural programming model was in use. C programming language is an example of structural programming.
But the problem with structural programming is, code maintainability, code re-usability. So, to resolve these problems Object Oriented programming was introduced.
In object oriented programming everything moves around Object and Class.
Let’s understand Object Oriented approach with an example.
Now a days everybody is using a smartphone.
Think your smartphone as an object.
So, this object (smartphone) have so many attributes/characteristics like Model, Color, Price.
This object (smartphone) will have some functionality or behavior i.e. voice calling, video calling, sms service, game etc.
Now, you may collect all attributes and functionalities of smartphone in a box, this box will have a ready made blueprint or template to set attributes and functionalities of smartphone.
This box is your Class in Object Oriented Programing approach, where attributes are similar to variables or data. And behaviors are similar to Methods.
MobilePhone phone; What this line says.
In above line MobilePhone is the name of a class and phone is the object. This is the way to declare an object in C# or Java.
What is an object?
Object is an instance of a class.
MobilePhone phone;
MobilePhone is the name of a class and phone is the object.
You may create multiple objects of a class.
What is class?
Class is a blueprint or a template. Which contains variables and functionalities.
Class name should always start with Capital letter, if you will start a class name with lower case, it will not give you any error, but this is not a good practice to write program.
Below are some good practices to write class name –
Mobilephone
Cars
Students
Library
Below are not a good practice to write class name –
mobilePhone
cars
students
library
Object Oriented Programming Principals
Object Oriented Programming is methodology or an approach. So there are few principals which define this programming methodology.
There are 4 OOPS principals
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
1. Abstraction –
Abstraction is showing or exposing what is necessary and leave the rest. Let’s see in details here.
You bought a Laptop. You press the power button and Laptop starts.
While pressing the Power button, did you think about – How power will go to motherboard? How processor will execute? How SMPS fan is connected with motherboard?
The answer is No.
Why because, you just care about the Laptop screen, you press the power button and see the Laptop screen. You don’t bother about Motherboard, circuit, processor, chip, memory etc.
Abstraction is just to let you know what is necessary for you.
In Object Oriented programming, if you are calling a function, you just need to to call it with required parameter. You don’t bother about what logic written inside that function.
Let’s demonstrate this by sample code.
class MyTestClass { public void MyTestFunction(string userName, string password, int rollNumber) { bool result = AuthenticateUser(userName, password); if(result) { //Write some logic for eg – Check student details based on rollNumber } } private bool AuthenticateUser(string userName, string password) { //Write some code //If user authenticated then return true. return true; } } |
In above code, AuthenticateUser function is private, because outside world need not to know about this function. Out side world will just call MyTestFunction with required parameter and rest of the work will be done by this function.
So, exposing what is necessary to outside world is called as Abstraction.
2. Encapsulation –
Encapsulation is hiding the data from outside world.
Let’s understand this by an example –
class Students
{
private int rollNumber;
private string studentName;
public GetStudentMarks(rollNumber)
}
In above sample code, I have created a class called Students and declared few private variables i.e. rollNumber and studentName.
GetStudentMarks is a function which shows the marks of a student based on passed rollNumber.
Now see, data declared in above class is private, it means these data can not be accessible outside the Student class.
And the function declared as public. It means external modules can call GetStudentMarks function to see the marks of a student. External world has no access to data available in the class.
Here, what we are doing? We are protecting our data and giving access of function to outside world.
So Encapsulation means, hiding data into a single unit i.e. in a class.
See sample code below.
class MyTestClass { int i = 10; string name = “XYZ”; public void MyTestFunction() { //Write some code } } |
3. Inheritance –
Inheritance means using someone’s property i.e. inheriting property from a class.
For Eg – A child always inherit some properties from their parents. His eyes are similar to his father. This is a property inherited from his father.
Similarly, in object oriented programming a class can inherit another class.
Below code shows a sample inheritance.
Child class can inherit or use the property of Parent class.
class Child:Parent { } class Parent { } |
Important Points about Inheritance:
Parent Class Constructor must be accessible to Child class. It means either you don’t create any constructor in parent class because by if you don’t create a constructor in a class by default it will be public or if you are creating a constructor then it must be Public.
If you create a private or protected constructor then inheritance will not exist.
4. Polymorphism –
It is a Greek word which means many forms. In Object Oriented Programming single method can be written in multiple forms.
See below code.
MyMethod1() is a function which has 3 forms.
First form accepts no parameter.
2nd form accepts one parameter i.e. int i;
3rd form accepts 2 parameters i.e. int i and int j
public void MyMethod1() { Console.WriteLine(“Form1”); } public void MyMethod1(int i) { Console.WriteLine(“Form2”); } public void MyMethod1(int i, int j) { Console.WriteLine(“Form3”); } |
Now these methods can be called like this –
MyTestClass objTest = new MyTestClass(); objTest.MyMethod1(); //First form will be called objTest.MyMethod1(10); //First form will be called objTest.MyMethod1(20,30); //First form will be called |
In above code, method name is same but parameters are different.
There are 2 type of Polymorphism –
Method Overloading –
A method with same name with different parameters is called Method Overloading. Method Overloading can be in same class or in different class. Method Overloading is also called as Early Binding.
Examples of Method Overloading –
public void Method1()
public void Method2(int i);
public void Method3(float j);
public void Method4(int i, string s);
public void Method5(string s, int i) ;
Below are Incorrect examples of Method Overloading –
public void Method1();
public int Method1();
Method Overloading says same name with different parameters.
Method Overriding –
A method with same name and same signature is called as Method overriding. Method Overriding must be in different class. i.e. Method Overriding implement a parent class in child class with same signature. Method Overriding invokes at runtime, so it is also called as Late Binding.
Below is the code to explain Method Overriding –
To override a method, you must use a method with virtual keyword in parent class and use override keyword with that method in child class.
Output for below code is – “Child Class”
class Program { public static void Main(string[] args) { ChildClass c = new ChildClass(); c.Add(10, 20); } } public class BaseClass { public virtual void Add(int a, int b) { Console.WriteLine(“Base class”); } } public class ChildClass:BaseClass { public override void Add(int a, int b) { Console.WriteLine(“Child class”); } } |