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
- how to extend classes
- the keyword
protected
.
- override and overload methods
- other meanings of
final
- the keyword
super
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?
- Subclasses inherit those superclass members declared as
public
or protected
.
- constructors are not members and are not inherited by subclasses.
How to declare subclass
public class CheckingAccount extends BankAccount
| |
| |
| |
subclass superclass
protected
- To declare a protected member, use the keyword
protected
.
- the members can be accessed by its subclass and the class itself.
- the protected member can also be accessed by the classes in the same
package. We will learn about package later.
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
- A subclass cannot override methods that are declared
final
in the superclass.
Methods a Subclass Must Override
- A subclass must override methods that are declared
abstract
in the superclass, or the subclass itself must be abstract.
We will discuss the concept about abstract
later.
Use of final
- Final variables are constants, they cannot be changed.
- Final methods cannot be overriden by subclasses.
- Final class cannot be extended.
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
- the meaning of subclass and superclass
- how to use
extends
- the meaning of
protected
- what kind of variables and methods can be used by subclasses.
- the meaining of overriding and overloading.
- how to use
final
for variables, methods and classes
and its meaning.
- how to use
super
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.
- abstract classes
- package
- inner class
- interface
- throw an exception
- cast
- and more....
But right now, let's take a break and see how to use Java to write cool
programs.