Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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 ajoutée par CHANNA MALLAIAH UDDANDAIAHGARI , Senior Software Engineer , Amadeus Labs
Date de publication: 2015/08/24
Muhammad Iqbal
par 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

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?