Inheritance: Dept. of Computer Science Faculty of Science and Technology
Inheritance: Dept. of Computer Science Faculty of Science and Technology
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
SuperClass
+ Functionality() : void
Client us e s Server
Inhe rits ( “is a” te s t )
+ Functionality() : void
SubClass
§ composition is dynamic
class Client
§ inheritance is static {
// here we instanciate the ‘Server’ class
class SubClass : SuperClass Server s = ...
public void MethodUsingServer()
{ ... } {
// and use its instance methods from within the ‘Client’ class
// thus ‘composing’ the client without inheritance from any ‘base’
s.Functionality();
}
}
Inhe ritanc e
§ Keywords relevant to inheritance +
polymorphism
§ a b s t ra c t – class or method is not concrete; sub-classes
are expected to provide the implementation
§ v irt u a l – indicates a method can be overridden in the
sub-class
§ o v e rrid e – override an implementation in the super-
class; only abstract and virtual methods can be
overridden
§ n e w – note the overloaded usage... indicates a
method is a new implementation that should “hide” the
implementation in the super-class
What is Inheritance?
A relationship between a more general class (called the
superclass or base class) and a more specialised
class(called the subclass or derived class).
For example, a Cat is a specific type of Animal. Therefore
the Animal class could be the superclass/base class, and
the Cat class could be a subclass of Animal.
Why is Inheritance important?
Inheritance is a fundamental concept in Object-Oriented
programming.
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