Classes and Inheritance III

In this lecture you will continue to learn about the inheritance.
You should read the textbook p204-209 or
Java tutorial The topics covers

Three programs.

We are going to use these 3 programs to illusatete the idea.
Download the programs here
BankAccount.java
, CheckingAccount.java , SavingAccount.java .
Let's browse the program:
BankAccount , CheckingAccount , SavingAccount .

Subclass

Definition:  A subclass is a class that extends another class. A subclass inherits state and behavior from all of its ancestors.
Example: 
the class CheckingAccount has the following public methods:
public void deposit(int depositAmount) <--
public void withdraw(int withdrawAmount) |______ from BankAccount
public int getSaving() |
public int getName() <--
public int getMonthCharge() <--------- from CheckingAccount
public String showInfo() <--------- override
the class SavingAccount has the following public methods:
public void deposit(int depositAmount) <-- 
public void withdraw(int withdrawAmount) |______ from BankAccount
public int getSaving() |
public int getName() <--
public double getInterest()
public static double getInterestRate()
public static void setInterestRate(double newInterestRate)
public String showInfo() <--------- override

What Members Does a Subclass Inherit?

How to declare subclass

public class CheckingAccount extends BankAccount
| |
| |
| |
subclass superclass

protected

public class Alpha {
protected int iamprotected;
protected void protectedMethod() {
System.out.println("protectedMethod");
}
}

Overriding Methods

You can implement a method in the subclass with the same method names, return type and arguments as method in the superclass.
Example
in BankAccount , there is a method
public String showInfo()
In both CheckingAccount and SavingAccount , there are methods with the same return type (String), same method name (showInfo) and same argument list ( nothing).
This is called overriding.

Methods a Subclass Cannot Override

Methods a Subclass Must Override

We will discuss the concept about abstract later.

Use of final

Reason: for security.  Hackers extend some of your importants classes and substitute their classes for the origin.
Example : String class is a final class.

Example on final

public class FinalTestA {
public final int finalInteger = 1;
public final void finalMethod() {
System.out.println("Final method");
}
}

public class FinalTestB {
public static void main(String[] args) {
FinalTestA a = new FinalTestA();
a.finalInteger = 2; // <--- illegal, final variable cannot be changed
}

public class FinalTestC extends FinalTestA {
public void finalMethod() { // <----illegal, final method cannot be overriden
System.out.println("Final method in FinalTestC");
}

public void finalMethod(int a) { // <------legal, this is overloading
System.out.println("This number is " + a);
}
}

one more example on final

public final class FinalTestD {
public int finalInteger = 1;
public void finalMethod() {
System.out.println("Final method");
}
}

public class FinalTestE extends FinalTestD {  // <---- illegal, cannot extend  
    // nothing                                //                 final class
}

How to use super

There are 2 ways to use super. We can use it in constructor to call the superclass's constructor.
Example
public CheckingAccount(String name,  int saving, boolean interest) {
super(name, saving);
this.interest = interest;
}
public SavingAccount(String name,  int saving) {
super(name, saving);
}
The line super(name, saving) calls the constructor BankAccount(String name, int saving) and initiate the name and saving inherit from the superclass BankAccount .

Note
:
You must put call the superclass' constructor on the first line!

Another way to use super

You can use super to call superclass ' protected or public members. It is particular useful for calling overriding method.

Example

In SavingAccount we have
return super.showInfo() + "\nInterest Rate : " + interestRate;
In CheckingAccount we have
return super.showInfo() + "\n" + newLine;

Summary

After this lecture. You should know

Things you havn't learned

There are still lot of things you havn't learned. I will cover them in the coming lectures or in Pic20B.
But right now, let's take a break and see how to use Java to write cool programs.