• 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

Confused about this statements

 
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Quest Interface:


The HolyGrailQuest Class that implements it :


The KnightOfTheRoundTable class that owns the HolyGrailQuest Class :


My main concerns in the KnightOfTheRoundTable Class :



Can we instantiate an interface? I can't understand this "instantiation" even if Quest was implemented directly by KnightOfTheRoundTable. But what I have here is Quest that's implemented by a subclass. That's just beyond comprehension.



Another wonder of the world. How can an interface be a container of an instance of a class?

Help here. Thanks.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I understood the confusion that you have.

Well, those type of statements are allowed in Java. The first line



is a declaration, whereas



is initialization.

An interface is nothing but an 100% abstract class. During the declaration part, you are declaring an object that may contain any class that implements the interface. We used to tell that the classes or interface that have inheritance as their relationship, is having a "IS-A" relationship. For example, if I have a class named Bird, and if i inherit this class in "Pigeon" class, then we tell that "Pigeon is-a bird".

This will be useful in accessing a common method, that are present in all the subclasses. Again for example, if i have one more class in the above example, say, "Parrot", and if i want to call the fly() [assume we have it] method in both the classes, it is efficient to use a Bird class object [even if it is an interface], and store an instance of Parrot or Pigeon. At runtime, the JVM decides the object to invoke the method.

This is runtime polymorphism.
 
Hari r.haran
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


At runtime, the JVM decides the object to call.

Regards,

Hari
 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.
Ok, it works for inheritance (IS-A). But in this case, the relationship is (HAS-A), an inner class. Does this rule still apply for this condition? thanks.

In your code above, I conclude this. the fly method called belongs to the last object initialized to the Bird object. For example :


then the invoked fly would belong to Falcon. The previous four birds are removed. is this right?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not exactly. That code assigns (not initializes) several objects to that Bird variable named "obj" (not Bird object), one after the other. So after line 6, that variable contains a reference to a Falcon object. So when you call the "fly" method of that variable, the "fly" method of the Falcon object to which it refers is called.

Those other four objects which were created, and now have nothing referring to them? No, they aren't "removed". They just sit there in memory until the garbage collector runs and notices they have nothing referring to them. This may happen at some time in the future but nothing is guaranteed except that the garbage collector will remove them from memory before the program runs out of memory.
 
WARNING! Do not activate jet boots indoors or you will see 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