04 - Inheritance
04 - Inheritance
Table of Contents
• Inheritance
• Class Hierarchies
• Inheritance in C#
• Accessing Members of the Base Class
• Reusing Classes
• Type of Class Reuse
2
ANIMAL
Extending Classes
Inheritance
Inheritance
Superclass - Parent class, Base Class
The class giving its members to its child class
Subclass - Child class, Derived class
The class taking members from its base class
Base
Superclass
Derived
Subclass
4
Inheritance – Example
Base class
Person
+Name: string
+Address: string
Employee Student
+Company: string +School: string
5
Class Hierarchies
Class Hierarchies
Inheritance leads to hierarchies of classes and/or interfaces in
an application Base class holds
common
Game characteristics
SinglePlayerGame MultiplePlayers-
Game
Student Employee
Student : Person
8
Inheritance – Derived Class
Derived classes take all members from base classes
Person
Mother : Person
Reusing Person
Father : Person
11
Thinking about Inheritance – Extends
Derived class instance contains instance of its base class
Person Employee
(Base Class) (Derived Class)
+Sleep():void +Work():void
+Study():void
12
Transitive Relation
Inheritance has a transitive relation
class Person { … }
class Student : Person { … }
class CollegeStudent : Student { … }
Person
Student
CollegeStudent
13
Multiple Inheritance
In C# there is no multiple inheritance
Only multiple interfaces can be implemented
Person Student
CollegeStudent
14
Accessing Base Class Members
Access to Base Class Members
Use the base keyword
class Person { … }
class Employee : Person {
public void Fire(string reasons)
{
Console.Writeline($"{base.name} got fired
because of
{reasons}");
}
}
16
Problem: Single Inheritance
Animal
+Eat():void
Dog dog = new Dog();
dog.Eat();
dog.Bark();
Dog
+Bark():void
17
Problem: Multiple Inheritance
Animal
+Eat():void
Puppy puppy = new Puppy();
Dog puppy.Eat();
puppy.Bark();
+Bark():voi
d puppy.Weep();
Puppy
+Weep():voi
d
18
Problem: Hierarchical Inheritance
19
Reusing Code at Class Level
Reusing Classes
Inheritance and Access Modifiers
Derived classes can access all public and protected
members
Internal members are accessed in the same assembly
Private fields are not inherited in subclasses
class Person {
private string id;
string name;
protected string address;
public void Sleep(); }
21
Shadowing Variables
Derived classes can hide superclass variables
class Person { protected int
weight; }
class Patient : Person {
protected float weight;
Hides int weight
public void Method()
{
double weight = 0.5d;
} Hides float weight
}
22
Shadowing Variables – Access
Use base and this to specify member access
class Patient : Person {
protected float weight; Local variable
public void Method() {
double weight = 0.5d;
this.weight = 0.6f;
Instance member
Base class member base.weight = 1;
}
}
23
Virtual Methods
virtual - defines a method that can be overriden
25
Inheritance Benefits – Extension
We can extend a class that we can't otherwise change
Collections
List
Extends
CustomList
26
Problem: Random List
Create an list that has
All functionality of a List<string>
Method that returns and removes a random element
Collections
List<string>
+RandomElement():string
RandomList
27
Solution: Random List
List<string>
CustomList
30
Composition
Using classes to define classes
31
Delegation
class Laptop
{
Monitor monitor;
Laptop
void IncrBrightness()
monitor.Brighten(); Monitor
32
Problem: Stack of Strings
Create a simple StackOfStrings class which inherits the
Stack<string>
StackOfStrings
+IsEmpty(): Boolean
+AddRange(): void
33
Solution: Stack of Strings
Inheritance
… is a powerful tool
for…code reuse
Subclass
… inherits members from
Superclass and can override methods
Look for classes with the same role
Look for IS-A and IS-A-SUBSTITUTE
Consider Composition and Delegation
35
Exercises
• Time to practices