Lecture Notes – Inheritance (Ch 9-10)

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

IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
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.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
Our BankAccount class should define three methods  deposit  withdraw  getBalance How a programmer will carry out these operations?  We assume that.
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.
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.
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.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
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.
CHAPTER 11 INHERITANCE. CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
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.
Lecture Notes – Inheritance and Polymorphism (Ch 9-10) Yonglei Tao.
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:
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.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one 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.
Chapter 3 Implementing Classes
1 Principles of Computer Science II Prof. Nadeem Abdul Hamid CSC 121 – Spring 2006 Lecture Unit 4 - Inheritance.
Chapter Ten: Inheritance
Chapter 13 - Inheritance.
Lecture 3 John Woodward.
Data Structures and Algorithms revision
Inheritance In the real world, objects aren’t usually one-of-a-kind.
Lecture 12 Inheritance.
03/10/14 Inheritance-2.
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Inheritance in Java.
Implementing Classes Yonglei Tao.
Phil Tayco Slide version 1.1 Created Oct 30, 2017
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
Chapter 11 Inheritance and Polymorphism Part 1
Presentation transcript:

Lecture Notes – Inheritance (Ch 9-10) Yonglei Tao

Inheritance An “is a” relationship between classes Generalization and specialization Software reuse Employee Worker Manager

Extending a Class public class Employee { protected String name; protected double basePay; public double getBasePay () { return basePay; } public void setBasePay ( doule amt ) { basePay = amt; } public String toString () { return “Name: “+name+“\nPay: “+basePay; } }   public class Worker extends Employee { private double hours; public double getHours() { return hours; } public void setHours( double hrs ) { hours = hrs; } public String toString () { return super.toString()+“\nHours: “+hours; }

Syntax Inheritance

Syntax Calling a Superclass Method

Constructors // in class Employee public Employee () { } public Employee (String name, double pay) { this.name = name; basePay = pay; }   // in class Worker public Worker (String name, double pay, double hours) { super (name, pay); this.hours = hours;

Syntax Calling a Superclass Constructor

Polymorphic References Worker w = new Worker(“Ed”, 9.25, 25); System.out.println (“Base Pay: “ + w.getBasePay()); System.out.println (w); Employee p; // a polymorphic reference p = new Worker(“Tom”, 7.95, 40); System.out.println (p); p = new Manager(“John”, 36000); A super class reference can refer to an object of any subclass

The Object Class All classes extend Object, directly or indirectly, explicitly or implicitly, and therefore inherit its methods

Methods of Object String toString() boolean equals (Object other) int hashCode() Object clone() Class getClass() Redefine a method if the default definition is not appropriate for a class that you define Methods toString() and equals() are most useful Good idea to override them in your classes

Overriding the toString Method Object.toString prints class name and the hash code of the object: BankAccount momsSavings = new BankAccount(5000); String s = momsSavings.toString(); // Sets s to something like "BankAccount@d24606bf"

Overriding the toString Method To provide a nicer representation of an object, override toString: public String toString() { return "BankAccount[balance=" + balance + "]"; } This works better: BankAccount momsSavings = new BankAccount(5000); String s = momsSavings.toString(); // Sets s to "BankAccount[balance=5000]"

Overriding the equals Method equals tests for same contents: if (coin1.equals(coin2)) . . . // Contents are the same

Overriding the equals Method == tests for references to the same object: if (coin1 == (coin2)) . . . // Objects are the same

Overriding the equals Method Need to override the equals method of the Object class: public class Coin { private String name; private double value; ... public boolean equals(Object otherObject) }

Overriding the equals Method Cannot change parameter type; use a cast instead: public class Coin { ... public boolean equals(Object otherObject) Coin other = (Coin) otherObject; return name.equals(other.name) && value == other.value; } You should also override the hashCode method so that equal objects have the same hash code

Inheritance Hierarchies Often categorize concepts into hierarchies:

Multiple Inheritance

Inheritance Hierarchies Example: Different account types: Checking account: No interest Small number of free transactions per month Charges transaction fee for additional transactions Savings account: Earns interest that compounds monthly Superclass: BankAccount Subclasses: CheckingAccount & SavingsAccount

Inheritance Hierarchies 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 must override deposit and withdraw methods to count the transactions

Inheritance Hierarchies

BankAccount.java 1 /** 2 A bank account has a balance that can be changed by 3 deposits and withdrawals. 4 */ 5 public class BankAccount 6 { 7 private double balance; 8 9 /** 10 Constructs a bank account with a zero balance. 11 */ 12 public BankAccount() 13 { 14 balance = 0; 15 } 16 17 /** 18 Constructs a bank account with a given balance. 19 @param initialBalance the initial balance 20 */ 21 public BankAccount(double initialBalance) 22 { 23 balance = initialBalance; 24 }

26 /** 27 Deposits money into the bank account. 28 @param amount the amount to deposit 29 */ 30 public void deposit(double amount) 31 { 32 balance = balance + amount; 33 } 34 35 /** 36 Withdraws money from the bank account. 37 @param amount the amount to withdraw 38 */ 39 public void withdraw(double amount) 40 { 41 balance = balance - amount; 42 } 43 44 /** 45 Gets the current balance of the bank account. 46 @return the current balance 47 */ 48 public double getBalance() 49 { 50 return balance; 51 } 52 }

SavingsAccount.java 1 /** 1 /** 2 An account that earns interest at a fixed rate. 3 */ 4 public class SavingsAccount extends BankAccount 5 { 6 private double interestRate; 7 8 /** 9 Constructs a bank account with a given interest rate. 10 @param rate the interest rate 11 */ 12 public SavingsAccount(double rate) 13 { 14 interestRate = rate; 15 } 16 17 /** 18 Adds the earned interest to the account balance. 19 */ 20 public void addInterest() 21 { 22 double interest = getBalance() * interestRate / 100; 23 deposit(interest); 24 } 25 }

CheckingAccount.java 1 /** 1 /** 2 A checking account that charges transaction fees. 3 */ 4 public class CheckingAccount extends BankAccount 5 { 6 private static final int FREE_TRANSACTIONS = 3; 7 private static final double TRANSACTION_FEE = 2.0; 8 9 private int transactionCount; 10 11 /** 12 Constructs a checking account with a given balance. 13 @param initialBalance the initial balance 14 */ 15 public CheckingAccount(double initialBalance) 16 { 17 // Construct superclass 18 super(initialBalance); 19 20 // Initialize transaction count 21 transactionCount = 0; 22 } 23

24 public void deposit(double amount) 25 { 26 transactionCount++; 27 // Now add amount to balance 28 super.deposit(amount); 29 } 30 31 public void withdraw(double amount) 32 { 33 transactionCount++; 34 // Now subtract amount from balance 35 super.withdraw(amount); 36 } 37 38 /** 39 Deducts the accumulated fees and resets the 40 transaction count. 41 */ 42 public void deductFees() 43 { 44 if (transactionCount > FREE_TRANSACTIONS){ 45 double fees = TRANSACTION_FEE * 46 (transactionCount - FREE_TRANSACTIONS); 47 super.withdraw(fees); 48 } 49 transactionCount = 0; 50 } 51 }

AccountTester.java 1 /** 1 /** 2 This program tests the BankAccount class and 3 its subclasses. 4 */ 5 public class AccountTester 6 { 7 public static void main(String[] args) 8 { 9 SavingsAccount momsSavings = new SavingsAccount(0.5); 10 11 CheckingAccount harrysChecking = new CheckingAccount(100); 12 13 momsSavings.deposit(10000); 14 15 momsSavings.transfer(2000, harrysChecking); 16 harrysChecking.withdraw(1500); 17 harrysChecking.withdraw(80); 18 19 momsSavings.transfer(1000, harrysChecking); 20 harrysChecking.withdraw(400); 21

AccountTester.java (cont.) 22 // Simulate end of month 23 momsSavings.addInterest(); 24 harrysChecking.deductFees(); 25 26 System.out.println("Mom’s savings balance: " 27 + momsSavings.getBalance()); 28 System.out.println("Expected: 7035"); 29 30 System.out.println("Harry’s checking balance: " 31 + harrysChecking.getBalance()); 32 System.out.println("Expected: 1116"); 33 } 34 } Program Run: Mom's savings balance: 7035.0 Expected: 7035 Harry's checking balance: 1116.0 Expected: 1116

Visibility Private members are inherited by the child class, but cannot be referenced by name However, they may be used indirectly

Final Classes and Methods public final class A { ... // cannot be extended }   public class B { final void f () { ... } // cannot be overridden .... Improve security and optimization