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

Slides:



Advertisements
Similar presentations
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Advertisements

Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
OOP: Inheritance By: Lamiaa Said.
CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CS 100Lecture 61 CS100J Lecture 6 n Previous Lecture –Programming Concepts n Programming by stepwise refinement –a pattern –sequential refinement –case.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Inheritance (notes for 10/26 lecture). Inheritance Inheritance is the last of the relationships we will study this semester. Inheritance is (syntactically)
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Lecture 9 Polymorphism Richard Gesick.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 4 Inheritance.
Some Important topics. Working with Files Working with a file involves three steps: – Open the file – Perform operations on the file – Close the file.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Constructor It is a special member of a class that has the following characteristic 1)It has the same name as of its class. 2)It don’t have an explicit.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
CS 100Lecture 261 CS100J Lecture 26 n Previous Lecture –Application of inheritance –“Higher-order" methods –Abstract classes –Visibility modifier: protected.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Object-Oriented Programming: Polymorphism Chapter 10.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-1.
BY:- TOPS Technologies
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.
CS100A Lecture 22 Previous Lecture This Lecture Inheritance
Continuing Chapter 11 Inheritance and Polymorphism
CSC 205 Java Programming II
Chapter 9 Inheritance and Polymorphism
Encapsulation Inheritance PolyMorhpism
Chapter 11 Inheritance and Polymorphism
CS100J Lecture 8 Previous Lecture This Lecture Programming Concepts
Chapter 11 Inheritance and Polymorphism Part 1
Presentation transcript:

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

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

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

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

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

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

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

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.

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.

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);

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

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).

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!

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!

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!

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: …”