Basic Concepts of Generalization & Overriding? Example with C#

Generalization means multi classes can inherit same attributes from the same superclass.

“Generalization need to create program that can be customize in according with new requirements.”

Overriding

“The process were the subclass redefine the function of superclass is called overriding”

Example:

  1. Class Employee  
  2. {  
  3.     Leave  
  4. }  
  5.      Class Trainee  
  6. {  
  7.      Leave (function)  

In OOP the base class (parent class) is actually a “superclass “and the derived class (child class) is a “subclass”.

A class that inherits or drives attributes from base class (parent class) or another class is called the “derived class (child class).”

Syntax for create derived class

  1. <Access-specifies> class <base-class>  
  2. {  
  3.     Statement  
  4. }  
  5.   
  6. Class<derived-class> :< base-class>  
  7. {  
  8.     Statement  

  • Each instance of the derived class includes its own attributes and all the attributes of the base class.
  • Any change mode to the base class (super class) automatically changes the behavior of it subclass.
  • Constructors are called in the order of “base to derived class”.
  • Destructors are called in the order of “derived to base class”.

Example:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. namespace overriding  
  5. {  
  6.      class Base  
  7. {  
  8.      public Base() //assign construtor of base class  
  9. {  
  10.      Console.WriteLine("Constructor of Base Class:");  
  11. }  
  12.      ~Base() //assign destrutor of base class  
  13. {  
  14.      Console.WriteLine("Destructor of Base Class:");  
  15.      Console.ReadLine();  
  16. }  
  17.      class derived : Base  
  18. {  
  19.      public derived() //assign constructor of derived class  
  20.      {  
  21.          Console.WriteLine("Contructor of Dervived Class:");  
  22.      }  
  23.          ~derived() //assign destructor of derived class  
  24.      {  
  25.          Console.WriteLine("Destructor of Derived Class:");  
  26.          Console.ReadLine();  
  27.      }  
  28. }  
  29.      class Basederived  
  30.      {  
  31.          static void Main(string[] args)  
  32.      {  
  33.          derived dr = new derived();  
  34.          Console.ReadLine();  
  35.        }  
  36.    }  
  37.   }  



Ebook Download
View all
Learn
View all