Inheritance Introduction In real situations either when modeling


















![Using Subclasses class Bank. Example 1 { public static void main(String args[]) { Savings. Using Subclasses class Bank. Example 1 { public static void main(String args[]) { Savings.](https://slidetodoc.com/presentation_image_h2/b72a793a1a651c69f286b267ee2b9b59/image-19.jpg)




![Using Subclasses class Bank. Example 2 { public static void main(String args[]) { double Using Subclasses class Bank. Example 2 { public static void main(String args[]) { double](https://slidetodoc.com/presentation_image_h2/b72a793a1a651c69f286b267ee2b9b59/image-24.jpg)




- Slides: 28
Inheritance
Introduction • In real situations either when modeling real world objects such as vehicles, animals, etc. or when modeling abstract data structures such as queues, stacks, collections, windows, menu boxes the structure of different object families can be viewed as a kind of "family" tree. • Java like most OO languages allows us to use these types of relationships to reuse code and functionality by making classes that use characteristics of "parent" classes
Defining the Differences • Classes often share capabilities • We want to avoid re-coding these capabilities • Reuse of these would be best to – Improve maintainability – Reduce cost – Improve “real world” modeling
Inheritance Defined • When one class re-uses the capabilities defined in another class. • The new subclass gains all the methods and attributes of the superclass. Superclass Subclass Car Vehicle Truck
Benefits of Inheritance • Saves effort of “reinventing the wheel” • Allows us to build on existing code, specializing without having to copy it, rewrite it, etc. • To create the subclass, we need to program only the differences between the superclass and the subclass that inherits from it. • Allows for flexibility in class definitions.
Example Node Data set. Data get. Data List. Node Tree. Node Next Left Right set. Next get. Next set. Left set. Right get. Left get. Right
Inheritance: Example class Node { private int data; public Node(int new. Data) { set. Data(new. Data); } public void set. Data(int new. Data) { data = new. Data; } public int get. Data() { return data; } } // Node
Inheritance: Example class List. Node extends Node { private List. Node next; // Fields public List. Node(int new. Data) { // Constructor super(new. Data); set. Next(null); } public void set. Next(List. Node new. Next) { next = new. Next; } public List. Node get. Next() { return next; } } // List. Node
Inheritance: Example class Tree. Node extends Node { private Tree. Node left; private Tree. Node right; public Tree. Node(int new. Data) { // Constructor super(new. Data); set. Left(null); set. Right(null); } public void set. Left(Tree. Node new. Left) { left = new. Left; } public void set. Right(Tree. Node new. Right) { right = new. Right; }
Inheritance: Example // class Tree. Node (Continued) public Tree. Node get. Left() { return left; } public Tree. Node get. Right() { return right; } } // Tree. Node
Two Types of Inheritance • Extension – Adds attributes and methods • Redefinition (Method Overriding) – Changes/modifies existing methods, specializing as needed
Inheritance Example: Bank Accounts • Consider a primitive bank account which allows only three kinds of transactions: – Deposits – Withdrawals – Ability to check current balance
The Base Class Bank_Account set. Balance Get. Balance deposit withdraw • balance
A Superclass Bank_Account class Bank. Account { private double balance; public Bank. Account() { set. Balance(0); } // constructor public void set. Balance(double balance) { this. balance = balance; System. out. println( "New balance now: "+ get. Balance()); } public double get. Balance() { return balance; }
A Superclass Bank_Account // class Bank. Account continued public void deposit(double amt) { set. Balance(get. Balance() + amt); } public void withdraw(double amt) { if(get. Balance() < amt) { amt = get. Balance(); System. out. println ("Can only withdraw " + amt); } set. Balance() - amt); } } // Bank. Account
Inheritance by Extension • Imagine that we wish to create a new kind of Bank Account that is: – Identical to the base class in all respects, except one – We want to add the ability for the account to earn interest • Without inheritance, we’d have to write it from scratch, duplicating code, etc. • With inheritance, we need code only the new capability and inherit the rest.
Illustration of Inheritance Bank. Account Get. Balance • balance deposit set. Balance withdraw Savings. Account • RATE • MIN_BALANCE calc. Interest
Inheritance by Extension class Savings. Account extends Bank. Account { public final static double RATE = 0. 023; public final static double MIN_BALANCE = 500. 00; public double calc. Interest() { double interest; if (get. Balance() >= MIN_BALANCE) interest = get. Balance()*RATE; else interest = 0. 00; return interest; } }
Using Subclasses class Bank. Example 1 { public static void main(String args[]) { Savings. Account my. Savings; double my. Interest; my. Savings = new Savings. Account(); my. Savings. deposit(500. 00); my. Interest = my. Savings. calc. Interest(); my. Savings. deposit(my. Interest); my. Savings. withdraw( IOGadget. read. Double( Bank. Account "Enter amount to withdraw")); } } Savings. Account
Inheritance by Redefinition • Imagine that we wish to create a new kind of Savings Account that is identical to Savings Account in all respects, except: – We want to change the way in which withdrawals are handled – The base class already handles withdrawals, but now we want a subclass that does them differently. • Without inheritance, we’d have to rewrite it from scratch. • With inheritance, we need code only the new way that we want withdrawals to work,
Illustration of Redefinition Savings. Account • RATE • MIN_BALANCE calc. Interest Deluxe. Savings allow. Overdraft withdraw • overdraft. OK • OVERDRAFTCHARGE
Inheritance with Redefinition class Deluxe. Savings extends Savings. Account { private boolean overdraft. OK; public final static double OVERDRAFTCHARGE = 20. 0; public Deluxe. Savings() { super(); set. Overdraft. OK(false); } public void set. Overdraft. OK(boolean odok) { overdraft. OK = odok; } public void withdraw(double amt) { if (overdraft. OK) { set. Balance(get. Balance() - amt); if (get. Balance() < 0) set. Balance(get. Balance() - OVERDRAFTCHARGE); } else { super. withdraw(amt); } } // withdraw } // Deluxe. Savings
super & this • super means “look in the superclass” • this means “look in this class” • Constructor: super(); this(); • Method: super. m(); • Method: this. m(); • Field: super. x; • Field: this. x;
Using Subclasses class Bank. Example 2 { public static void main(String args[]) { double interest, amt 1, amt 2; Deluxe. Savings mds = new Deluxe. Savings(); mds. deposit(250. 00); amt 1 = IOGadget. read. Double("Enter amount to withdraw"); mds. withdraw(amt 1); mds. set. Overdraft. OK(true); interest = mds. calc. Interest(); mds. deposit(interest); amt 2 = IOGadget. read. Double("Enter amount to withdraw"); mds. withdraw(amt 2); } // main } // Demo
Design & Use • Declare common methods/attributes as high in the class hierarchy as possible • All subclasses will inherit these capabilities • Specialize (extend and redefine) in subclasses • When a method is invoked, the request is serviced by the lowest, most specific class and moves up as needed to find a match
Bank. Account balance deposit withdraw get. Balance Savings. Account RATE MIN_BALANCE calc. Interest Deluxe. Savings overdraft. OK OVERDRAFT_CHARGE set. Overdraft. OK set. Overdraft. NOT withdraw Method Resolution Deluxe. Savings ds; ds = new Deluxe. Savings(); ds. deposit(1000. 00); ds. deposit( ds. calc. Interest()); ds. withdraw(500. 00) ds. set. Overdraft. OK(true); ds. withdraw(2000. 00);
Summary of Inheritance • Extension take a base class and add new capabilities to it (methods, fields). • Redefinition (method overriding) takes a base class and redefines an existing method, implementing it in a new way in order to change capability or performance. • Both allow us to code only the differences.