Download - C Sharp Jn (6)
Zeeshan Hanif
Software Development Training Program
Zeeshan Hanif
Zeeshan Hanif
Inheritance What is Inheritance Inherited Members new method virtual and override methods Polymorphism Abstract classes Sealed classes Object class Boxing and Unboxing Interfaces
Zeeshan Hanif
Inheritance
Inheritance is a powerful mechanism that allows a class to inherit functionality from an existing class.
For example, Cat, Dog, both are kinds of Mammal.
Car and Bus both are kinds of Vehicle
Zeeshan Hanif
is-a Relationship
The is-a relationship is one of specialization.
When we say, Dog is-a mammal, means dog is specialized kind of mammal
Mammal is generalization kind of Dog
Zeeshan Hanif
Car
Luxury cars
Sports Car
Zeeshan Hanif
Inheritance
In object-oriented terminology, sports cars and luxury cars are derivedclasses of car class. Similarly, the car class is the baseclass of luxury car and sports car.
Zeeshan Hanif
Example
public class Car {public int wheels;public string color;
}public class SportsCar : Car {
public int noOfSeats;}
Zeeshan Hanif
Inheritance Each derived class inherits all
variables from the base class. Sports cars and luxury cars both inherits four wheels, gears etc.
Also, each derived class inherits methods from the base class. Luxury cars and sports cars share some behaviors: braking and changing speed, for example.
Zeeshan Hanif
Inheritance
A However, derived classes are not limited to the state and behaviors provided to them by their base class.
Derived classes can add more variables and methods. Sports cars have two seats and, some sports cars have an extra set of gears.
Zeeshan Hanif
Inheritance What is Inheritance Inherited Members new method virtual and override methods Polymorphism Abstract classes Sealed classes Object class Boxing and Unboxing Interfaces