Abstract Class, Lecture Slide - Computer Science, Slides of C programming

Inheritance for polymorphism, Inheritance for code reuse, Abstract class, Abstract class:syntax, Interface, Interface syntax, Interfaces, Polymorphism via interfaces, An example

Typology: Slides

2010/2011

Uploaded on 10/05/2011

christina
christina 🇺🇸

4.6

(23)

393 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS 112 Introduction to
Programming
Lecture #33:
Abstract Class; Interface
http://flint.cs.yale.edu/cs112/
2
Recap: Inheritance for Code Reuse
rClasses are often organized as a class
hierarchy
min C#, all classes are derived from the Object
class
ToString:converts an object to a string representation
Equals: checks if two objects are the same
GetType:returns the type of a type of object
rAn inherited member is passed down the line
meach class can also override a method definition
to favor its own
3
Recap: Inheritance for Polymorphism
rAn object reference can refer to an object of its class, or to an
object of any class derived from it by inheritance
rAssigning an object to an ancestor reference is considered to be a
widening conversion, and can be performed by simple assignment
rAssigning 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
rThe widening conversion is used to implement polymorphism
mit 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
# name : string
# address : string
# phone : string
+ ToString() : string
+ Pay() : double
Volunteer
+ Pay() : double
Employee
# socialSecurityNumber: String
# payRate: double
+ ToString() : string
+ Pay() : double
Executive
-bonus : double
+ AwardBonus(execBonus : double) : void
+ Pay() : double
Hourly
-hoursWorked: int
+ AddHours(moreHours: int) : void
+ ToString() : string
+ Pay() : double
PayRoll
+ Main (args : string[]) : void
-staffList: staffMemeber[]
Staff
+ Payday() : void
-staffList: StaffMemeber[]
contains multiple objects
Polymorphic references and arrays work well with each other
5
Outline
rAdmin.
ØRefining class definition
Øabstract class
mother features
rInterface
6
Abstract Class
rMotivation: if you think no object of a class should be
created, declare the class abstract
rIn general, an abstract class is a placeholder in a class
hierarchy that represents a generic concept
mThe 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
pf3

Partial preview of the text

Download Abstract Class, Lecture Slide - Computer Science and more Slides C programming in PDF only on Docsity!

CS 112 Introduction to

Programming

Lecture #33:

Abstract Class; Interface

http://flint.cs.yale.edu/cs112/

2

Recap: Inheritance for Code Reuse

r Classes are often organized as a class

hierarchy

m in C#, all classes are derived from the Object

class

  • ToString: converts an object to a string representation
  • Equals: checks if two objects are the same
  • GetType: returns the type of a type of object

r An inherited member is passed down the line

m each class can also override a method definition

to favor its own

3

Recap: Inheritance for Polymorphism

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

name : string

address : string

phone : string

  • ToString() : string + Pay() : double

Volunteer

+ Pay() : double

Employee

socialSecurityNumber : String

payRate : double

  • ToString() : string + Pay() : double

Executive

  • bonus : double
  • AwardBonus(execBonus : double) : void + Pay() : double

Hourly

  • hoursWorked : int
  • AddHours(moreHours : int) : void
  • ToString() : string + Pay() : double

PayRoll

  • Main (args : string[]) : void
  • staffList: staffMemeber[]

Staff

  • Payday() : void

- staffList : StaffMemeber[]

contains multiple objects

Polymorphic references and arrays work well with each other

5

Outline

r Admin.

ÿ Refining class definition

ÿ abstract class

m other features

r Interface

6

Abstract Class

r Motivation: if you think no object of a class should be

created, declare the class abstract

r In general, an abstract class is a placeholder in a class

hierarchy that represents a generic concept

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

Abstract Classes: Syntax

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

Abstract Class: Some Properties

r An abstract class cannot be instantiated

m but you can define a reference of an abstract class

r Only abstract classes can contain abstract

methods

r The child of an abstract class must override

the abstract methods of the parent, or it too

must be declared abstract

See Shape.cs, Point.cs, Circle.cs, Cylinder.cs, AbstractShapeTest.cs

9

Outline

r Admin.

q Refining class definition

m abstract class

ÿ other features

q Interface

10

C# Contains Many Other Features to

Further Refine the Definition of a Class

r Sealed class or method

m if you declare a class or method sealed, then the

class or method cannot be extended

r Operator overloading

m you can define operators for a class so that

operations look more like a normal arithmetic

operation

11

Outline

r Admin.

q Refining class definition

ÿ Interface

12

Using Interface for Multiple Inheritance

r Java/C# decision: single inheritance ,

meaning that a derived class can have only

one parent class

r To take the advantages of multiple

inheritance, Java/C# defines interfaces,

which give us the best aspects of multiple

inheritance without the complexity