0% found this document useful (0 votes)
7 views

Programming 2 Week 2

Uploaded by

Melissa Lemus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Programming 2 Week 2

Uploaded by

Melissa Lemus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Programming 2 (C#)

Week 2

1
Program term 1.2 (Programming 2)
01 (wk 46) recap classes, SoC / SRP
02 (wk 47) inheritance, virtual/override/abstract
03 (wk 48) polymorphism, interfaces
04 (wk 49) lists, dictionaries
05 (wk 50) error/exception handling, nullable
06 (wk 51) file I/O
07 (wk 52) Christmas holiday
08 (wk 01) Christmas holiday
09 (wk 02) repetition / practice exam
10 (wk 03) exams
11 (wk 04) exams
12 (wk 05) exams

2 Programmeren 2 - week 2 (24/25)


Inheritance

3 Programmeren 2 - week 2 (24/25)


Inheritance
‒ Define new classes ‘on top of’ existing classes to expand them and/or modify behavior

‒ Reuse of existing code + adding new functionalities (methods) + changing behavior (methods)

‒ Derived class is derived from base class

‒ Derived class inherits all members of the base class

4 Programmeren 2 - week 2 (24/25)


Example of inheritance
‒ Employee inherits from Person
(Employee 'is a' Person)

‒ Manager inherits from Employee


(Manager 'is an' Employee,
and therefore, also a Person)

5 Programmeren 2 - week 2 (24/25)


Example of inheritance
‒ Employee inherits from Person
(Employee 'is a' Person)

‒ Manager inherits from Employee


(Manager 'is an' Employee,
and therefore, also a Person)

6 Programmeren 2 - week 2 (24/25)


Inheritance
‒ Employee is a derived class from the base class Person

‒ Manager is a derived class from the base class Employee

7 Programmeren 2 - week 2 (24/25)


Demo Code
‒ Person/Employee Inheritance
‒ In demo code repo on GitHub: employee_inheritance project

8 Programmeren 2 - week 2 (24/25)


Class Exercise
‒ Create a Vehicle class with fields for fuel capacity
and top speed
‒ Create a Truck class that inherits from Vehicle and
adds a field for cargo capacity
‒ You can use the example to the right as a
reference

9 Programmeren 2 - week 2 (24/25)


Solution Demo Code
‒ Vehicle/Truck Inheritance
‒ In demo code repo on GitHub: vehicle_inheritance project

10 Programmeren 2 - week 2 (24/25)


calling base constructor

11 Programmeren 2 - week 2 (24/25)


Constructors
‒ You can call a constructor of the base class explicitly from a constructor of the derived class, in
order to forward (pass) data

‒ We need to use ' : base(…) '

12 Programmeren 2 - week 2 (24/25)


Constructors
‒ Initializing fields of the
base class (first name, …)
is done by the constructor of
the base class (Person)

‒ Initializing fields of the


derived class (department, …)
is done by the constructor of
the derived class (Employee)

13 Programmeren 2 - week 2 (24/25)


Constructors
‒ Initializing fields of the
base class (first name, …)
is done by the constructor of
the base class (Person)

‒ Initializing fields of the


derived class (department, …)
is done by the constructor of
the derived class (Employee)

14 Programmeren 2 - week 2 (24/25)


Demo Code
‒ Use of constructor in derived class
‒ In demo code repo on GitHub: employee_inheritance_constructors project

15 Programmeren 2 - week 2 (24/25)


Class exercise
‒ Update the Vehicle and Truck
classes to use constructors
‒ Use the code on the right as
an example
‒ Bonus: Change the fields
accessibility from public to
protected

16 Programmeren 2 - week 2 (24/25)


Solution Demo Code
‒ Vehicle/Truck Constructors
‒ In demo code repo on GitHub: vehicle_inheritance_constructors project

17 Programmeren 2 - week 2 (24/25)


Adding functionalities

18 Programmeren 2 - week 2 (24/25)


Adding methods
‒ We can add extra functionality/behavior to derived classes
(the derived class has all functionalities of the base class, and complemented with more)

19 Programmeren 2 - week 2 (24/25)


Adding methods

20 Programmeren 2 - week 2 (24/25)


Adding methods
‒ Both manager and
employee have an
‘IncreaseSalary’ method,
but only the manager can
fire an employee

21 Programmeren 2 - week 2 (24/25)


Changing behaviour
(virtual / override)

22 Programmeren 2 - week 2 (24/25)


Changing behavior
‒ A derived class can 'overwrite' a method of the base class. This means that a derived class can
implement an existing functionality differently (change behavior)

‒ We need to use keywords 'virtual' and 'override'

23 Programmeren 2 - week 2 (24/25)


Changing behavior

24 Programmeren 2 - week 2 (24/25)


Changing behavior

‒ A manager receives a
salary increase of 1,500 euros,
not 1,000 euros...

25 Programmeren 2 - week 2 (24/25)


Demo Code
‒ Use of virtual/override for method inheritance
‒ In demo code repo on GitHub: employee_inheritance_constructors project
→Employee class
→Manager class

26 Programmeren 2 - week 2 (24/25)


abstract

27 Programmeren 2 - week 2 (24/25)


Abstract classes
‒ Abstract classes are always the base for other classes
(base classes can not be instantiated: no objects can be made)

‒ Abstract classes contain partial implementation


(data and/or methods)

‒ Abstract classes can have one or more methods without body; derived classes must implement
these (abstract) methods

28 Programmeren 2 - week 2 (24/25)


Abstract classes
‒ Class ‘LibraryItem’ is abstract; no
objects can be made from it

‒ This abstract class contains a few


properties and methods; derived
classes won’t have to implement
them (they ‘inherit’ these members)

29 Programmeren 2 - week 2 (24/25)


Abstract classes
‒ ‘new LibraryItem (…)’ will give a
compile-error

30 Programmeren 2 - week 2 (24/25)


Abstract classes
‒ Deriving is done the same way as
with ‘normal’ (non-abstract) classes:
Book : LibraryItem

‒ In the constructor we immediately


pass (base) information to the base
class with: base (...)

31 Programmeren 2 - week 2 (24/25)


Abstract methods
‒ Class ‘Figure’ is abstract; no objects can be
created with: new Figure(...)

‒ A derived class must implement method


‘Draw’, since this method is abstract

‒ If an abstract method is not implemented, then a


compile-error occurs:
‘<class-name> does not implement inherited
abstract member <method-name>’

32 Programmeren 2 - week 2 (24/25)


Abstract methods
‒ Class ‘Square’ derives from abstract class
‘Figure’

‒ Class ‘Square’ implements abstract method


‘Draw’. Keyword ‘override’ is needed here

33 Programmeren 2 - week 2 (24/25)


Demo Code
‒ Abstract class
‒ In demo code repo on GitHub: abstract_shape

34 Programmeren 2 - week 2 (24/25)


Demo Code Repo
https://github.com/danpaul/programming_2_demo

35 Programmeren 2 - week 2 (24/25)


Homework
‒ (practical class) Programming 2
- week 2 assignments → Moodle

36 Programmeren 2 - week 2 (24/25)

You might also like