There are four major concepts of object Oriented programming.
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
Abstraction
public class ParentClass
public class ParentClass
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
Abstraction
In object-oriented software, complexity is managed by using abstraction. Abstraction is a process that involves identifying the critical behavior of an object and eliminating irrelevant and complex denials. Abstraction is a process of identifying the relevant qualities and behaviors an object should possess.
Example- A Laptop consists of many things such as processor, motherboard, RAM, keyboard, LCD screen, wireless antenna, web camera, USB ports, battery, speakers etc. To use it, you don't need to know how internally LCD screens, keyboard, web camera, battery, wireless antenna, speaker’s works. You just need to know how to operate the laptop by switching it on.
Note- When derived class inherited with abstract class; derived class must be override abstract class methods.
Example of Abstraction: -
using System;
using System.Collections.Generic;
using System.Linq;
namespace abstarction
{
public abstract class university
{
public abstract void BTech();
public abstract void MBA();
}
public class GBTU : university
{
public override void BTech()
{
Console.WriteLine("GBTU BTech Fee 50000/-");
}
public override void MBA()
{
Console.WriteLine("GBTU MBA Fee 100000/-");
}
}
public class MTU : university
{
public override void BTech()
{
Console.WriteLine("MTU BTech Fee 40000/-");
}
public override void MBA()
{
Console.WriteLine("MTU MBA Fee 800000/-");
}
}
class Program
{
static void Main(string[] args)
{
GBTU g = new GBTU();
g.BTech();
g.MBA();
MTU m = new MTU();
m.BTech();
m.MBA();
Console.ReadLine();
}
}
}
Output-
GBTU BTech Fee 50000/-
GBTU MBA Fee 100000/-
MTU BTech Fee 40000/-
MTU MBA Fee 800000/-
Explanation :-
Ø From above example we have make one abstract class university and two abstract methods Btech and MBA. GBTU and MTU both are override university course fee.
Ø University course common for both GBTU and MTU so university method BTech and MBA is abstract.
Ø GBTU and MTU inherited abstract method so university method must be override here.
Encapsulation:
Wrapping up data member and method together into a single unit (i.e. Class) is called Encapsulation.
Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object.
Encapsulation is like your bag in which you can keep your pen, book etc. It means this is the property of encapsulating members and functions.
class Bag
{
book;
pen;
ReadBook();
}
Encapsulation means hiding the internal details of an object, i.e. how an object does something.
Encapsulation prevents clients from seeing its inside view, where the behaviour of the abstraction is implemented.
Encapsulation is a technique used to protect the information in an object from the other object.
Hide the data for security such as making the variables as private, and expose the property to access the private data which would be public.
So, when you access the property you can validate the data and set it.
Example:
class Demo
{
private int _mark;
public int Mark
{
get { return _mark; }
set { if (_mark > 0) _mark = value; else _mark = 0; }
}
}
Real world Example of Encapsulation:-
Let's take example of Mobile Phone and Mobile Phone Manufacturer
Suppose you are a Mobile Phone Manufacturer and you designed and developed a Mobile Phone design(class), now by using machinery you are manufacturing a Mobile Phone(object) for selling, when you sell your Mobile Phone the user only learn how to use the Mobile Phone but not that how this Mobile Phone works.
This means that you are creating the class with function and by making object (capsule) of it you are making availability of the functionality of you class by that object and without the interference in the original class.
Inheritance:
When a class acquire the property of another class is known as inheritance.
Inheritance is process of object reusability.
For example, A Child acquire property of Parents.
public class ParentClass
{
public ParentClass()
{
Console.WriteLine("Parent Constructor.");
}
public void print()
{
Console.WriteLine("I'm a Parent Class.");
}
}
public class ChildClass : ParentClass
{
public ChildClass()
{
Console.WriteLine("Child Constructor.");
}
public static void Main()
{
ChildClass child = new ChildClass();
child.print();
}
}
Output:
Parent Constructor.
Child Constructor.
I'm a Parent Class.
Child Constructor.
I'm a Parent Class.
Inheritance:
When a class acquire the property of another class is known as inheritance.
Inheritance is process of object reusability.
For example, A Child acquire property of Parents.
public class ParentClass
{
public ParentClass()
{
Console.WriteLine("Parent Constructor.");
}
public void print()
{
Console.WriteLine("I'm a Parent Class.");
}
}
public class ChildClass : ParentClass
{
public ChildClass()
{
Console.WriteLine("Child Constructor.");
}
public static void Main()
{
ChildClass child = new ChildClass();
child.print();
}
}
Output:
Parent Constructor.
Child Constructor.
I'm a Parent Class.
Child Constructor.
I'm a Parent Class.
Polymorphism we can achieve by two ways in .Net.
1. Compile Time Polymorphism (Method Overloading ,operator Overloading)
2. Run Time Polymorphism (Using Virtual Functions and doing method overriding)