ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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
تم إضافة السؤال من قبل CHANNA MALLAIAH UDDANDAIAHGARI , Senior Software Engineer , Amadeus Labs
تاريخ النشر: 2015/08/24
Muhammad Iqbal
من قبل 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.

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟