interface Device
{
public void doIt();
}
public class Electronic implements Device
{
public void doIt()
{
}
}
abstract class Phone1 extends Electronic
{
}
abstract class Phone2 extends Electronic
{
public void doIt(int x)
{
}
}
class Phone3 extends Electronic implements Device
{
public void doStuff()
{
}
}
Can any one tell me why this compiles.. Because "Phone3" implements Device and it should have doIt() method but it does not have. But still this compiles. May i know Y?
Phone3 extends Electronic, and Electronic has the method doIt(), implementing the Device interface. The implementation of the doIt method is thus just inherited from the Electronic base class.
It makes sense if you make the example more realistic. Change Device to Ringable, with a ring method. Create a base class SimplePhone implementing Ringable, with an implementation of the ring method. And make a subclass of SimplePhone called BeautifulPinkPhone. The beautiful pink phone will be able to ring because it's just a simple phone with a pink color.
implements Device is redundant in class Phone3 definition. The class in inheriting the fact of implementing the Device interface from the Electronic class.
That is, every class extending Electronic is implementing Device also, and is also inheriting the implementation of doIt that Electronic provides. Every one of them can extend/provide a different implementation of doIt by overriding it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With