Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.

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

Computer Science A 9: 3/11. Inheritance Today: Inheritance (JC – CCJ ) I have to leave at 11am (but you can stay)
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
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.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
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.
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.
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.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
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.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Inheritance Motivation –Code reuse –Conceptual modeling.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Inheritance Abstract Classes Check out Inheritance from SVN.
ITM 352 Class inheritance, hierarchies Lecture #.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Inheritance.
Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
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).
Topic 4 Inheritance.
John Byrne Inheritance Bank account examples. John Byrne Example account hierarchy F account relationships: ACCOUNT SAVINGSCHEQUE TIME_ACCT.
CHAPTER 11 INHERITANCE. CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
CSC 205 Java Programming II Inheritance Inheritance In the real world, objects aren’t usually one-of-a-kind. Both cars and trucks are examples of.
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.
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.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
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 13 - Inheritance.
Data Structures and Algorithms revision
CSC 222: Object-Oriented Programming Spring 2017
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Lecture Notes – Inheritance (Ch 9-10)
Inheritance in Java.
CSC 222: Object-Oriented Programming Fall 2017
Phil Tayco Slide version 1.1 Created Oct 30, 2017
Computing with C# and the .NET Framework
Subclasses Chapter 11 Copyright © 2000 W. W. Norton & Company.
CSC 205 Java Programming II
Chapter Three - Implementing Classes
Chapter 10 – Inheritance Big Java by Cay Horstmann
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Polymorphism and access control
د.سناء الصايغ الفصل الأول البرمجة الشيئية
CSC 205 – Java Programming II
Adapted from Java Concepts Companion Slides
Presentation transcript:

Inheritance Part I

Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn about protected and package access control To understand the common superclass Object and to override its toString() and equals() methods

An Introduction to Inheritance Inheritance: extend classes by adding methods and fields Example: Savings account = bank account with interest class SavingsAccount extends BankAccount { new methods new instance fields }

An Introduction to Inheritance (Cont’d) The class SavingsAccoun t automatically inherits all methods and instance fields of the class BankAccount Extended class = superclass ( BankAccount ), extending class = subclass ( Savings ) Continued… SavingsAccount collegeFund = new SavingsAccount(10); // Savings account with 10% interest collegeFund.deposit(500); // OK to use BankAccount method with SavingsAccount object

An Introduction to Inheritance (Cont’d) Inheriting from class: subclass inherits behavior and state One advantage of inheritance is code reuse Every class extends the Object class either directly or indirectly Figure 1: An Inheritance Diagram

An Introduction to Inheritance (Cont’d) In subclass, specify added instance fields, added methods, and changed or overridden methods public class SavingsAccount extends BankAccount { private double interestRate; public SavingsAccount(double rate) { interestRate = rate; } public void addInterest() { double interest = getBalance() * interestRate / 100; deposit(interest); } }

An Introduction to Inheritance (Cont’d) Encapsulation: The method addInterest() calls getBalance() method rather than updating the balance field of the superclass (field is private ) Note that addInterest calls getBalance without specifying an implicit parameter (the calls apply to the same object)

Layout of a Subclass Object SavingsAccount object inherits the balance instance field from BankAccount, and gains one additional instance field: interestRate : Figure 2: Layout of a Subclass object

Syntax of Inheritance class SubclassName extends SuperclassName { methods instance fields }

Syntax of Inheritance (Cont’d) Example: public class SavingsAccount extends BankAccount { private double interestRate; public SavingsAccount(double rate) { interestRate = rate; } public void addInterest() { double interest = getBalance() * interestRate / 100; deposit(interest); } Purpose: To define a new class that inherits from an existing class, and define the methods and instance fields that are added in the new class.

Inheritance Hierarchies Sets of classes can form complex inheritance hierarchies Example: Figure 3: A Part of the Hierarchy of Ancient Reptiles

A Simpler Hierarchy: Hierarchy of Bank Accounts Consider a bank that offers its customers the following account types: 1.Checking account: no interest; small number of free transactions per month, additional transactions are charged a small fee 2.Savings account: earns interest that compounds monthly

A Simpler Hierarchy: Hierarchy of Bank Accounts (Cont’d) Figure 4: Inheritance Hierarchy for Bank Account Classes Inheritance hierarchy:

A Simpler Hierarchy: Hierarchy of Bank Accounts (Cont’d) All bank accounts support the getBalance() method All bank accounts support the deposit() and withdraw() methods, but the implementations differ Checking account needs a method deductFees() ; savings account needs a method addInterest()

A Simpler Hierarchy: Hierarchy of Bank Accounts (Cont’d) All bank accounts support the getBalance() method All bank accounts support the deposit() and withdraw() methods, but the implementations differ Checking account needs a method deductFees() ; savings account needs a method addInterest()

A Less Simpler Hierarchy: Hierarchy of Person Figure 5: Inheritance Hierarchy for Person-derived Classes

Inheriting Methods Override method:  Supply a different implementation of a method that exists in the superclass  Must have same signature (same name and same parameter types)  If method is applied to an object of the subclass type, the overriding method is executed Inherit method:  Don't supply a new implementation of a method that exists in superclass  Superclass method can be applied to the subclass objects

Inheriting Methods (Cont’d) Add method:  Supply a new method that doesn't exist in the superclass  New method can be applied only to subclass objects

Inheriting Instance Fields Can't override fields Inherit field: All fields from the superclass are automatically inherited Add field: Supply a new field that doesn't exist in the superclass

Inheriting Instance Fields (Cont’d) What if you define a new field with the same name as a superclass field?  Each object would have two instance fields of the same name  Fields can hold different values  Legal but extremely undesirable

Implementing the CheckingAccount Class Overrides the deposit() and withdraw() methods to increment the transaction count: public class CheckingAccount extends BankAccount { public void deposit(double amount) {...} public void withdraw(double amount) {...} public void deductFees() {...} // new method private int transactionCount; // new instance field }

Implementing the CheckingAccount Class (Cont’d) Each CheckingAccount object has two instance fields:  balance (inherited from BankAccount )  transactionCount (new to CheckingAccount )

Implementing the CheckingAccount Class You can apply four methods to CheckingAccount objects:  getBalance() (inherited from BankAccount )  deposit(double amount) (overrides BankAccount method)  withdraw(double amount) (overrides BankAccount method)  deductFees() (new to CheckingAccount)

Inherited Fields Are Private Consider deposit method of CheckingAccount Can't just add amount to balance balance is a private field of the superclass public void deposit(double amount) { transactionCount++; // now add amount to balance... }

Inherited Fields Are Private A subclass has no access to private fields of its superclass Subclass must use public accessors inherited from the superclass

Invoking a Super Class Method Can't just call deposit(amount) in deposit() method of CheckingAccount That is the same as this.deposit(amount) Calls the same method (infinite recursion) Instead, invoke superclass method super.deposit(amount)

Invoking a Super Class Method Now calls the deposit() method of BankAccount class Complete method: public void deposit(double amount) { transactionCount++; // Now add amount to balance super.deposit(amount); }

Syntax 13.2: Calling a Superclass Method super.methodName(parameters) Example: public void deposit(double amount) { transactionCount++; super.deposit(amount); } Purpose: To call a method of the superclass instead of the method of the current class