Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS100A Lecture 22 Previous Lecture This Lecture Inheritance

Similar presentations


Presentation on theme: "CS100A Lecture 22 Previous Lecture This Lecture Inheritance"— Presentation transcript:

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

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 CS 100 Lecture 22

3 Taxonomy of Polygons Polygon Quadrilateral Triangle Trapezoid
Isosceles Parallelogram Equilateral Rhombus Square CS 100 Lecture 22

4 Two Taxonomies of Person
Male Female boy man girl woman Person Young Old boy girl man woman CS 100 Lecture 22

5 Not a Taxonomy Person Young Old Male Female boy man girl woman
Taxonomies are strictly hierarchical CS 100 Lecture 22

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 Account Room Person Matrix CS 100 Lecture 22

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 Account Room Person Matrix Male Female CS 100 Lecture 22

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. CS 100 Lecture 22

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. CS 100 Lecture 22

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. CS 100 Lecture 22

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); // An account has only a balance. System.out.println(act1.balance); // System.out.println(act1.rate); NOT LEGAL! CS 100 Lecture 22

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(); /* Room’s toString method is available for both Rooms and Bathrooms. */ System.out.println(r1); System.out.println(r2); CS 100 Lecture 22

13 Method Overriding Recall that if you don’t 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 CS 100 Lecture 22

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! CS 100 Lecture 22

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! CS 100 Lecture 22

16 Polymorphism, continued
class Room { int id; // Id number of room . . . public String toString() { return ”Room: ” + id; } } class Bathroom extends Room { 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: …” Room r2 = new Bathroom(); System.out.println(r2); // output: “Bathroom: …” CS 100 Lecture 22


Download ppt "CS100A Lecture 22 Previous Lecture This Lecture Inheritance"

Similar presentations


Ads by Google