Let's take a glance at the following code in Java.
interface FirstInaterface
{
public void show();
}
interface SecondInterface extends FirstInaterface
{
@Override
public void show();
public void someConcreteMethod();
}
interface ThirdInterface extends SecondInterface
{
@Override
public void show();
}
final class Demo implements ThirdInterface
{
@Override
public void show()
{
System.out.println("The show() method invoked");
}
@Override
public void someConcreteMethod()
{
System.out.println("The someConcreteMethod() method invoked");
}
}
final public class Main
{
public static void main(String[] args)
{
Demo demo=new Demo();
demo.show();
demo.someConcreteMethod();
}
}
The above code in Java shows a multilevel inheritance of interfaces in which ThirdInterface has two interfaces above it. The show() method is firstly being overridden in the interface SecondInaterface ahd then again in the interface ThirdInterface and this interface is finally being inherited by the class Demo.
In such a scenario, which version of the show() methods from the above interfaces will be incorporated by the class Demo? How is the compiler able to resolve a specific version of the show() method dynamically at run time?
I feel that the show() method in the last interface in the above interface inheritance hierarchy (namely ThirdInterface) will be invoked by the compiler. If it is so then, the show() methods in the above two interfaces (namely, FirstInaterface, SecondInaterface) are useless and serve no purpose at all, since they are declared within interfaces, they themselves can never have their own implementations anywhere. When such kind of interface inheritance is useful?
showMethod()
of the object will get invoked. So here object is of classDemo
so its version will be taken.also at compile time
Demo
class need to have declaration ofshow
andsomeConcreteMethod
method otherwise it will turn into compile time errorIn interface you actually can't provide the implementation
Yes it is just declaration.. also it implicitly inherit from the parent interface. so if you write it or don't it doesn't make any difference
The compiler will not choose the
show()
method of any interface : it will call the method of the class, that's all. The interfaces just tell that a show method must be implemented in the class.Overriding a method in an inteface is useless (except maybe if you add annotations). Interface inheritance is only useful if you add methods.
Only method implementations can override, not method specifications in an interface.
What "versions"? The method signatures are identical, so there is really only one method specification. At runtime, there will be an implementation class, and only the method implementation in that class is what gets actually called.
Um, what about classes that implement
FirstInterface
directly? If anything, what serves no (technical) purpose is "overriding" methods in child interfaces. IfSecondInterface
andThirdInterface
did not have ashow()
method, it would have exactly the same syntactical effect.However, there can be a valid reason to override methods in a child interface: to provide a different comment explaining semantic changes in the method contract. Examples for this can be seen in the collections framework of the Java API:
Set.add()
has a different contract thanCollection.add()
, since it adds the restriction that duplicate elements should not be added.The only use I can think of in overriding superinterface methods in subinterfaces is documentation. This way, you see at one glance which methods this interface requires, and do not have to check in superinterfaces to find additional methods. It also might make sense if you want to clarify the use of method for this subinterface in contrast to the use in the superinterface or in other subinterfaces.
For the compiler there is no difference at all if you override those methods or not.
Of course, if you would use AOP languages as AspectJ, you could inject an implementated method into an interface, and this question might become relevant. In those cases the compiler would take the first implementation it comes accross in the interface hierarchy starting with the interface your class implements.
the
show()
method ofDemo
will be invoke because when you writeDemo demo=new Demo();
then at the run time JVM run method from class.... interface are just contract .... they can't give implementationif u do not override
show(); and someConcrete()
then it will show an compile time error...these type of implemention are use full because of reuse ability that means there is some behavior in fist Interface , some in 2nd something in 3rd ... and if u want a class which have all these functionality and some extra then u have 2 way..
1. make another interface having all those functionality .Or
2. make a interface and inharite it through those interface which have some functionality and add some extra in your interface..