308203A Introduction to Computing II Lecture 3: Interfaces and Inheritance - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

308203A Introduction to Computing II Lecture 3: Interfaces and Inheritance

Description:

final - prevents a method from being overriden. abstract - indicates that a method is undefined and. must be overriden in the subclass ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 16
Provided by: alfre6
Category:

less

Transcript and Presenter's Notes

Title: 308203A Introduction to Computing II Lecture 3: Interfaces and Inheritance


1
308-203AIntroduction to Computing IILecture 3
Interfaces and Inheritance
Fall Session 2000
2
Interfaces
  • More than one class may perform the same service
  • e.g. Planes, trains and automobiles
  • Interfaces describe what classes can do

3
Example
interface Vehicle float costOfTrip(
) ...
4
Example
class Car implements Vehicle float
costOfTrip( )
interface Vehicle float costOfTrip(
) ...
class Train implements Vehicle float
costOfTrip( )
5
Example
Vehicle chooseLeastExpensive(Vehicle
choices) for (int j 0 j lt
choices.length j) if (choicesj.costOfTrip(
) lt best) bestVehicle choicesj best
choicesj.costOfTrip( )
6
Whats the point
  • We can manipulate objects without knowing
  • their exact class
  • Code we write will work, even with new
  • kinds of objects
  • Weve abstracted a kind of behavior

7
Polymorphism
Definition Polymorphism is the ability of a
method call to invoke an underlying method
apropriate to the type of the object.
8
Any questions?
9
Interface shortcomings
  • What if two classes share not only behavior
  • but data? E.g. all vehicles have
    wheels
  • What if two classes have methods which are
  • implemented in the same way?
  • What if we want to change the behavior of a
  • class slightly?

10
Inheritance
  • Make new classes based on a previous class
  • Behavior remains the same unless overriden
  • Code can be modified incrementally, without
  • restarting from scratch for each class
  • Code can be shared

11
Terminology
Superclass - a class on which another class is
based Subclass - a class derived from another
class Override - the replacement of a method
inherited from a superclass by
some new method
12
Related Java Keywords
  • extends - what class do we build on
  • this - lets an object refer to itself
  • super - lets an object refer to methods
  • as defined in its superclass
  • final - prevents a method from being overriden
  • abstract - indicates that a method is undefined
    and
  • must be overriden in the
    subclass

13
Public/private/protected
Keywords to restrict access to methods/data
  • Public - visible by any other class
  • Private - visible only by this class (not
    subclasses!)
  • Protected - visible to this class and subclasses

14
A Simple Example
public class Workaholic extends Worker
public doWork( ) super.doWork(
) drinkCoffee( ) super.doWork( )
15
Any questions?
Write a Comment
User Comments (0)
About PowerShow.com