• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interface Q

 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the follwing code:

1. interface Beta {}
2.
3. class Alpha implements Beta
{
4. String testIt()
{
5. return �Tested�;
6. }
7. }
8.
9. public class Main1
{
10. static Beta getIt()
{
11. return new Alpha();
12. }
13.
public static void main( String[] args )
{
14. Beta b = getIt();
15. System.out.println( b.testIt() );
16. }
17. }

What is the result?
A. Tested
B. Compilation fails.
C. The code runs with no output.
D. An exception is thrown at runtime.

Answer: B

On compiling, I got:

C:\>javac Main1.java
Main1.java:20: cannot resolve symbol
symbol : method testIt ()
location: interface Beta
System.out.println( b.testIt() );

1 error

Now, my question is:
what method is invoked when the compiler comes across the statement b.testIt(); As far as I understand, method invocation for non-static methods depends on the class of the reference variable at runtime, which in this case, I believe, is Alpha (since the method getIt actually returns an Alpha object - return new Alpha());
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, the method from the actual class gets executed BUT you've cast the class instance to the interface and the interface doesn't have the class.
Therefore the method isn't accessible to the reference which you defined as being of the interface.
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As far as I understand, method invocation for non-static methods depends on the class of the reference variable at runtime, which in this case, I believe, is Alpha (since the method getIt actually returns an Alpha object - return new Alpha());


The method invocation for non-static methods depends on the object its pointing to, the object created here is of type Alpha, so Alpha's method should be called during run time.

Now coming to your question:
All the method invocation happens during run-time, but during compilation the compiler checks to see if the reference type contains that method or not. So, in ur case the reference type is Beta, but Beta doesnt decalre the method testIt(), thats why it results into compilation problems.
Hope thats clear,
well, more discussion on this can be viewed on accessing this link
Here
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic