File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interfaces" Watch "Interfaces" New topic
Author

Interfaces

Vamshi Vrukodar
Greenhorn

Joined: May 25, 2003
Posts: 6
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
Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
a.m1() will invoke the m1() method defined in InterfaceExample and return the s1 that class InterfaceExample sees.


Meng Yi
Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
try out the code below and you will see the difference:

It will print BA
Vamshi Vrukodar
Greenhorn

Joined: May 25, 2003
Posts: 6
hi Yi Meng,
Thank you...I understood the concept.
Regards
Saket Barve
Ranch Hand

Joined: Dec 19, 2002
Posts: 229
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
Kaz Yosh
Ranch Hand

Joined: May 22, 2003
Posts: 63
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.


"If anything can go wrong, it will"
Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interfaces
 
Similar Threads
Interface Question
String buffer
Please explain me the output in detail
StringBuffer question
clarification