• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confused about this statements  RSS feed

 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • 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.
 
Hari r.haran
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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?
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!