Professional Documents
Culture Documents
CSE459 CSharp 03 Inheritance
CSE459 CSharp 03 Inheritance
C# Classes
Classes
Modularity:
Classes
support
Inheritance
Inheritance
Inheritance
relationships
are often shown
graphically in a class
diagram, with the arrow
pointing to the parent
class.
Inheritance should
create an is-a
relationship, meaning the
child is a more specific
version of the parent.
Animal
Bird
Controlling Inheritance
Book
# pages : int
+ GetNumberOfPages() : void
Dictionary
- definition : int
+ PrintDefinitionMessage() : void
+ public
- private
# protected
Single Inheritance
Some
Overriding Methods
A child
Class Hierarchies
A child
Snake
Lizard
Bird
Parrot
Mammal
Horse
Bat
Class Hierarchies
CommunityMember
Employee
Faculty
Professor
Student
Staff
Instructor
Under
Alumnus
Graduate
Class Hierarchies
Shape
TwoDimensionalShape
Circle
Square
Triangle
ThreeDimensionalShape
Sphere
Cube
Cylinder
Class Hierarchies
An
Good
is transitive.
Dynamic Binding
A polymorphic
Dynamic Binding
Suppose
Overriding Methods
C#
Overriding Methods
If
deals with
multiple methods in the
same class with the
same name but different
signatures
Overloading lets you
define a similar
operation in different
ways for different data
Example:
int foo(string[] bar);
int foo(int bar1, float a);
Overriding
Employee
Volunteer
# socialSecurityNumber : String
# payRate : double
+ ToString() : string
+ Pay() : double
+ Pay() : double
Executive
- bonus : double
+ AwardBonus(execBonus : double) : void
+ Pay() : double
Hourly
- hoursWorked : int
+ AddHours(moreHours : int) : void
+ ToString() : string
+ Pay() : double
Assigning
in polymorphism.
Note:
Type Unification
Everything
String class overrides the Equals method to compare the content of two
strings