Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in interface variables..

 
Sriram.Narayanan Thiagarajan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

interface I {int interfaceConstant =10;}

interface I1 extends I {int interfaceConstant=12;}

public class A implements I1
{
public static void main(String s[])
{
System.out.println(interfaceConstant);
}
}

output - 12

I don't understand why the o/p is 12

 
Naveen Zed
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by T.Sriram.Narayanan Thiagarajan:
Hi All,

interface I {int interfaceConstant =10;}

interface I1 extends I {int interfaceConstant=12;}

public class A implements I1
{
public static void main(String s[])
{
System.out.println(interfaceConstant);
}
}

output - 12

I don't understand why the o/p is 12



I think the cause is due to the overriding between the inherited interfaces
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java variables overriding is not possible.
In second interface you are redeclaring interfaceConstant variable
or you can say you are hidding first interfaceConstant by second interfaceConstant variable this is why you get output - 12
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

check code Above.
 
Sriram.Narayanan Thiagarajan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interface variables are implicitly final
Then how come we redeclare an interface variable
 
Awais Sheikh
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both Interface varibles are declaring, intializing and working seperately.
and there is not link b/w them. So which interface variable you will call you will get that value.
Hope you understand now.
thanks
 
Sriram.Narayanan Thiagarajan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
understood

thanks Awais Sheikh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic