• Post Reply Bookmark Topic Watch Topic
  • New Topic

objects of an interface type  RSS feed

 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im confused(again)

i just finished refactoring an applet i wrote in 2000. the last compiler error had to do with an AudioClip. it compiles now but im confused how this works. wikipedia says "Object references in Java may be specified to be of an interface type; in which case, they must either be null, or be bound to an object that implements the interface." but i dont see how the following code fits either criteria. this code is from the old applet because i havent actually run the new one yet(its a long story).


 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if whatever the "fm" object is has a getAudioClip method defined as returning a reference to an implementor of AudioClip, whats the problem?

Bill
 
Walter Gabrielsen Iii
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AudioClip is an interface, correct?

So, what's happening with the AudioClip object reference, named "sheep," is that another class has implemented AudioClip and that child or sub class is being assigned to reference "sheep."

This is legal in Java because of object inheritance. A child subclass can pretend that it's the same type of object as its parent superclass. It works even if the parent type is an interface.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is legal in Java because of object inheritance. A child subclass can pretend that it's the same type of object as its parent superclass. It works even if the parent type is an interface.

No, it's inheritance of Type. An interface is never a 'superclass' and it's usually wrong to think of a superclass or implemented interface as a parent.

Puppy could be an instance, but not a subclass of Dog. On the other hand, Labrador, Poodle and Terrier could all be subclasses of Dog.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!