-
Inheritance allows a software developer to derive
a new class from an existing one, for the purpose of reuse, enhancement,
adaptation, etc.
-
superclass (a.k.a. base class)
-
subclass (a.k.a. derived class, extended class)
-
Inheritance models the is-a relationship.
-
If class E is an extended class of class B, then
any object of E can act-as an object of B.
-
Examples: Book.java,
Dictionary.java,
Words.java
-
Protected visibility
-
Super constructor
-
Examples: Book2.java,
Dictionary2.java,
Words2.java
-
Overriding
-
Super reference
-
Examples: Book3.java,
Dictionary3a.java,
Dictionary3b.java, Books.java
-
Single vs. multiple inheritance
-
A class may have only one superclass.
-
Overloading vs. overriding
-
Overloading
More than one methods have the same name
but different signatures
-
Overriding
Replacing the implementation of a methods
in the superclass with one of your own.
-
You can only override a method with the same signature.
-
You can only override instance methods.
-
The Object class
-
The root of Java class hierarchy
-
Defines some common methods:
-
public String toString()
-
public boolean equals(Object other)
-
If a class does not explicitly declare a superclass,
its superclass is Object.
-
Abstract class
-
An abstract class is a class with partial implementation.
-
It implements behaviors that are common to all subclasses,
but defers to the subclasses to implement others (abstract methods).
-
An abstract class cannot be instantiated
-
The subclass of an abstract class must override the
abstract methods of the parent, or it too will be considered abstract
-
Interface
-
Interfaces are classes with no implementation.
-
Interfaces represent pure design.
-
Abstract classes represent mixed design and implementation.
-
An interface consists of only abstract methods and
constants, i.e., static and final.
-
All methods and constants are public.
-
No static methods.
-
An interface cannot be instantiated
-
Inheritance relation also applies to interfaces
-
superinterface and subinterface
-
Multiple inheritance allowed
-
An interface may extend multiple interfaces
-
A class my implement multiple interfaces
-
Type conversion
-
Implicit conversion
-
Numeric variables: Any numeric types can be converted
to another numeric type with larger range, e.g.
char ==> int, int ==> long, int ==>
float, float ==> double.
-
Object reference: An object reference of class C
can be converted to a reference of a superclass of C.
-
Explicit conversion (cast)
-
Numeric variables: Any numeric types can be explicitly
cast to any other numeric type. May lose bits, precision.
-
Object reference: Cast an object reference of a class
to a reference of any other class is:
-
syntactically allowed; but
-
runtime checked.
-
Cast object references:
class Student { ... }
class Undergraduate extends Student { ... }
class Graduate extends Student { ... }
Student student1, student2;
student1 = new Undergraduate(); // ok
student2 = new Graduate(); // ok
Graduate student3;
student3 = student2; // compilation error
student3 = (Graduate) student2; // explicit cast, ok
student3 = (Graduate) student1; // compilation ok, run-time error
-
A polymorphic reference is one which can refer to
different types of objects.
-
Example: Books2.java