Java Reference
In-Depth Information
}
//Child.java
public class Child extends Parent {
public FileOutputStream doSomething(int y, String s) {
//do something else
}
}
Covariant return types are not allowed for primitive types, only Object types. The
following code does not compile:
//Parent.java
public class Parent {
public int doNothing() {
return 0;
}
}
//Child.java
public class Child extends Parent {
public short doNothing() { //not valid!
return 1;
}
}
The following compiler error is generated:
Child.java:2: doNothing() in Child cannot override doNothing() in
Parent; attempting to use incompatible return type
found : short
required: int
public short doNothing() {
^
Covariant return types are yet another new concept introduced in Java 5.0,
so expect at least one question on the exam that involves understanding
how they work.
Method Hiding
Method hiding occurs when a child class contains a static method that is also defi ned in
its parent, using the same rules of instance method overriding discussed earlier. If a static
method in a child class contains the same static method as its parent class, then the method
in the child class hides the method in the parent class but does not override it.
Search WWH ::




Custom Search