Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 100Lecture 241 CS100J Lecture 24 n Previous Lecture –MatLab demonstration n This Lecture –Inheritance –Method overriding –Polymorphism –Reading: n Lewis.

Similar presentations


Presentation on theme: "CS 100Lecture 241 CS100J Lecture 24 n Previous Lecture –MatLab demonstration n This Lecture –Inheritance –Method overriding –Polymorphism –Reading: n Lewis."— Presentation transcript:

1 CS 100Lecture 241 CS100J Lecture 24 n Previous Lecture –MatLab demonstration n This Lecture –Inheritance –Method overriding –Polymorphism –Reading: n Lewis and Loftus, Chapter 7 n Savitch, Chapter 7

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

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

4 CS 100Lecture 244 Two Taxonomies of Person Person boy girl OldYoung man woman Person boy man FemaleMale girl woman

5 CS 100Lecture 245 Not a Taxonomy Person boy man FemaleMale girl woman OldYoung n Taxonomies are strictly hierarchical

6 CS 100Lecture 246 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. In Java, all classes are organized into a taxonomy known as the class hierarchy, Class Object is at the top of the hierarchy. n 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. n 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 Example. Classes Account, Room, Person, and Matrix are each subclasses of class Object Person Object RoomMatrixAccount

7 CS 100Lecture 247 Defining a Subclass n The Java class definition class class-name 1 extends class-name 2 {... } explicitly defines class-name 1 to be a subclass of class-name 2. n Example. class Male extends Person {... } class Female extends Person {... } Person Object RoomMatrix Female Male Account

8 CS 100Lecture 248 Inheritance n Objects of a given class have all characteristics (fields and methods) of objects above them in the hierarchy. n A subclass is said to inherit the fields and methods of its superclass. n The class hierarchy is sometime called the inheritance hierarchy.

9 CS 100Lecture 249 Method Resolution n Let o be an object of type t, i.e., o was constructed by a constructor t. n 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. 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 CS 100Lecture 2410 Inheritance of Methods class Room { int id;// Id number of room...... public String toString() { return ”Room: ” + id; } { 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);

11 CS 100Lecture 2411 Method Overriding Recall that if you don’t define a toString methods in a class, a “default” toString method is used. 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. Where does that default method come from? It is the toString method of class Object. n Redefining a method that is already defined in a superclass is called method overriding (not to be confused with method overloading). n Method overriding lets you have a method that is specialized for a subclass. Room Object Bathroom

12 CS 100Lecture 2412 Field Selection n 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. n Question. Suppose you select field f of whatever object e refers to. Which field f is accessed? Answer. The first definition of f found (at compile time) in the class hierarchy, starting at t, working up through its superclasses, to Object. Answer. The first definition of f found (at compile time) in the class hierarchy, starting at t, working up through its superclasses, to Object. n Important point to be clarified later in the discussion about polymorphism: In resolving the field reference f, the search up the inheritance hierarchy starts at type t (the type of expression e), and not type t’ (the type of the object).

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

14 CS 100Lecture 2414 Types of Variables n Recall that the type of a variable determines the types of values that can be stored in the variable. n A type t variable can contain type t objects. Room r = new Room(); // LEGAL! Room r = new Room(); // LEGAL! Account act = new Acount(); // LEGAL! Account act = new Acount(); // LEGAL! n 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 CS 100Lecture 2415 Polymorphism A type t variable can contain any object whose type is t or a subtype of t 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 CS 100Lecture 2416 Polymorphism, continued class Room { int id;// Id number of room...... public String toString() { return ”Room: ” + id; } { return ”Room: ” + id; }} class Bathroom extends Room {... public String toString() { return ”Bathroom: ” + id; } { 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: …”


Download ppt "CS 100Lecture 241 CS100J Lecture 24 n Previous Lecture –MatLab demonstration n This Lecture –Inheritance –Method overriding –Polymorphism –Reading: n Lewis."

Similar presentations


Ads by Google