OOPS Concepts
Ans: Object Oriented Programming (OOP) All Programming Language will support this OOP there are Three main concepts in OOP
a)Encapsulation
b)Abstraction
c)Inheritance
d)Polymorphism
Encapsulation :
Encapsulation is the process of Keeping the data and method together into ObjectsOR
Simple Putting Together into one simple class Like.
OR
Means that a group of related properties, methods, and other members are treated as a single unit or object.
_____________________________________________________________
Class My Class
{
public MyClass()
{
}
int a;
int b;
Public void GetAdd()
{
int C=a+b;
}
}
____________________________________________________________
Abstraction : Hiding the Data
Inheritance:
Inheritance Enables you to Create Class that ReusesOR
Acquiring the Properties of Base Class(Parent Class)
OR
Means Provide Further Implementation ,Reusing of Base Class
__________________________________________________________
Public Class MyClass
{
Public MyClass()
{
}
int a;
int b;
Public void GetMyMethod()
{
int c=a+b;
}
}
Public Class MyChildClass : MyClass
{
Public MyChildClass()
{}
}
C# Doesn't Support Multiple Inheritance By Using Interfaces We Can
Polymorphism
Polymorphism means same operation may Behave Differently in Different ClassesExample of Compile Time Polymorphism: Method Overloading
Example of Run Time Polymorphism: Method Overriding
Method Overloading- Method with same name but with different arguments is called method overloading.
- Method Overloading forms compile-time polymorphism.
- Example of Method Overloading:
class MyClass
{
Public string hello()
{
return "Hello";
}
public string hello(string s)
{
return "Hello"+S.Tostring();
}
}
Method Overriding- Method overriding occurs when child class declares a method that has the same type arguments as a method declared by one of its superclass.
- Method overriding forms Run-time polymorphism.
-Virtual Key word is required for Overriding
- Example of Method Overriding:
Public Class MyClass
{
virtual void hello()
{
Console.WriteLine(“Hello from MyClass”);
}
}
Public Class MyChildchild : MyClass
{
override void hello()
{
Console.WriteLine(“Hello from Child”); }
}
}________________________________________________