



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
This document from the cs112 course at yale university explores the concept of object-oriented programming (oop) and inheritance in c#. It explains how classes serve as program modules and blueprints for generating objects, the role of inheritance in code reuse, and the meaning of 'is-a' relationships. The document also covers the use of protected visibility modifiers and calling a parent's constructor in a child's constructor.
Typology: Slides
1 / 5
This page cannot be seen from the preview
Don't miss anything!




http://flint.cs.yale.edu/cs112/
2
r C# classes play dual roles: m program modules: contain a list of (static) method declarations and (static) data fields m blueprints for generating objects:
r Classes support m “data encapsulation” (e.g., private components, for abstraction) m “inheritance” (for code reuse)
3
r Inheritance allows a software developer to derive a new class from an existing one
r The existing class is called the parent class, or superclass , or base class
r The derived class is called the child class, or subclass, or derived class.
r As the name implies, the child inherits characteristics of the parent
r That is, the child class inherits the methods and data defined for the parent class 4
Inheritance should Inheritance should create ancreate an isis--aa relationshiprelationship ,, meaning the childmeaning the child is ais a more specificmore specific version of theversion of the parentparent
Animal
Bird
Animal
Bird
5
Base class Derived classes Student GraduateStudent UndergraduateStudent
Shape Circle Triangle Rectangle Loan CarLoan HomeImprovementLoan MortgageLoan Employee FacultyMember StaffMember Account CheckingAccount SavingsAccount Fig. 9.1 Inheritance examples.
6
r Define a new class DerivedClass which extends BaseClass
class BaseClass { // class contents } class DerivedClass : BaseClass { // class contents }
r Base class: the “parent” class; if omitted the parent is Object
See Book.cs, Dictionary.cs, BookInheritance.cs
7
r A child class inherits the methods and data defined for the parent class; however, whether a data or method member of a parent class is accessible in the child class depends on the visibility modifier of a member r Variables and methods declared with private visibility are not accessible in the child class m a private data member defined in the parent class is still part of the state of a derived class r Variables and methods declared with public visibility are accessible; but public variables violate our goal of encapsulation r There is a third visibility modifier that helps in inheritance situations: protected
8
q Variables and methods declared with protected visibility in a parent class are only accessible by a child class or any class derived from that class
q The details of each modifier are linked on the schedule page
r Example m Book.cs m Dictionary.cs m TestBookDictionary.cs
Book # pages : int
Dictionary
9
r Constructors are not inherited, even though they have public visibility r The first thing a derived class does is to call its base class’ constructor, either explicitly or implicitly m implicitly it is the default constructor m yet we often want to use a specific constructor of the parent to set up the "parent's part" of the object r Syntax: use base public DerivedClass : BaseClass { public DerivedClass(…) : base(…) { // … } } rExample mSee Book.cs mSee Dictionary.cs mSee BookConstructor.cs
rExample mSee FoodItem.cs mSee Pizza.cs mSee FoodAnalysis.cs (^10)
11
rExample mThought.cs mAdvice.cs mThoughtAndAdvice.cs
12
r Overloading deals with multiple methods in the same class with the same name but different signatures
r Overloading lets you define a similar operation in different ways for different data
r Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature
r Overriding lets you define a similar operation in different ways for different object types
19
Holiday day; day = new Holiday(); … day = new Christmas();
Holiday
Christmas
20
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 the most useful m for implementing polymorphism
Holiday day = new Christmas();
Christmas christ = new Christmas(); Holiday day = christ; Christmas christ2 = (Christmas)day;
21
r A polymorphic reference is one which can refer to different types of objects at different times
r An object reference can refer to one object at one time, then it can be changed to refer to another object (related by inheritance) at another time m it is the type of the object being referenced, not the reference type, that determines which method is invoked m polymorphic references are therefore resolved at run-time, not during compilation; this is called dynamic binding
r Careful use of polymorphic references can lead to elegant, robust software designs
22
23
m Shape.cs m Circle.cs m Sphere.cs m Cylinder.cs
24
StaffMember
Volunteer
+ Pay() : double
Employee
Executive
Hourly
25
Polymorphism via Inheritance and
Arrays of Objects
m StaffMember.cs m Volunteer.cs
m Employee.cs m Executive.cs m Hourly.cs m Staff.cs m PayRoll.cs