• 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 question??

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


How come the above works and prints amethod as O/P??? We are using the interface reference to call the method in the class that implements the interface??? Is this legal??
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Why do you think its illegal.Because
Easingwold IS-A Coxwold

and hence it works.

Hope This helps
Prashanth
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By all means this is legal.
Its a IS-A kind of relationship.

Anything which is "UP" in the inheritance hierarchy can always refer everything which is "DOWN" in the hierarchy.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At runtime the JVM looks for the actual object being refered by c, which is actually the object of Easingwold. Thus it successfully invokes amethod().
 
Saurabh Vyas
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This example may give a better idea.

 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys, I got it.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ,
At compile time compiler checks the reference type and methods that is invokes on that reference variable. Means compiler will check that amethod() is declare in interface or not if not iwill check its super if possible.But here you have declare this one. So no compilation error will be generated . And
At Runtime JVM checks object type and method that is invoke,is it inside that class or not.but you have have defined that (amethod ()) in that class .So at runtime there will no exception, So code will be run.

Thanks
Prashant Kumar Singh
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1. interface Coxwold{
2. public void amethod();
3. }
4. public class Easingwold implements Coxwold{
5. public static void main(String argv[]){
6. Coxwold c = new Easingwold();
7. c.amethod();
8. }
9. public void amethod(){
10. System.out.println("amethod");
11.}
12.}


( 6 )will be checked at the compile time for.. whether Easingwold IS-A Coxwold
( 7 )compilation fails if Coxwold does not know about the method..means the method is not declared in the interface.
Both of the condition does passes hence will print amethod.
[ November 06, 2006: Message edited by: Sanjeev Kumar Singh ]
 
Hey cool! They got a blimp! But I have a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic