Optimizing AI for immediate response in Smart CCTV
4 oo inheritance in c# (1)
1. ITDP 106 - Information Technology Essentials Elke Vogel-Adham 1
Inheritance
CIS 2403
Object Oriented Programming
2. ITDP 106 - Information Technology Essentials Elke Vogel-Adham 2
Association
Program Class instantiates in the Main methods
an object one from the Movie class
Until now …. Only Association
Progam
Main()
one
Name: Argo
length: 90
director: Lamont Johnson
Movie()
3. ITDP 106 - Information Technology Essentials Elke Vogel-Adham 3
Inheritance
Inheritance
= your object’s family tree
used to avoid duplicate
code
to save time in coding
to reduce chances of
errors
4. ITDP 106 - Information Technology Essentials Elke Vogel-Adham 4
Inheritance
Important terms:
– Base class: class used as the
basis for inheritance (Vehicle)
– Base class = superclass
= parent class
– Derived class: a class that
inherits form a base class
– Derived class = extended class
= subclass
= child class
– Ancestors = entire list of parent
classes
– Descendants = entire list of child
classes
5. ITDP 106 - Information Technology Essentials Elke Vogel-Adham 5
Inheritance
Use extends to inherit from a base class!
The arrow stands for inheritance!
Person
firstName: string
lastName: string
Person()
Display()
PartTime
room: string
PartTime()
Display()
Employee
empNumber: int
empName: string
empHours:double
Employee()
Pay()
PartTime
hourlyRate: int
PartTime()
Pay()
6. ITDP 106 - Information Technology Essentials Elke Vogel-Adham 6
Inheritance
Methods can be overriden by the subclass
use virtual (parent class) and override (child class)
Person
firstName: string
lastName: string
Person()
Pay()
Faculty
room: string
Faculty()
Pay()
Employee
empNumber: int
empName: string
empHours:double
Employee()
Pay()
PartTime
hourlyRate: int
PartTime()
Pay()
7. ITDP 106 - Information Technology Essentials Elke Vogel-Adham 7
Inheritance
Constructors
when the base class has a constructor, your subclass
needs one, too.
use super to connect to base class constructor
Employee
empNumber: int
empName: string
empHours:double
Employee()
Pay()
PartTime
hourlyRate: int
PartTime()
Pay()
8. ITDP 106 - Information Technology Essentials Elke Vogel-Adham 8
Inheritance
Access Modifier: protected
Can be accessed in the class and in the
sub class
Person
firstName: string
lastName: string
Person()
Display()
Faculty
room: string
Faculty()
Display()
Employee
empNumber: int
empName: string
empHours:double
Employee()
Pay()
PartTime
hourlyRate: int
PartTime()
Pay()
9. ITDP 106 - Information Technology Essentials Elke Vogel-Adham 9
Accessing the base class
The Super Keyword
Sometimes you want to use the parent
class method that is overriden by the
child class method
Use the keyword “super” for accessing
the base Class method
Example: base.Display()
Person
firstName: string
lastName: string
Person()
Display()
Faculty
room: string
Faculty()
Display()
Employee
empNumber: int
empName: string
empHours:double
Employee()
Pay()
PartTime
hourlyRate: int
PartTime()
Pay()
10. ITDP 106 - Information Technology Essentials Elke Vogel-Adham 10
Abstract Class
Abstract class
a class that is never instantiated
you must create a subclass that
inherits from the superclass
abstract classes have abstract
members that the derived class must
override to provide functionality
Person
firstName: string
lastName: string
Person()
Display()
Faculty
room: string
Faculty()
Display()
Employee
empNumber: int
empName: string
empHours:double
Employee()
Pay()
PartTime
hourlyRate: int
PartTime()
Pay()
11. ITDP 106 - Information Technology Essentials Elke Vogel-Adham 11
Abstract Class
Abstract class ExampleVehicle
Price: double
Year: int
Vehicle ()
showDetail()
Car
EngineSize: double
Car()
showDetail()