CS100A Lecture 22 Previous Lecture This Lecture Inheritance

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

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.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
OOP: Inheritance By: Lamiaa Said.
CS 211 Inheritance AAA.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Lecture 10: Inheritance Subclasses and superclasses The inheritance chain Access control The Object cosmic superclass The super keyword Overriding methods.
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 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.
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.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
CS 100Lecture 241 CS100J Lecture 24 n Previous Lecture –MatLab demonstration n This Lecture –Inheritance –Method overriding –Polymorphism –Reading: n Lewis.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
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.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
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…..
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
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.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
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.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
1 More About Derived Classes and Inheritance Chapter 9.
Object-Oriented Concepts
Inheritance ITI1121 Nour El Kadri.
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.
Inheritance in Java.
Computing with C# and the .NET Framework
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Week 8 Lecture -3 Inheritance and Polymorphism
Continuing Chapter 11 Inheritance and Polymorphism
Object Oriented Programming
Lecture 23 Polymorphism Richard Gesick.
CSC 205 Java Programming II
Chapter 9 Inheritance and Polymorphism
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
Extending Classes.
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9: Polymorphism and Inheritance
Week 6 Object-Oriented Programming (2): Polymorphism
Encapsulation Inheritance PolyMorhpism
Building Java Programs
Chapter 11 Inheritance and Polymorphism
Chapter 10: Method Overriding and method Overloading
CIS 199 Final Review.
Chapter 11 Inheritance and Polymorphism
Review of Previous Lesson
Method Overriding and method Overloading
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
Programming in C# CHAPTER 5 & 6
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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