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:
- Class Employee
- {
- Leave
- }
- Class Trainee
- {
- 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
- <Access-specifies> class <base-class>
- {
- Statement
- }
-
- Class<derived-class> :< base-class>
- {
- 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:
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace overriding
- {
- class Base
- {
- public Base()
- {
- Console.WriteLine("Constructor of Base Class:");
- }
- ~Base()
- {
- Console.WriteLine("Destructor of Base Class:");
- Console.ReadLine();
- }
- class derived : Base
- {
- public derived()
- {
- Console.WriteLine("Contructor of Dervived Class:");
- }
- ~derived()
- {
- Console.WriteLine("Destructor of Derived Class:");
- Console.ReadLine();
- }
- }
- class Basederived
- {
- static void Main(string[] args)
- {
- derived dr = new derived();
- Console.ReadLine();
- }
- }
- }
- }