Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Slides:



Advertisements
Similar presentations
Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
Advertisements

Chapter 8 – Interfaces and Polymorphism Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
I NHERITANCE Chapter 10. I NHERITANCE Mechanism for enhancing existing classes You need to implement a new class You have an existing class that represents.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
Object-Oriented Programming in C++ Lecture 6 Inheritance.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =
CHAPTER 11 INHERITANCE CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Ten: Inheritance.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance Part III. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Object Oriented Design CSC 171 FALL 2001 LECTURE 12.
Chapter 10  Inheritance 1 Chapter 10 Inheritance.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
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.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Inheritance Motivation –Code reuse –Conceptual modeling.
Programming Languages and Paradigms Object-Oriented Programming.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
ITM 352 Class inheritance, hierarchies Lecture #.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Inheritance.
Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining.
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Java Programming Week 4: Inheritance (Chapter 10).
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Topic 4 Inheritance.
CHAPTER 11 INHERITANCE. CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 10 Inheritance. Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Encapsulation ◦ Blackbox concept Data and method(s) Hidden details InterfaceEffect(s) methods called class.
INFSY 535.  Small systems  Larger systems 1.Understand the program requirement- what 3. Write and test each part (unit testing) 4. Maintenance 2. Specify.
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:
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Inheritance INHERITANCE: extend existing classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class.
Chapter 13 Inheritance. Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
1 Principles of Computer Science II Prof. Nadeem Abdul Hamid CSC 121 – Spring 2006 Lecture Unit 4 - Inheritance.
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Chapter Ten: Inheritance
Chapter 13 - Inheritance.
Lecture 3 John Woodward.
Data Structures and Algorithms revision
Lecture Notes – Inheritance (Ch 9-10)
Inheritance in Java.
Computing with C# and the .NET Framework
CSC 205 Java Programming II
Chapter Three - Implementing Classes
Chapter 10 – Inheritance Big Java by Cay Horstmann
Polymorphism and access control
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Adapted from Java Concepts Companion Slides
Introduction to Inheritance
Presentation transcript:

Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Example: Different account types: 1.Checking account: No interest Small number of free transactions per month Charges transaction fee for additional transactions 2.Savings account: Earns interest that compounds monthly Superclass: BankAccount Subclasses: CheckingAccount & SavingsAccount Inheritance Hierarchies

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Behavior of account classes: All support getBalance method Also support deposit and withdraw methods, but implementation details differ Checking account needs a method deductFees to deduct the monthly fees and to reset the transaction counter Checking account can override deposit and withdraw methods to count the transactions Savings account needs a method addInterest() Inheritance Hierarchies

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Inheritance Hierarchies

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Syntax 10.1 Inheritance

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A subclass method overrides a superclass method if it has the same name and parameter types as a superclass method When such a method is applied to a subclass object, the overriding method is executed Overriding Methods

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. In subclass, specify additional instance variables, additional methods, and changed or overridden methods: public class SavingsAccount extends BankAccount { private double interestRate; public SavingsAccount(double rate) { // Constructor implementation } public void addInterest() { // method implementation } public void getBalance() { // overridden method implementation } Inheritance Hierarchies

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A subclass has no access to private instance variables of its superclass Encapsulation: addInterest calls getBalance rather than updating the balance variable of the superclass (variable is private ) Note that addInterest calls getBalance without specifying an implicit parameter (the calls apply to the same object) Inheriting from a class differs from implementing an interface: the subclass inherits behavior from the superclass Inheritance Hierarchies

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A subclass has no access to the private instance variables of the superclass: public class SavingsAccount extends BankAccount { public void addInterest() { double interest = getBalance() * interestRate / 100; balance = balance + interest; // Error }... } Common Error: Shadowing Instance Variables

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Beginner’s error: “solve” this problem by adding another instance variable with same name: public class SavingsAccount extends BankAccount { private double balance; // Don’t public void addInterest() { double interest = getBalance() * interestRate / 100; balance = balance + interest; // Compiles but doesn’t // update the correct balance }... } Common Error: Shadowing Instance Variables

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Now the addInterest method compiles, but it doesn’t update the correct balance! Common Error: Shadowing Instance Variables

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Example: deposit and withdraw methods of the CheckingAccount class override the deposit and withdraw methods of the BankAccount class to handle transaction fees: public class BankAccount {... public void deposit(double amount) {... } public void deposit(double amount) {... } public void withdraw(double amount) {... } public double getBalance() {... } } public class CheckingAccount extends BankAccount {... public void deposit(double amount) {... } public void withdraw(double amount) {... } public void withdraw(double amount) {... } public void deductFees() {... } } Overriding Methods

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Idea: public class CheckingAccount extends BankAccount { public void deposit(double amount) { transactionCount++; // Now add amount to balance using deposit() deposit(); // Not complete } } Won't work because compiler interprets deposit(amount); as this.deposit(amount); which calls the method we are currently writing ⇒ infinite recursion Overriding Methods

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Use the super reserved word to call a method of the superclass: public class CheckingAccount extends BankAccount { public void deposit(double amount) { transactionCount++; // Now add amount to balance super.deposit(); } Overriding Methods

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. public void deductFees() { if (transactionCount > FREE_TRANSACTIONS) { double fees = TRANSACTION_FEE * (transactionCount - FREE_TRANSACTIONS); super.withdraw(fees); } transactionCount = 0; }... } Overriding Methods (cont.)

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Syntax 10.2 Calling a Superclass Method

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Constructors have special requirements for using super()… Calling the super class constructor

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Syntax 10.3 Calling a Superclass Constructor

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Superclass references don’t know the full story: anAccount.deposit(1000); // OK anAccount.addInterest(); // No--not a method of the class to which anAccount // belongs Why would anyone want to know less about an object? Reuse code that knows about the superclass but not the subclass: public void transfer(double amount, BankAccount other) { withdraw(amount); other.deposit(amount); } Can be used to transfer money from any type of BankAccount Converting Between Subclass and Superclass Types

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Syntax 10.4 The instanceof Operator

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. How to 10.1: Inheritance Hierarchy

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Type of a variable doesn’t completely determine type of object to which it refers: Question q1 = new NumericQuestion(); q1.display(); Question q2 = new FreeResponseQuestion(); q2.display(); Which display method is called? Dynamic method lookup: When the virtual machine calls an instance method, it locates the method of the implicit parameter's class Also called Dynamic method invocation Polymorphism and Inheritance

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Polymorphism: Ability to treat objects with differences in behavior in a uniform way The method call display(); is a shortcut for this.display(); this can refer to a Question or a subclass object Polymorphism and Inheritance ChoiceQuestion q1 = new ChoiceQuestion(); q1.display(); MultiChoiceQuestion q2 = new MultiChoiceQuestion(); q2.display();

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Recall AnimalList with all dogs, cats, rabbits, etc. calling sleep() If a subclass implements a method that is in a superclass, such as sleep(), the sublcass overrides the super class method Dynamic lookup (dynamic invocation) occurs to determine which method to call To get both the subclass method and the super class method be invoked: sleep() super.sleep() Polymorphism and Inheritance

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Recall AnimalList with all dogs, cats, rabbits, etc. calling sleep() If a subclass implements a method that is in a superclass, such as sleep(), the sublcass overrides the super class method Dynamic lookup (dynamic invocation) occurs to determine which method to call To get both the subclass method and the super class method be invoked: sleep() super.sleep() Polymorphism and Inheritance

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. If a subclass does not implement a method that is invoked, the compiler traverses up the hierarchy to find the closest one Polymorphism and Inheritance mChoiceQ.display(); display()

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. All classes defined without an explicit extends clause automatically extend Object : Object : The Cosmic Superclass

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Object : The Cosmic Superclass Most useful methods: String toString() boolean equals(Object otherObject) Good idea to override these methods in your classes