Java Reference
In-Depth Information
The implication of declaring a class as abstract is that it cannot be instantiated. The following code will generate
a compile-time error:
new ABCImpl(); // A compile-time error. ABCImpl is abstract
The only way to use the ABCImpl class is to inherit another class from it and provide the missing implementations
for the m2() and m3() methods of the IABC interface. The following is the declaration for a new class DEFImpl , which
inherits from the ABCImpl class:
package com.jdojo.interfaces;
public class DEFImpl extends ABCImpl {
// Other code goes here
public void m2() {
// Code for the method goes here
}
public void m3() {
// Code for the method goes here
}
}
The DEFImpl class provides implementations for the m2() and m3() methods of the ABCImpl class. Note that the
DEFImpl class inherits the m1() , m2() , and m3() methods from its superclass ABCImpl and m3() . The compiler does not
force you to declare the DEFImpl class as an abstract class anymore. You can still declare the DEFImpl class abstract ,
if you want to.
You can create an object of the DEFImpl class because it is not abstract . What are the types of an object of the
DEFImpl class? It has four types: DEFImpl , ABCImpl , Object , and IABC . An object of the DEFImpl class is also of the
ABCImpl type because DEFImpl inherits from ABCImpl . An object of the ABCImpl class is also of type IABC because
ABCImpl implements IABC interface. Since a DEFImpl is an ABCImpl , and an ABCImpl is an IABC , it is logical that a
DEFImpl is also an IABC . This rule has been demonstrated by the following snippet of code. An object of the DEFImpl
class has been assigned to variables of DEFImpl , Object , ABCImpl , and IABC types.
DEFImpl d = new DEFImpl();
Object obj = d;
ABCImpl a = d;
IABC ia = d;
The Supertype-Subtype Relationship
Implementing an interface to a class establishes a supertype-subtype relationship. The class becomes a subtype of all
the interfaces it implements and all interfaces become a supertype of the class. The rule of substitution applies in this
supertype-subtype relationship. The rule of substitution is that a subtype can replace its supertype everywhere. Let's
consider the following class declaration for a class C , which implements three interfaces J , K , and L :
public class C implements J, K, L {
// Code for class C goes here
}
 
Search WWH ::




Custom Search