CS100A Lecture 22 PowerPoint PPT Presentation

presentation player overlay
About This Presentation
Transcript and Presenter's Notes

Title: CS100A Lecture 22


1
CS100A Lecture 22
  • Previous Lecture
  • MatLab demonstration
  • This Lecture
  • Inheritance
  • Method overriding
  • Polymorphism
  • Reading Lewis and Loftus, Chapter 8

2
Object-Oriented Programming
  • Object-oriented programming aspires to model the
    real world as a collection of objects.
  • Objects are classified into categories called
    classes.
  • Objects in the real world and in mathematics can
    be further classified by hierarchical taxonomies.
  • For the taxonomy
  • we say every Y is-a X. For example,
  • every kitten is a cat.

X
Y
Cat
Kitten
3
Taxonomy of Polygons

4
Two Taxonomies of Person
Person
Female
Male
boy
man
girl
woman
Person
Old
Young
boy
girl
man
woman
5
Not a Taxonomy
Person
Female
Male
Old
Young
boy
man
girl
woman
  • Taxonomies are strictly hierarchical

6
The Class Hierarchy
  • In Java, all classes are organized into a
    taxonomy known as the class hierarchy, Class
    Object is at the top of the hierarchy.
  • If class s is below class c in the class
    hierarchy, class s is called a subclass of c, a c
    is a superclass of s.
  • The Java class definition
  • class class-name
  • . . .
  • implicitly defines class-name to be a subclass
    of class Object.
  • Example. Classes Account, Room, Person, and
    Matrix are each subclasses of class Object

Object
Person
Room
Matrix
Account
7
Defining a Subclass
  • The Java class definition
  • class class-name1 extends class-name2
  • . . .
  • explicitly defines class-name1 to be a subclass
    of class-name2.
  • Example.
  • class Male extends Person
  • . . .
  • class Female extends Person
  • . . .

Object
Room
Matrix
Person
Account
Female
Male
8
Inheritance
  • Objects of a given class have all characteristics
    (fields and methods) of objects above them in the
    hierarchy.
  • A subclass is said to inherit the fields and
    methods of its superclass.
  • The class hierarchy is sometime called the
    inheritance hierarchy.

9
Method Resolution
  • Let o be an object of type t, i.e., o was
    contructed by a constructor t.
  • Question. Suppose you invoke method m on object
    o. Which definition of m is invoked?
  • Answer. The first definition of m found (at run
    time) in the class hierarchy, starting at t,
    working up through its superclasses, to Object.

10
Field Selection
  • Let e be a reference expression of type t, where
    t is some class. i.e., at run time expression e
    will evaluate to a reference to some object of
    type t, where t is t or a subtype of t.
  • Question. Suppose you select field f on whatever
    object e refers to . Which field f is invoked?
  • Answer. The first definition of f found (at
    compile time) in the class hierarchy, starting at
    t, working up through its superclasses, to
    Object. Note that t and not t is used to resolve
    the field reference.

11
Inheritance of Fields
  • class Account
  • int balance 0 // current balance
  • . . .
  • class SavingsAccount extends Account
  • double rate 0 // interest rate
  • . . .
  • // Client code
  • Account act1 new Account()
  • SavingsAccount act2 new SavingsAccount()
  • // A SavingsAccount has both balance and rate.
  • System.out.println(act2.balance) // LEGAL!
  • System.out.println(act2.rate)

12
Inheritance of Methods
  • class Room
  • int id // Id number of room
  • . . .
  • public String toString()
  • return Room id
  • class Bathroom extends Room
  • boolean shower // true if room has shower.
  • . . .
  • // Client code
  • Room r1 new Room()
  • Bathroom r2 new Bathroom()
  • / Rooms toString method is available for both
    Rooms and Bathrooms. /

13
Method Overriding
  • Recall that if you dont define a toString
    methods in a class, a default toString method
    is used.
  • Where does that default method come from? It is
    the toString method of class Object.
  • Redefining a method that is already defined in a
    superclass is called method overriding (not to be
    confused with method overloading).
  • Method overriding lets you have a method that is
    specialized for a subclass.

Object
Room
Bathroom
14
Types of Variables
  • Recall that the type of a variable determines the
    types of values that can be stored in the
    variable.
  • A type t variable can contain type t objects.
  • Room r new Room() // LEGAL!
  • Account act new Acount() // LEGAL!
  • A type t variable may not contain objects of type
    t if t and t are unrelated types.
  • // Room r new Account() NOT LEGAL!
  • // Account act new Room() NOT LEGAL!

15
Polymorphism
  • A type t variable can contain any object whose
    type is t or a subtype of t
  • // Client Code.
  • / Because every Bathroom is a Room, both of the
    following statements are legal. /
  • Room r1 new Room() // LEGAL!
  • Room r2 new Bathroom() // LEGAL!
  • / You can only access fields that are guaranteed
    to exist based on the type of the object
    reference. /
  • // System.out.println(r2.shower) NOT LEGAL!
  • / Because not every Room is a Bathroom, only the
    second of the following statements is legal. /
  • // Bathroom r3 new Room() NOT LEGAL!
  • Bathroom r4 new Bathroom() // LEGAL!

16
Polymorphism, continued
  • class Room
  • int id // Id number of room
  • . . .
  • public String toString()
  • return Room id
  • class Bathroom extends Room
  • . . .
  • public String toString()
  • return Bathroom id
  • / Client code. The class of the object, not the
    type of the variable, determines which method is
    invoked. /
  • Room r1 new Room()
  • System.out.println(r1) // output Room
Write a Comment
User Comments (0)
About PowerShow.com