aspose file tools*
The moose likes Beginning Java and the fly likes Class / Subclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class / Subclass" Watch "Class / Subclass" New topic
Author

Class / Subclass

leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327

Specifically, What is lime? Is it a Fruit reference to a Citrus object or the other way around?
Secondly, how many objects are created here?


Thanks, leo
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

lime is a Fruit reference to a Citrus object. The only object I see being created is the explicit Citrus one (it's possible that println() creates some objects behind the scenes, but I don't think you're asking about those.)


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
To say the same thing in a lot more words ... you have a variable declared with type Fruit. A variables that holds an object is really just a pointer, a little bit of information about where to find an object in memory. A variable like lime is allowed to point to an object exactly matching the declaration (Fruit) or any subclass (Citrus). When you call lime.method() you run the method on the actual object, whether it is Fruit or Citrus.
We often say the derived class "is a" base class, or "Citrus is a Fruit". That's not always 100% true but as a general concept it tells us we can make a variable of type Fruit point to an object of type Citrus because for this purpose a Citrus is a Fruit.
Hope that helped. Objects are tons of fun, no?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Bryan Noll
Greenhorn

Joined: Feb 06, 2004
Posts: 26
Good answers.
There's a couple good articles in the Campfire Stories area that explain these concepts well.
http://www.javaranch.com/campfire/StoryCups.jsp
http://www.javaranch.com/campfire/StoryPassBy.jsp
StoryPassBy is the specific one...but it helps to read StoryCups first.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Those are good articles, Brian. After reading them, be sure to follow up with the "How my Dog learned Polymorphism" article, which is right on topic.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class / Subclass