• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Interfaces

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am new to this group. I am preparing for the programmers certification exam. I had a doubt regarding Interfaces. If u run the following program,,,
interface A {
String s1 = "A";
String m1();
}
interface B extends A {
String s1 = "B";
String m1();
}
class InterfaceExample implements B {
public String m1() {return s1;}
public static void main(String[] args) {
A a = new InterfaceExample();
System.out.print(a.m1());
}
}
The output is B. But I thought, since a is of type Interface A, the string returned will be that declared in Interface A. Can anyone please give an explanation for the output.
Thank you
Vamshi
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a.m1() will invoke the m1() method defined in InterfaceExample and return the s1 that class InterfaceExample sees.
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try out the code below and you will see the difference:

It will print BA
 
Vamshi Vrukodar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Yi Meng,
Thank you...I understood the concept.
Regards
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yi,


a.m1() will invoke the m1() method defined in InterfaceExample and return the s1 that class InterfaceExample sees.


I'm not clear yet. If the InterfaceExample sees "s1" of interface B, why would it print the value of s1 in interface A for the
System.out.println( a.s1 )?
Saket
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because when you look up the variables, object is looking for the variable with the same type. In this case, object type is A and actual object is interfaceexample. So the variable you are looking for is type A's variable which is "A" in interface A.
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Saket Barve,
Actually at compile time, the compiler will only look for methods/variables of the reference type class/interface. e.g. if the method m1 is not in interface A, it will simply report a compiler error despite that the method m1 is in the class InterfaceExample.
a.m1() and a.s1 all make references to those in interface A, but for the method, dynamic binding is applied as the method m1 is properly overriden. The actual method binded will depend on the actual object rather than the reference type.
[ May 26, 2003: Message edited by: Yi Meng ]
 
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic