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:

Real world example: TV Remote

Abstraction  

Process of hiding the details of implementation and display the essential feature (describe object, by define their unique & relevant characteristic)

Real world example: TV 

Encapsulation vs Abstraction


Encapsulation Abstraction
Solves the problem atImplementation levelDesign level
HideCode and data in class to protectUnwanted 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. 
     Why???
  • It's only allow to other class to inherit and cannot be instantiated. 
    Usage
  • 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. 
      Usage
  • C# doesn't support multiple inheritance so interfaces are used to implement multiple inheritance.
FeatureInterfaceAbstract class
InheritanceMany interfaceOnly one abstract class
Default implementationOnly method signatureCan code complete method or method signature
Access ModifiersEverything publicCan have access modifiers
SpeedTime taken for find actual method in corresponding classFast
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.
      Usage
  • 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)
      Usage
  • Restrict the inheritance
public sealed class Parent { // Class members here. }

Comments

Popular posts from this blog

Basic concepts of Blockchain

Gulp Part 01

Hello world Application