Review:    What happens when NewClass inherits from SomeClass

The NewClass inherits (= receives) all normal members from its superclass:

All inherited members are now defined inside NewClass and you can use the public members directly

Demo    program showing how to use (call) inherited public members

public class NewClass extends SomeClass 
{
    public NewClass(int a)
    {
        super(a);
    }

    // Inherits method1() and method2()

    public void method3()
    {
        // use inherited methods
	method1();
	method2();
    }
} 







 
public class SomeClass
{
    public int x;

    public SomeClass()
    {
        x = 99;
    }
    
    public SomeClass(int a)
    {
        x = a;
    }

    public void method1( )
    {
        System.out.println("SomeClass.method1(). x = " + x);
    }
    
    public void method2( )
    {
        System.out.println("SomeClass.method2(). x = " + x);
    }
} 
    public static void main(String[] args)
    {
        NewClass a = new NewClass(44);       
        a.method3();
    }

Review:    an overriding method will overshadow its inherited method

When you override an inherited method, a method call will invoke the overriding method:

Note:   overriding an inherited method does not change the method in the super class !

Demo    program showing the call method1() will invoke a overriding method

public class NewClass extends SomeClass 
{
    public NewClass(int a)
    {
        super(a);
    }

    // Inherits method1() and method2()

    public void method1( ) // Overrides method1()
    {
        System.out.println("NewClass.m1(): x = " + x);
    }

    public void method3()
    {
        // use inherited methods
	method1();  // Invokes overriding method
	method2();
    }
} 


 
public class SomeClass
{
    public int x;

    public SomeClass()
    {
        x = 99;
    }
    
    public SomeClass(int a)
    {
        x = a;
    }

    public void method1( )
    {
        System.out.println("SomeClass.method1(). x = " + x);
    }
    
    public void method2( )
    {
        System.out.println("SomeClass.method2(). x = " + x);
    }
} 
    public static void main(String[] args)
    {
        NewClass a = new NewClass(44);       
        a.method3();
    }

Calling an overridden method from within the subclass

You can invoke an overridden method from within a subclass method using super.methodName(...)

Note:   overriding an inherited method does not change the method in the super class !

Demo    program showing super.method1() will invoke a overridden method

public class NewClass extends SomeClass 
{
    public NewClass(int a)
    {
        super(a);
    }

    // Inherits method1() and method2()

    public void method1( ) // Overrides method1()
    {
        System.out.println("NewClass.m1(): x = " + x);
    }

    public void method3()
    {
        // use inherited methods
	super.method1();  // Invokes overridden method
	method2();
    }
} 


 
public class SomeClass
{
    public int x;

    public SomeClass()
    {
        x = 99;
    }
    
    public SomeClass(int a)
    {
        x = a;
    }

    public void method1( )
    {
        System.out.println("SomeClass.method1(). x = " + x);
    }
    
    public void method2( )
    {
        System.out.println("SomeClass.method2(). x = " + x);
    }
} 
    public static void main(String[] args)
    {
        NewClass a = new NewClass(44);       
        a.method3();
    }

Example showing the need to use super to invoke a overridded method

Consider the basic BankAccount class to store information on bank accounts:

public class SavingsAccount
       extends BankAccount
{   
    private final static int MAXNUMFREE = 3;
    private final static double PENALTY = 1.0;  

    private int numWithdrawals;
                                     
    public SavingsAccount(double x)
    {
        super(x);
    }

    public void withdraw(double amount)
    {
        super.withdraw(amount);

	numWithdrawals++;
	if ( numWithdrawals > MAXNUMFREE )
	    super.withdraw(PENALTY);
    }

    public void monthlyReset()
    {
        numWithdrawals = 0;
    }
}   
public class BankAccount
{
    private double balance;

    public BankAccount(double x)
    {
        balance = x;
    }

    public double getBalance()
    {
        return balance;
    }
    
    public void deposit(double amount)
    {
        balance = balance + amount;
    }
    
    public void withdraw(double amount) 
    {
        balance = balance - amount;
    }
}  



The BankAccount support the basic operations: getBalance(), deposit() and withdraw()

Example showing the need to use super to invoke a overridded method

The bank also has a special SavingsAccount specially for people who want to save money:

public class SavingsAccount
       extends BankAccount
{   
    private double interestRate = 0.05;
    private final static int MAXNUMFREE = 3;
    private final static double PENALTY = 1.0;  
    private int numWithdrawals;
                                     
    public SavingsAccount(double x)
    {
        super(x);
    }

    public void withdraw(double amount)
    {
        super.withdraw(amount);

	numWithdrawals++;
	if ( numWithdrawals > MAXNUMFREE )
	    super.withdraw(PENALTY);
    }

    public void addInterest()
    {
        balance += interestRate/12*balance;
    }
} 
public class BankAccount
{
    private double balance;

    public BankAccount(double x)
    {
        balance = x;
    }

    public double getBalance()
    {
        return balance;
    }
    
    public void deposit(double amount)
    {
        balance = balance + amount;
    }
    
    public void withdraw(double amount) 
    {
        balance = balance - amount;
    }
}  



The SavingsAccount offers in addition to the basic operations, a monthly interest for savers

Example showing the need to use super to invoke a overridded method

The bank discourage frequent withdrawal from a SavingsAccount and will allow 3 free withdrawals per month:

public class SavingsAccount
       extends BankAccount
{   
    private double interestRate = 0.05;
    private final static int MAXNUMFREE = 3;
    private final static double PENALTY = 1.0;  
    private int numWithdrawals;
                                     
    public SavingsAccount(double x)
    {
        super(x);
    }

    public void withdraw(double amount)
    {
        super.withdraw(amount);

	numWithdrawals++;
	if ( numWithdrawals > MAXNUMFREE )
	    super.withdraw(PENALTY);
    }

    public void addInterest()
    {
        balance += interestRate/12*balance;
    }
} 
public class BankAccount
{
    private double balance;

    public BankAccount(double x)
    {
        balance = x;
    }

    public double getBalance()
    {
        return balance;
    }
    
    public void deposit(double amount)
    {
        balance = balance + amount;
    }
    
    public void withdraw(double amount) 
    {
        balance = balance - amount;
    }
}  



After the 3 free withdrawals, each additional withdrawal will incur a $1.00 penalty

Example showing the need to use super to invoke a overridded method

Therefore, the inherited withdraw( ) method is inadequate and must be overridded:

public class SavingsAccount
       extends BankAccount
{   
    private double interestRate = 0.05;
    private final static int MAXNUMFREE = 3;
    private final static double PENALTY = 1.0;  
    private int numWithdrawals;
                                     
    public SavingsAccount(double x)
    {
        super(x);
    }

    public void withdraw(double amount)
    {
        super.withdraw(amount);

	numWithdrawals++;
	if ( numWithdrawals > MAXNUMFREE )
	    super.withdraw(PENALTY);
    }

    public void addInterest()
    {
        balance += interestRate/12*balance;
    }
} 
public class BankAccount
{
    private double balance;

    public BankAccount(double x)
    {
        balance = x;
    }

    public double getBalance()
    {
        return balance;
    }
    
    public void deposit(double amount)
    {
        balance = balance + amount;
    }
    
    public void withdraw(double amount) 
    {
        balance = balance - amount;
    }
}  



Because the variable balance is private, we must use the withdraw( ) method in the superclass to update balance

Example showing the need to use super to invoke a overridded method

When we withdraw from a SavingsAccount, we first perform the withdrawal:

public class SavingsAccount
       extends BankAccount
{   
    private double interestRate = 0.05;
    private final static int MAXNUMFREE = 3;
    private final static double PENALTY = 1.0;  
    private int numWithdrawals;
                                     
    public SavingsAccount(double x)
    {
        super(x);
    }

    public void withdraw(double amount)
    {
        super.withdraw(amount);

	numWithdrawals++;
	if ( numWithdrawals > MAXNUMFREE )
	    super.withdraw(PENALTY);
    }

    public void addInterest()
    {
        balance += interestRate/12*balance;
    }
} 
public class BankAccount
{
    private double balance;

    public BankAccount(double x)
    {
        balance = x;
    }

    public double getBalance()
    {
        return balance;
    }
    
    public void deposit(double amount)
    {
        balance = balance + amount;
    }
    
    public void withdraw(double amount) 
    {
        balance = balance - amount;
    }
}  



Next, we must keep track of the number of withdrawals made...

Example showing the need to use super to invoke a overridded method

We use the variable numWithdrawals to keep track of the number of withdrawals made:

public class SavingsAccount
       extends BankAccount
{   
    private double interestRate = 0.05;
    private final static int MAXNUMFREE = 3;
    private final static double PENALTY = 1.0;  
    private int numWithdrawals = 0;
                               
    public SavingsAccount(double x)
    {
        super(x);
    }

    public void withdraw(double amount)
    {
        super.withdraw(amount);

	numWithdrawals++;
	if ( numWithdrawals > MAXNUMFREE )
	    super.withdraw(PENALTY);
    }

    public void addInterest()
    {
        balance += interestRate/12*balance;
    }
} 
public class BankAccount
{
    private double balance;

    public BankAccount(double x)
    {
        balance = x;
    }

    public double getBalance()
    {
        return balance;
    }
    
    public void deposit(double amount)
    {
        balance = balance + amount;
    }
    
    public void withdraw(double amount) 
    {
        balance = balance - amount;
    }
}  



Finally, we check for penalties...

Example showing the need to use super to invoke a overridded method

We deduct PENALTY when the number of withdrawals exceeds the # free withdrawals:

public class SavingsAccount
       extends BankAccount
{   
    private double interestRate = 0.05;
    private final static int MAXNUMFREE = 3;
    private final static double PENALTY = 1.0;  
    private int numWithdrawals = 0;
                               
    public SavingsAccount(double x)
    {
        super(x);
    }

    public void withdraw(double amount)
    {
        super.withdraw(amount);

	numWithdrawals++;
	if ( numWithdrawals > MAXNUMFREE )
	    super.withdraw(PENALTY); // penalty
    }

    public void addInterest()
    {
        balance += interestRate/12*balance;
    }
} 
public class BankAccount
{
    private double balance;

    public BankAccount(double x)
    {
        balance = x;
    }

    public double getBalance()
    {
        return balance;
    }
    
    public void deposit(double amount)
    {
        balance = balance + amount;
    }
    
    public void withdraw(double amount) 
    {
        balance = balance - amount;
    }
}  



I have a test program in the next slide....

Test program for SavingsAccount

public class myProg
{
    public static void main(String[] args)
    {
        SavingsAccount a = new SavingsAccount(100);
        
        a.withdraw(10); 
        System.out.println(a.getBalance());  // 90
        a.withdraw(10); 
        System.out.println(a.getBalance());  // 80
        a.withdraw(10); 
        System.out.println(a.getBalance());  // 70
        a.withdraw(10); 
        System.out.println(a.getBalance());  // 59 ! ($1 penalty)
    }
}