Java Reference
In-Depth Information
abstract method in a class can override an abstract method in its superclass without
providing an implementation. The subclass abstract method may refine the return type or
exception list of the overridden abstract method. Let's consider the following code. Class B
overrides the abstract m1() method of class A and it does not provide its implementation.
It only removes one exception from the throws clause. Class C overrides the m1() method of
class B and provides implementation for it. Note that the change in return type or exception
list as shown in m1() method for class B and class C must follow the rules of method overriding.
An
public abstract class A {
public abstract void m1() throws CE1, CE2;
}
public abstract class B extends A {
public abstract void m1() throws CE1;
}
public class C extends B {
public void m1() {
// Code goes here
}
}
A concrete instance method may be overridden by an
abstract instance method. This can
be done to force the subclasses to provide implementation for that method. All classes in Java
inherit equals() , hashCode() , and toString() methods of the Object class. Suppose you
have a class CA and you want all its subclasses to override and provide implementation for the
equals() , hashCode() , and toString() methods of the Object class. You need to override
these three methods in class CA and declare them abstract, like so:
public abstract class CA {
public abstract int hashCode();
public abstract boolean equals(Object obj);
public abstract String toString();
// Other code goes here
}
In this case, concrete methods of the
Object class have been overridden by abstract
methods in the CA class. All concrete subclasses of CA are forced to override and provide
implementations for the equals() , hashCode() , and toString() methods.
Method Overriding and Generic Method Signatures
Java 5 introduced the concept of generic types. If you are using a version of Java prior to Java 5, this section does not
apply. Java 5 lets you declare generic methods. When Java code with generics types is compiled, the generic types are
transformed into raw types. The process that is used to transform the generic type parameters information is known
as type erasure. Let's consider the GenericSuper class in Listing 16-41. It has a generic type parameter T . It has two
methods, m1() and m2() . Its m1() method uses the generic type T as its parameter type. Its m2() method defines a new
generic type to use it as its parameter type.
 
Search WWH ::




Custom Search