one class can implement multiple interfaces but it can't extend two calsses why??????
Printable View
one class can implement multiple interfaces but it can't extend two calsses why??????
Multiple inheritance, means one class directly can't extend two classes.
i want the exact reason for that??..what will be the problem if one class extends multiple calsses directly...one class can implement multiple interfaces directly right...
That is because to avoid dangers like ambiguity that is present in C++, you may know.
Thanks for your respone ......could you please brief me on that????what danger it is????
Oh dear, say ClassA extend from ClassB and ClassC. Say ClassB has a method sampleMethod() and ClassC has sampleMethod(), which method ClassA should call. Compiler really mess-up with it.
That's why SUNs' avoid this. So you can simply use interfaces. As I said there is a trick for multiple inheritance also.
Thanks yaar...Thanks a lot for your help...
Is that clear for you. If the question is solved don't forget what I say ;)
But what about interfaces???if two interfaces contains same method compiler won't mess-up...
Did you try it?
yes i tried it...it is not showing any compilation problem but the actual implemented method is not executing...could u please help me on this....
What you mean actual implemented method is not executing. I'm not clear with it. Do you have any code with you.
Sorry Its executing it is not showing any problems.
Ok, so you overloading methods, right?
Please have a look on this code:
public class Test implements Sample1,Sample2{
public static void main(String args[]){
System.out.println("public static void main");
Test t=new Test();
t.jen();
}
public void jen() {
System.out.println("Success");
}
}
Interface 1:
ublic interface Sample1 {
public void jen();
}
Interface:2
public interface Sample2 {
public void jen();
}