Download presentation
Presentation is loading. Please wait.
Published byJohana McConnell Modified over 9 years ago
1
Chapter 13 - Inheritance
2
Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To understand how to inherit and override superclass methods To be able to invoke superclass constructors To be able to invoke superclass constructors To learn about protected and package access control To learn about protected and package access control To understand the common superclass Object and to override its toString and equals methods To understand the common superclass Object and to override its toString and equals methods
3
In OOP languages, new classes can be derived from an existing class. In OOP languages, new classes can be derived from an existing class. Why? Why? organizes related classes organizes related classes reduces code redundancy reduces code redundancy increases code reuse increases code reuse enables polymorphic references enables polymorphic references
4
13.1 Introduction to Inheritance Inheritance: extend classes by adding methods and fields Inheritance: extend classes by adding methods and fields Example: Savings account is a bank account with interest Example: Savings account is a bank account with interest class SavingsAccount extends BankAccount { new methods new instance fields }
5
SavingsAccount automatically inherits all methods and instance fields of BankAccount SavingsAccount automatically inherits all methods and instance fields of BankAccount SavingsAccount collegeFund = new SavingsAccount(10); // Savings account with 10% interest collegeFund.deposit(500); // OK to use BankAccount method with SavingsAccount // object
6
Original/base class is known as the superclass (BankAccount) Original/base class is known as the superclass (BankAccount) extending class is the subclass (SavingsAccount) extending class is the subclass (SavingsAccount)
7
Every class extends the Object class either directly or indirectly Every class extends the Object class either directly or indirectly
8
Inheritance vs Interface Inheriting from class IS NOT the same as implementing interface Inheriting from class IS NOT the same as implementing interface subclass inherits behavior and state subclass inherits behavior and state Interfaces have no state or defined behavior (only names of shared methods) Interfaces have no state or defined behavior (only names of shared methods)
9
Code Reuse One advantage of inheritance is code reuse One advantage of inheritance is code reuse Not “reinventing the wheel” Not “reinventing the wheel” Already have a class that does some base functions, why not just build up on it? Already have a class that does some base functions, why not just build up on it? Deposit, withdraw, getBalance common among all accounts Deposit, withdraw, getBalance common among all accounts
10
In subclass, specify added instance fields, added methods, and changed or overridden methods In subclass, specify added instance fields, added methods, and changed or overridden methods Inheritance takes care of what is common, you define what is different Inheritance takes care of what is common, you define what is different public class SavingsAccount extends BankAccount{ private double interestRate; public SavingsAccount(double rate) { interestRate = rate; } public void addInterest() { double interest = getBalance() * interestRate / 100; deposit(interest);}}
11
Encapsulation Why do we call getBalance? Why do we call getBalance? double interest = getBalance() * interestRate / 100; Encapsulation: addInterest calls getBalance because balance field of the superclass is private Encapsulation: addInterest calls getBalance because balance field of the superclass is private Cannot access private members of another class (even a subclass!) Cannot access private members of another class (even a subclass!)
12
SavingsAccount object inherits the balance instance field from BankAccount, and gains one additional instance field: interestRate SavingsAccount object inherits the balance instance field from BankAccount, and gains one additional instance field: interestRate
13
Note that addInterest calls getBalance without specifying an implicit parameter (the calls apply to the same object) Note that addInterest calls getBalance without specifying an implicit parameter (the calls apply to the same object) Means the call to is getBalance is applied to the same object as the object that called addInterest Means the call to is getBalance is applied to the same object as the object that called addInterest
14
Syntax class SubclassName extends SuperclassName { methods instance fields }
15
13.2 Inheritance Hierarchies Inheritance is a way to categorize Inheritance is a way to categorize In real world, categories often use hierarchies In real world, categories often use hierarchies Generic items yield more specific items Generic items yield more specific items Bird Robin, Blue Jay, Cardinal, etc. Bird Robin, Blue Jay, Cardinal, etc. Sets of classes can form complex inheritance hierarchies Sets of classes can form complex inheritance hierarchies
17
Hierarchy What is the common set of features? What is the common set of features? Superclass Superclass There are all birds class Bird{…} There are all birds class Bird{…} ISA Hierarchy ISA Hierarchy What is at the top of every hierarchy? What is at the top of every hierarchy?
18
Example Consider a bank that offers its customers the following account types: Consider a bank that offers its customers the following account types: Checking account: no interest; small number of free transactions per month, additional transactions are charged a small fee Checking account: no interest; small number of free transactions per month, additional transactions are charged a small fee Savings account: earns interest that compounds monthly Savings account: earns interest that compounds monthly
19
Inheritance Hierarchy
20
Behaviors All bank accounts support the getBalance method All bank accounts support the getBalance method All bank accounts support the deposit and withdraw methods, but the implementations differ 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 Checking account needs a method deductFees ; savings account needs a method addInterest
21
13.3 Inheriting Instance Fields and Methods A subclass can define additional instance fields and methods A subclass can define additional instance fields and methods With existing methods With existing methods They can override definitions from the superclass They can override definitions from the superclass They can inherit them as is They can inherit them as is
22
Overriding methods Supply a different implementation of a method that exists in the superclass Supply a different implementation of a method that exists in the superclass Must have same signature (same name and same parameter types) 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 If method is applied to an object of the subclass type, the overriding method is executed
23
Inherit method Don't supply a new implementation of a method that exists in superclass Don't supply a new implementation of a method that exists in superclass Superclass method can be applied to the subclass objects Superclass method can be applied to the subclass objects
24
Add method Supply a new method that doesn't exist in the superclass Supply a new method that doesn't exist in the superclass New method can be applied only to subclass objects New method can be applied only to subclass objects
25
Inheriting Instance Fields Can't override fields Can't override fields Inherit field: All fields from the superclass are automatically inherited (but may not be visible) Inherit field: All fields from the superclass are automatically inherited (but may not be visible) Add field: Supply a new field that doesn't exist in the superclass Add field: Supply a new field that doesn't exist in the superclass
26
Inheriting Methods What if you define a new field with the same name as a superclass field? 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 Each object would have two instance fields of the same name Fields can hold different values Fields can hold different values Legal but extremely undesirable Legal but extremely undesirable Another instance of shadowing Another instance of shadowing
27
CheckingAccount Class Overrides deposit and withdraw to increment the transaction count: Overrides deposit and withdraw to increment the transaction count: public class CheckingAccount extends BankAccount { private int transactionCount; // new instance field private int transactionCount; // new instance field public void deposit(double amount) {... } public void withdraw(double amount) {... } public void deductFees() {... } // new method }
28
Each CheckingAccount object has two instance fields: Each CheckingAccount object has two instance fields: balance ( inherited from BankAccount) balance ( inherited from BankAccount) transactionCount ( new to CheckingAccount) transactionCount ( new to CheckingAccount)
29
You can apply four methods to CheckingAccount objects: You can apply four methods to CheckingAccount objects: getBalance() ( inherited from BankAccount) getBalance() ( inherited from BankAccount) deposit(double amount) (overrides BankAccount method ) deposit(double amount) (overrides BankAccount method ) withdraw(double amount) ( overrides BankAccount method) withdraw(double amount) ( overrides BankAccount method) deductFees() (new to CheckingAccount) deductFees() (new to CheckingAccount)
30
Inheriting Private fields Consider deposit method of CheckingAccount Consider deposit method of CheckingAccount public void deposit(double amount) {transactionCount++; // now add amount to balance …}
31
Inheriting Private fields Consider deposit method of CheckingAccount Consider deposit method of CheckingAccount public void deposit(double amount) {transactionCount++; // now add amount to balance balance = balance + amount; } Will this work?
32
Can't just add amount to balance Can't just add amount to balance balance is a private field of the superclass balance is a private field of the superclass A subclass has no access to private fields of its superclass A subclass has no access to private fields of its superclass Subclass must use public interface Subclass must use public interface
33
Inheriting Private fields Consider deposit method of CheckingAccount Consider deposit method of CheckingAccount public void deposit(double amount) {transactionCount++; // now add amount to balance deposit(amount);} Will this work?
34
Invoking a Superclass Method Can't just call deposit(amount) in deposit method of CheckingAccount Can't just call deposit(amount) in deposit method of CheckingAccount That is the same as this.deposit(amount) //Checking account! That is the same as this.deposit(amount) //Checking account! Calls the same method (infinite recursion) Calls the same method (infinite recursion)
35
Solution: super Java allows you to specify calling a method of the super class with the keyword super Java allows you to specify calling a method of the super class with the keyword super Invoke superclass method Invoke superclass method super.deposit(amount) Now calls deposit method of BankAccount class Now calls deposit method of BankAccount class
36
public void deposit(double amount) {transactionCount++; // Now add amount to balance super.deposit(amount);}
37
public class CheckingAccount extends BankAccount { private static final int FREE_TRANSACTIONS = 3; private static final double TRANSACTION_FEE = 2.0;... public void withdraw(double amount) {transactionCount++; // Now subtract amount from balance super.withdraw(amount); }
38
public void deductFees() { if (transactionCount > FREE_TRANSACTIONS) { double fees = TRANSACTION_FEE * (transactionCount - FREE_TRANSACTIONS); super.withdraw(fees); } transactionCount = 0; }
39
Object Class a class extends Object by default when no extends clause is used, e.g: a class extends Object by default when no extends clause is used, e.g: class Thing {... } class Thing {... } class Thing extends Object {... } class Thing extends Object {... }
40
Override (redefine) We can override public (and protected) methods of any superclass Use the same signature to override an inherited method.
41
13.4 Inheritance and Constructors Unlike members and methods of a superclass, constructors of a superclass are not inherited by its subclasses. Unlike members and methods of a superclass, constructors of a superclass are not inherited by its subclasses. You must define a constructor for a subclass or use the default constructor added by the compiler. You must define a constructor for a subclass or use the default constructor added by the compiler. How do you initialize superclass fields though How do you initialize superclass fields though In SavingsAccount, how do we initialize balance? In SavingsAccount, how do we initialize balance?
42
13.4 Inheritance and Constructors super(); Calls the default constructor of the superclass Calls the default constructor of the superclass Analogous to this() Analogous to this() Every constructor of a subclass must make a call to the superclass constructor. Every constructor of a subclass must make a call to the superclass constructor. If you don’t compiler will add in If you don’t compiler will add in A call to super( ) MUST be the first line of code in the constructor A call to super( ) MUST be the first line of code in the constructor
43
Checking Account public class CheckingAccount extends BankAccount { public CheckingAccount(double initialBalance) { // Construct superclass super(initialBalance); // Initialize transaction count transactionCount = 0; }... }
44
class MyClass { public MyClass(int x){...}} class SubClass extends MyClass{ //No Constructor } Won’t compile – default constructor of SubClass tries to call super( ), but MyClass( ) is not defined
45
class MyClass { public MyClass(int x){...}} class SubClass extends MyClass{ public SubClass(){ super();//INVALID!}}
46
13.4 Inheritance and Constructors If a class has a superclass that is not the Object class, then a constructor of the class should make an explicit call to a constructor of the superclass. If a class has a superclass that is not the Object class, then a constructor of the class should make an explicit call to a constructor of the superclass. Always provide a constructor for every class you define. Don’t rely on default constructors. Always provide a constructor for every class you define. Don’t rely on default constructors.
47
class MyClass { public MyClass(int x){...}} class SubClass extends MyClass{ public SubClass(int y){ super(y);//VALID!}}
48
Ok to convert subclass reference to superclass reference (think: BlueJay to Bird) Ok to convert subclass reference to superclass reference (think: BlueJay to Bird) SavingsAccount collegeFund = new SavingsAccount(10); BankAccount anAccount = collegeFund; Object anObject = collegeFund;
50
Superclass references don't know the full story: 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 is this? Why is this?
51
Conversions Converting up to superclass leads to less information Converting up to superclass leads to less information Why would we want this? Why would we want this? Reuse code that uses superclass Reuse code that uses superclass
52
Reuse code that knows about the superclass but not the subclass: Reuse code that knows about the superclass but not the subclass: public void transfer(double amount, BankAccount other) {withdraw(amount);other.deposit(amount);}
53
Already learned how to use this method to transfer from one BankAccount to another Already learned how to use this method to transfer from one BankAccount to another But we can also use it to transfer from one CheckingAccount to another! But we can also use it to transfer from one CheckingAccount to another! The method doesn’t know the difference, because it only needs to know that a CheckingAccount IS A BankAccount The method doesn’t know the difference, because it only needs to know that a CheckingAccount IS A BankAccount
54
Super to Sub Conversion How do we convert down the chain How do we convert down the chain BankAccount object CheckingAccount? BankAccount object CheckingAccount? Is this safe? Is this safe? We need a way to protect ourselves if we aren’t sure… We need a way to protect ourselves if we aren’t sure…
55
instanceof Purpose: Check to see if an object is of a particular class Purpose: Check to see if an object is of a particular class Give: identifier and class Give: identifier and class Returns: boolean – true if it is that type, false otherwise Returns: boolean – true if it is that type, false otherwise
56
Convert from super to sub if (anObject instanceof BankAccount) { BankAccount anAccount = (BankAccount) anObject;... }
57
13.6 Polymorphism In Ch.11, we learned that the type of the identifier (Measurable) does not have to match the type of the object (BankAccount, Coin) In Ch.11, we learned that the type of the identifier (Measurable) does not have to match the type of the object (BankAccount, Coin) Inheritance demonstrates the same phenomenon Inheritance demonstrates the same phenomenon A BankAccount identifier can be referring to a BankAccount, CheckingAccount, or SavingsAccount A BankAccount identifier can be referring to a BankAccount, CheckingAccount, or SavingsAccount
58
Which version of deposit is called? Which version of deposit is called? When is this determined? When is this determined? BankAccount anAccount = new CheckingAccount(); anAccount.deposit(1000);
59
Method calls are always determined on the type of the actual object being stored, not the type of the reference/identifier Method calls are always determined on the type of the actual object being stored, not the type of the reference/identifier This ability to refer to multiple types with varying behavior is called polymorphism This ability to refer to multiple types with varying behavior is called polymorphism
60
Limitation A limitation is that polymorphism only works if the reference type always has an implementation of that call A limitation is that polymorphism only works if the reference type always has an implementation of that call Ex. Will the following work? Ex. Will the following work? Measurable x = new BankAccount(); x.deposit(500);
61
Why? Previously, we called deposit on a BankAccount object. Previously, we called deposit on a BankAccount object. When compiling, Java needs to know that a deposit method is legal to call on that object, not which method will be called When compiling, Java needs to know that a deposit method is legal to call on that object, not which method will be called Even though we didn’t know which version would be called, we can be guaranteed that any object stored with a BankAccount reference can handle deposit Even though we didn’t know which version would be called, we can be guaranteed that any object stored with a BankAccount reference can handle deposit
62
If the method is specific to only one subclass, then the compiler can’t guarantee legality If the method is specific to only one subclass, then the compiler can’t guarantee legality Object anObject = new BankAccount(); anObject.deposit(1000); // Compiling Error ------- BankAccount ba = new CheckingAccount(); anObject.deductFees(); // Compiling Error
63
13.7 Access Control Java has four levels of controlling access to fields, methods, and classes: public access public access Can be accessed by methods of all classes Can be accessed by methods of all classes private access private access Can be accessed only by the methods of their own class Can be accessed only by the methods of their own class
64
protected access protected access Can be accessed by methods of this class and subclasses only Can be accessed by methods of this class and subclasses only See Advanced Topic 13.3 See Advanced Topic 13.3 package access package access The default, when no access modifier is given The default, when no access modifier is given Can be accessed by all classes in the same package Can be accessed by all classes in the same package
65
13.8 Object: The Cosmic Superclass Recall that everything inherits from Object Recall that everything inherits from Object What comes in this class? What comes in this class? Most useful methods: Most useful methods: String toString() String toString() boolean equals(Object otherObject) boolean equals(Object otherObject) Object clone() Object clone() Why to these matter? Why to these matter? Good idea to override these methods Good idea to override these methods
66
package java.lang; class Object belongs to java.lang package belongs to java.lang package is the superclass of all other classes is the superclass of all other classes has several generic methods has several generic methods equals equals toString toString getClass getClass clone clone
67
Object Class Methods boolean equals ( Object obj ) boolean equals ( Object obj ) returns true if (and only if) this object is alias of obj returns true if (and only if) this object is alias of obj default: compares addresses default: compares addresses String toString () String toString () returns a String representing this object returns a String representing this object default: @ default: @
68
13.8.1 toString() Going to concentrate on the toString method Going to concentrate on the toString method Returns a string representation of the object Returns a string representation of the object Useful for debugging: Useful for debugging: Rectangle box = new Rectangle(5, 10, 20, 30); String s = box.toString(); // Sets s to // "java.awt.Rectangle[x=5,y=10,width=20,height=30]"
69
Java insight Unlike other methods, toString() can actually be called implicitly Unlike other methods, toString() can actually be called implicitly Concatenation Concatenation "box = " + box; Calling print() or println() Calling print() or println() System.out.println(box); System.out.println(box); How can the compiler know to do this? How can the compiler know to do this? Because every object has a toString method through Inheritance polymorphism Because every object has a toString method through Inheritance polymorphism
70
What does toString return? The Object class definition returns the object and a hashcode (identifier) The Object class definition returns the object and a hashcode (identifier) BankAccount momsSavings = new BankAccount(5000); String s = momsSavings.toString(); // Sets s to something like "BankAccount@d24606bf“ // Sets s to something like "BankAccount@d24606bf“ Pretty boring… Pretty boring…
71
Overriding toString() We can override the definition of any inherited method! We can override the definition of any inherited method! Usually want to know what’s inside the object (fields, etc) Usually want to know what’s inside the object (fields, etc) Print name of class, then the values of instance fields in brackets Print name of class, then the values of instance fields in brackets
72
public String toString() { return "BankAccount[balance=" + balance + "]"; }
73
More useful version Now this works better: Now this works better: BankAccount momsSavings = new BankAccount(5000); String s = momsSavings.toString(); // Sets s to "BankAccount[balance=5000]"
74
Adv 13.4 toString is a little harder with subclasses toString is a little harder with subclasses How do we make BankAccount toString work for any subclasses? How do we make BankAccount toString work for any subclasses? Use getClass() method Use getClass() method public String toString() { return getClass().getName() + "[balance=" + balance + "]"; }
75
equals Seen before in the String class Seen before in the String class Tests that the data in the two objects are the same, not that both reference the same object Tests that the data in the two objects are the same, not that both reference the same object Version in Object does the same thing as == Version in Object does the same thing as == Testing for equality might have different meanings in different classes (do all the data members need to match, or just certain ones?) Testing for equality might have different meanings in different classes (do all the data members need to match, or just certain ones?) So we need to override! So we need to override!
76
public boolean equals(Object obj) { BankAccount b = (BankAccount)obj; return b.balance == this.balance; } Parameter must be of type Object, but we must cast it before we can test the data members Parameter must be of type Object, but we must cast it before we can test the data members It is pretty easy for BankAccount, with only 1 primitive data member, but gets trickier when data members include arrays and other objects It is pretty easy for BankAccount, with only 1 primitive data member, but gets trickier when data members include arrays and other objects
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.