Professional Documents
Culture Documents
Chapter 10
Inheritance and
Polymorphism
Topics
• 10.1 Inheritance
• 10.2 Polymorphism
• 10.3 Abstract Classes
– where Car is the derived class and Automobile is the base class
– the colon (:) indicates that this class is derived from another class
// base // derived
class Rectangle class Box : Rectangle
{ {
…. private int _height;
// parameterless constructor // parameter constructor
public Rectangle() { …. } public Box() { …. }
– This method has an Animal variable as its parameter. You can pass an
Animal object to the method
Animal myAnimal = new Animal(“Regular animal”);
ShowAnimalInfo(myAnimal);
– The method can display the object’s Species property and calls its
MakeSound method
• Due to polymorphism, you can also pass a Dog object as argument to
the ShowAnimalInfo method
Dog myDog = new Dog(“Fido”);
ShowAnimalInfo(myAnimal);