OOP in C# part 02
Encapsulation
combining data,functions into a single unit called class and hiding internal details of object. Generally, Data hides for security such as making the variables as private.Encapsulation is implemented by using access specifiers.C# supports the following access specifiers:
Process of hiding the details of implementation and display the essential feature (describe object, by define their unique & relevant characteristic)
Real world example: TV
Real world example: TV
Encapsulation vs Abstraction
Encapsulation | Abstraction | |
Solves the problem at | Implementation level | Design level |
Hide | Code and data in class to protect | Unwanted data and provide relevant data |
Inheritance
A Class that derive from another class known as base class. This promote reuse and maintainability
Public class parent{
//create constructor
Public parent(){
Console.WriteLine("Constructor.");
}
public void display() {
Console.WriteLine("Parent Class.");
}
}
Public class child:parent{
Public child(){
Console.WriteLine("child Constructor.");
}
}
Public static void main(){
//create new object from child class
child child1=new child();
//call parent display method
child1.display();
}
Polymorphism
- It allows you to invoke derived class methods through a base class reference during run-time.
- It has the ability for classes to provide different implementations of methods that are called through the same name.
Type of Polymorphism
- Overloading - compile time polymorphism / early binding
- Perform different task at the different input parameter
- Public void display(string name){}
- Public void display(int age,double mark){}
- Overriding - run-time polymorphism / late binding
- same method signature but different body
Public class parent{
//create constructor
Public parent(){
Console.WriteLine("Constructor.");
}
Public virtual void display() {
Console.WriteLine("Parent Class.");
}
}
Public class child:parent{
Public child(){
Console.WriteLine("child Constructor.");
}
public override void display(){
Console.WriteLine("Show From Derived Class.");
}
}
Virtual Keyword
- use for modify method,property ,index and event delegate allow it to be overridden in a derived class, use the override keyword in derived class.
- Can not use virtual with static,abstract and private.
Sealed Keyword
- used to stop method overriding in a derived classes.
Abstract Class
- cannot be instantiated.
- least one method defined as abstract or can have one or more abstract method.
- It's only allow to other class to inherit and cannot be instantiated.
- Implement standards
Interface
- NO implementation.
- Only signature (define method without body).
- all the methods and properties are abstract.
- All the methods and properties defined in Interface are by default public and abstract.
- C# doesn't support multiple inheritance so interfaces are used to implement multiple inheritance.
Feature | Interface | Abstract class |
Inheritance | Many interface | Only one abstract class |
Default implementation | Only method signature | Can code complete method or method signature |
Access Modifiers | Everything public | Can have access modifiers |
Speed | Time taken for find actual method in corresponding class | Fast |
Code |
public interface IParent
{
// no implementation
// no modifiers public // all are public // no virtual String display();
}
|
public abstract class Parent
{
public abstract String display();
}
|
Partial Class
- A class defined in two or more files is called a partial class.
- The keyword partial is used to define the class.
- During compile time all the partial class are compiled into one type only.
- When working on large project.
public partial class Parent
{
public void DoWork(){}
}
public partial class Parent
{
public void Teach(){ }
}
Sealed Class
- cannot be inherited ( cannot be used as a base class)
- Restrict the inheritance
Comments
Post a Comment