Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.

Similar presentations


Presentation on theme: "Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship."— Presentation transcript:

1 Inheritance

2 Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship exists between the classes Inheritance: A class inherits the attributes of another class –An “is a” relationship exists between the classes class Student extends Person //is a { private Grade grade; // has a private String major; private Transcript courses; // why not name? … }

3 Inheriting from Applet class SelfPortrait extends Applet Applet is a superclass of SelfPortrait (also base class for SelfPortrait) SelfPortrait is a subclass of Applet (also derived class from Applet) SelfPortrait may selectively override Applet’s methods, e.g., init(), paint() because SelfPortrait is an Applet, the browser knows these methods exist polymorphism: the subclass method is called if the object actually is the subclass, even if it is referenced by a variable of the superclass

4 super Keyword A subclass can invoke the constructors and methods of its superclass to invoke a superclass constructor from the subclass constructor: super (arguments); // must be first executable stmt. to invoke a superclass method: super.method (arguments);

5 BankAccount class public class BankAccount { private double balance; public BankAccount (double initialBal) { balance = initialBal; } public double getBalance() { return balance; } public void deposit (double amount) { balance += amount; } public void withdraw (double amount) { balance -= amount; }

6 Inheriting from BankAccount The BankAccount class has methods deposit(double), withdraw(double), & double getBalance() a CheckingAccount class would want the withdraw() method to charge a fee a SavingsAccount class would also need an addInterest() method a CDAccount would be a SavingsAccount whose withdraw method would charge a penalty for early withdrawal

7 CheckingAccount public class CheckingAccount extends BankAccount { public CheckingAccount (double initialBal) { super (initialBal); } public void withdraw (double amt) { final double FEE =.10; super.withdraw (amt + FEE);// super req’d }

8 SavingsAccount public class SavingsAccount extends BankAccount { private double interest; public SavingsAccount (double initialBal, double rate) { super (initialBal); interest = rate; } public void addInterest () { // super not required, no conflict deposit (interest * getBalance()); }

9 Certificate of DepositAccount public class CDAccount extends SavingsAccount { private int term; public CDAccount (double initialBal, double rate, int months){ super (initialBal, rate); term = months; } public void withdraw (double amt){ double penalty = 0.; int duration = …; if (duration < term){ penalty = 200; } super.withdraw (amt + penalty); }

10 Polymorphism The ability to reference an object of some subclass via a variable of one of its superclasses, and to have method invocations on that variable use the subclass’s version of an overridden method At runtime, the java interpreter looks at the actual object in memory, determines its class, and invokes the method (if any) associated with that class E.g., BankAccount acct = new CheckingAccount(2000); acct.withdraw (500); // calls CA’s withdraw

11 Polymorphism (cont.) BankAccount[] accts = new BankAccount[3]; accts[0] = new CheckingAccount(1000); accts[1] = new SavingsAccount(1000,.04); accts[2] = new CDAccount(1000,.065, 12); for (int j=0; j< 3; j++) { accts[j].deposit (1000); accts[j].withdraw (500); //accts[j].addInterest(); won’t compile if (accts[j] instanceof SavingsAccount) { ((SavingsAccount)accts[j]).addInterest(); }

12 Polymorphism (cont.) polymorphism applies to methods only, not variables variables are referenced according to the declared type of the object, not the actual type class Base { int data; // note: not private... } class Sub extends Base { int data; // shadows Base’s data... } class Test { Base b = new Sub(); b.data = 6; // Base’s version of data Sub s = new Sub(); s.data = 7; // Sub’s version of data

13 Methods of the Object Class Object is the ultimate superclass the Object class provides some methods which are intended to be overridden –public String toString() // returns string representation –public boolean equals (Object o) // compares objects

14 The toString() Method public String toString() returns a string representation of an object toString() is called automatically –in string concatenation: BankAccount acct = new BankAccount(); String s = “My account is “ + acct; –by the println method: System.out.println (acct); if not overridden: BankAccount@d24606bf

15 The toString() Method(cont.) Override to print instance data public class BankAccount { … public String toString() { return “BankAccount[balance=“ + balance + “]”; }

16 The equals() Method public boolean equals (Object o) compares this object with the given object if not overridden, an == comparison of the object references is made this is probably not what you want override to compare some or all of the instance data

17 The equals() Method(cont.) public class BankAccount { private double balance;... public boolean equals (Object o) { if (!(o instanceof BankAccount)) return false; BankAccount other = (BankAccount)o; if (balance == other.balance) { return true; } return false; } Our object's methods can access the private data of another object of the same class

18 Variable accessibility declaring a variable private makes it available to methods of its own class only it does not include subclasses declaring a variable without an access modifier makes it available to methods of other classes in the same package declaring a variable protected makes it available to methods of subclasses and other classes in the same package declaring a variable public makes it available to methods of any class anywhere

19 Superclass Instance Variables public class SavingsAccount extends BankAccount { private double interest; // remember: private double balance; in BA... public boolean equals (Object o) { if (!(o instanceof SavingsAccount)) return false; SavingsAccount other = (SavingsAccount)o; if (balance == other.balance && interest == other.interest) { return true; } return false; } } // balance can’t be accessed by subclass if private


Download ppt "Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship."

Similar presentations


Ads by Google