OOP2-Lecture-Week-04
OOP2-Lecture-Week-04
1. Inheritance
2. HAS-A relationship
3. IS-A relationship
4. Single
5. Multilevel
6. Hierarchical Inheritance
Inheritance
Inheritance is ability for derived class to inherit all the members of the base class
Derived class also can be referred to as “child class” or “subclass”
Base class also can be referred to as “parent class” or “superclass”
Syntax to specify inheritance
class SubClass : SuperClass
Vehicle
Motorbike Car
Saloon MPV
Truck
IS-A Relationship
Use the IS-A test to verify that your inheritance hierarchy is valid.
A Dog IS-A Animal – makes sense, therefore the Dog class can inherit from
an Animal class.
A Door IS-A Car– doesn’t make sense, so the Door class shouldn’t inherit
from the Car class.
An Animal IS-A Dog – doesn’t make sense. A Dog IS-A Animal, but an Animal
is not a Dog.
The IS-A test only works in one direction i.e. a Cat IS-A Animal, but an
Animal is not a Cat.
HAS-A Relationships
• Use the HAS-A test to verify whether an object should
contain another object as an instance variable. This is called
object composition.
Output:
Hierarchical Inheritance
In this type of inheritance, the multiple classes derives from one base
class. It's like having multiple kids, all inheriting traits from parent, but
in their own different ways.
Output:
Multilevel Inheritance
• C# 4.0 The Complete Reference; Herbert Schildt; McGraw-Hill Osborne Media; 2010
• Head First C# by Andrew Stellman
• Fundamentals of Computer Programming with CSharp – Nakov v2013
References
C# 4.0 The Complete Reference; Herbert Schildt; McGraw-Hill Osborne Media; 2010