Java Reference
In-Depth Information
Example 4-15. A parent class that overrides the default implementation of welcome
public
public class
class OverridingParent
OverridingParent extends
extends ParentImpl {
@Override
public
public void
void welcome () {
message ( "Class Parent: Hi!" );
}
}
Example 4-16. An example of a concrete method beating a default method
@Test
public
public void
void concreteBeatsDefault () {
Parent parent = new
new OverridingParent ();
parent . welcome ();
assertEquals ( "Class Parent: Hi!" , parent . getLastMessage ());
}
Here's a situation, presented in Example 4-18 , in which you might not expect the concrete
class to override the default method. OverridingChild inherits both the welcome method
from Child and the welcome method from OverridingParent and doesn't do anything it-
self. OverridingParent is chosen despite OverridingChild (the code in Example 4-17 ) ,
being a more specific type because it's a concrete method from a class rather than a default
method (see Figure 4-5 ).
Example 4-17. Again, our child interface overrides the default welcome method
public
public class
class OverridingChild
OverridingChild extends
extends OverridingParent implements
implements Child {
}
Example 4-18. An example of a concrete method beating a default method that is more spe-
cific
@Test
public
public void
void concreteBeatsCloserDefault () {
Child child = new
new OverridingChild ();
child . welcome ();
assertEquals ( "Class Parent: Hi!" , child . getLastMessage ());
}
Search WWH ::




Custom Search