From textbook examples, we know that we can create multiple inheritance by mixing parent class and interfaces. But hardly is there a descriptive section that details what exactly happens when a child interface is inheriting a parent interface. This is syntactically correct in... say Java.
public interface ParentInterface {
public void DoSomething();
public void DoingSomething();
}
public interface ChildInterface extends ParentInterface {
public void DoSomething();
}
You will pass the compilation for sure.
Now let's take a look at the class that 'implements' the interface.
public class SampleClass implements ChildInterface {
public void DoSomething()
{
}
public void DoingSomething()
{
}
}
If you take out DoingSomething(), your compiler will flip out and blast at you.
So SampleClass needs to implement the functions in ChildInterface as well as the functions in ParentInterface. And guess what, it is most likely be calling ChildInterface's DoSomething (instead of ParentInterface's DoSomething, it doesn't matter, since there's no body anyway).
Now, if you have another class, SampleClass2, that implements ParentInterface, guess what ..
public class SampleClass2 implements ParentInterface {
@Override
public void DoingSomething() {
// TODO Auto-generated method stub
}
@Override
public void DoSomething() {
// omitting this method would give you compilation error! But this was previously declared only in ChildInterface! // TODO Auto-generated method stub
}
}
Now in MainDriver
public static void main(String[] args) {
// TODO Auto-generated method stub
ParentInterface sc = new SampleClass();
ParentInterface sc2 = new SampleClass2(); //ChildInterface sc2 would give you error
}
Both sc and sc2 would allow you to call .DoSomething and .DoingSomething from ParentInterface.
In this case, ChildInterface has been completely ignored. So, polymorphism preserves but inheritance is broken(as there's nothing being reused from the ChildInterface at all).
As for practicability, this maybe of little value. But, a good point is that you only need to write 'implements ChildInterface' instead of 'implements ChildInterface, ParentInterface', but that's... not valuable at all in terms of OO.
No comments:
Post a Comment