Week 4: Inheritance

Download Report

Transcript Week 4: Inheritance

OO terminology & objectives
 Encapsulation


don’t touch my private data
get/set it using my public/package methods
 Inheritance


a parent provides for her/his children
children add to a parent’s knowledge
 Polymorphism

Java figures out family relationships:
when the parent knows best or
when the child knows more than the parent
Inheritance in Java
super class or parent, subclass or child
adds functionality to an existing class
class Child extends Parent
C++ multiple inheritance … NOT!

single parent families only in Java
use of the keyword super

not this object’s
constructor/variable/method,
but the one from the super class.
The Object Class
 “The Ultimate Superclass”
- all classes in Java extend the Object class
 Example (an array of generic objects):
ObjectDemo.java
How to override a parent’s
method in the child class?
use the same …



method name
signature (parameter list)
return type
different code in the child’s method overrides
parent's logic
 e.g. equals(), toString()
 access level can be increased (e.g. private to public)
 a different signature: overloads the method
 a different return type: compiler error
Example: Inherit.java, ShapeTest & docs
Polymorphism
 the same method call on two objects in the same
“family” (inheritance branch) can give different results
 Which method is actually called at run time?
 method(signature) in this object’s class
 else look up the super (parent) class tree
 run-time/dynamic binding determines which one
 compile time: the declared type of an object reference
Object x = new BigDecimal("123.45");
 run-time: the actual type of the object referred to
determines behaviour. It can be the declared class type
or one of its sub classes (inheritance).
x.toString() calls BigDecimal class method, not Object's
but it compiles because Object x has toString().
Polymorphism
 a final method: dynamic binding is turned off
 sub (child) classes can inherit but not override any
final super (parent) method
 like when your parent said, “…and that’s final!”.
 a method of limiting inheritance
Casting and Inheritance
 casting up:
superclass = subclass;
// superclass: the declared type of the variable is the superclass
// subclass: the declared type of the variable is the subclass
 casting down:
subclass = (subclass) superclass;
// a promise to the compiler
// the validity of “casting down” is checked at run time
 compile-time error:
subclass = superclass;
 Examples: Inherit2.java, Inherit3.java
Abstract Class: An Example
public abstract class Aclass {
private int m; // instance variable to be inherited
// implemented and to be inherited
public void set( int t ) { m = t; }
}
// abstract methods declared, not implemented
// must be defined by extending class
public abstract void show();
public abstract void change();
The Subclasses of an Abstract Class
public class Bclass extends Aclass {
… // inherit Class Aclass’s set method
public void show ( ) { … }
// implement details
public void change ( ) { … } // implement details
}
public class Cclass extends Aclass {
… // inherit Class Aclass’s set method
public void show( ) { … }
// class C version
public void change ( ) { … }
// class C version
}
What is an abstract class?
 an abstraction(i.e. generalization) of subclasses
- a class hierarchy is enforced
 abstract methods: the developers of the subclasses
MUST customize the code for these methods
 cannot be used to create an object instance
Aclass aClassRef; // object reference is OK
aClassRef = new Aclass(); // but cannot construct
 in practice: an abstract class has a mix of instance
variables, implemented and abstract
methods
Interfaces in Java
What is an interface?
How to declare an interface?
How to implement an interface?
How to use an interface?
What is an interface?
syntax:
a declaration of methods
(that are not implemented yet)
software development viewpoint
- an agreement on how a “specific” object
can be used by other objects
- a promise to other software developers that all the
methods will be implemented by a class
cannot be used to create an object
 static final variables may be included
(e.g. the Adjustable interface)
How is an interface used?
used as a reference type
- Example 1: DigitalPlayer.java, CDPlayer.java, TestPlayer.java
- Example 2: FoodProcessor.java
Advantage: the client code will not be broken(i.e. affected) by
a different implementation of the interface
 e.g. implements Comparable … int compareTo(Object obj)
see API for Comparable interface
event handling(GUI programming)
- Example: the ActionListener interface
 a class may implement many interfaces
( vs extending just one [abstract] class)
 an interface can extend one or more interfaces
-
Inner Classes
a class that is defined inside another class,
called the outer or enclosing class.
access privilege to members of the outer
class: fields, methods, other inner classes
can access outer class’s private instance
members
mostly used for event-driven programs
i.e. GUI programming
Example: Outer.java & OuterInnerTest.java
Anonymous Inner Classes
anonymous inner class definition (i.e. an
inner class that does not have a name)
- notational convenience: event handling
code can be put close to where GUI
objects are created
Anonymous Inner Classes
an anonymous inner class that
implements an interface
an anonymous inner class that
extends a class
Example: Anonymous.java
Inner Classes
inner classes than implement an interface
Example 1: PhotoCopier.java
anonymous inner class as used for GUI
Example 2: JFrameDemo.java
Rationale: coding convenience