Transcript 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
"is a" test for inheritance/super class
a String is an Object (true)
an Object is a String (false)
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 is automatic:
Object object;
String string;
object = string; // because string IS AN object
casting down is up to the programmer:
string = (String) object;
promises compiler that, at run time, object will refer to a String
type.
compile-time error:
subclass = superclass;
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?
one or more interfaces can be implemented by a class
class MyClass implements Comparable<MyClass>
other classes can tell if your class has implemented an
interface
if (myClass instanceof Comparable){
then other classes know they can call interface methods
int compare=myClass.compareTo(otherMyClass)}
only your class knows how to implement those methods
see API for Comparable interface
event handling(GUI programming)
Example: the ActionListener interface
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
an inner class that does not have a name
notational convenience: event handling code can
be put close to where GUI objects are created
not recommended because
code is messy and dense
can only implement behaviour, i.e. override methods
must use default constructor, i.e. cannot have state