

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Inheritance for polymorphism, Inheritance for code reuse, Abstract class, Abstract class:syntax, Interface, Interface syntax, Interfaces, Polymorphism via interfaces, An example
Typology: Slides
1 / 3
This page cannot be seen from the preview
Don't miss anything!


2
3
r An object reference can refer to an object of its class, or to an object of any class derived from it by inheritance r Assigning an object to an ancestor reference is considered to be a widening conversion, and can be performed by simple assignment
r Assigning an ancestor object to a reference can also be done, but it is considered to be a narrowing conversion and must be done with a cast
r The widening conversion is used to implement polymorphism m it is the type of the object being referenced, not the reference type, that determines which method is invoked
Holiday day = new Christmas();
Christmas christ = new Christmas(); Holiday day = christ; Christmas christ2 = (Christmas)day;
4
StaffMember
Volunteer
+ Pay() : double
Employee
Executive
Hourly
PayRoll
Staff
- staffList : StaffMemeber[]
contains multiple objects
Polymorphic references and arrays work well with each other
5
6
m The use of abstract classes is a design decision; it helps us establish common elements in a class that is too general to instantiate
Vehicle
Car Boat Plane
7
r Use the modifier abstract on a class header to declare an abstract class, e.g.,
abstract class Vehicle { // … public abstract void Move(); } class Car : Vehicle { //… public void Move() { Console.WriteLine( “Car is moving!” ); } } abstract class StaffMember { // … public abstract double Pay(); } 8
See Shape.cs, Point.cs, Circle.cs, Cylinder.cs, AbstractShapeTest.cs
9
10
11
12