Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

Very Important Basic thing in Java.?

If one interface having same variables with different data and i am implementing both interfaces in Sub Class. now it is giving compilation error that ambiguity in variable name.

Why it is not happening if we declare same method names in both interfaces and implement in our sub class?

 

Example:

 

 

interface S1{

int num=10;

}

interface S2{

int num=20;

}

class Sample1 implements S1,S2{

ps v m(){

.

}

 

o/p : Now it will give compilation error , bcoz same variable names in both interfaces.

 

interface S3{

int doSum();

}

interface S4{

int doSum();

}

Class Sample2 implements  S3,S4{

public int doSum(){

.

return3;

}

psvm(){

.

}

 

o/p now it wont give any compilation error.

 

 

user-image
Question added by CHANNA MALLAIAH UDDANDAIAHGARI , Senior Software Engineer , Amadeus Labs
Date Posted: 2015/08/24
Muhammad Iqbal
by Muhammad Iqbal , Java Engineer , Celerant Technology

"ambiguity in variable name.Why it is not happening"The ambiguity is not really in the variable *name* - its in the value. Which value to use?For methods - they have no 'value' since no implementation - except in the class, and there you have just a single implementation, so no ambiguity.In Java8 with default implementations in interface methods, I guess there you can get ambiguity.

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.