aspose file tools*
The moose likes Beginning Java and the fly likes Dot Syntax 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 "Dot Syntax" Watch "Dot Syntax" New topic
Author

Dot Syntax

Nick Delauney
Ranch Hand

Joined: Sep 28, 2002
Posts: 43
When using dot syntax ? Is the Parent class object created and the derived class object. For Example:
Cat.Tiger.meow();
For us to get to Tiger. Does Cat have to be instantiated ? or just Tiger ?
Thank you for any help.
[ April 19, 2003: Message edited by: Nick Delauney ]

N.D:"Anything worth having, takes time to get"
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
When using dot syntax ? Is the Parent class object created and the derived class object. For Example: Cat.Tiger.meow();
The dot does not refer to the "is a" relationship, it refers to the "has a" relationship. For example, if class Tiger has a method meow(), then you would invoke it using tiger.meow(). If your Tiger class is derived from Cat, you have two alternatives:
1. Define the implementation of meow() in Cat.
2. Define the implementation of meow() in Tiger, or override the base class implementation.
But in both cases, if you want your tiger to meow, you would say:
Cat tiger = new Tiger(); // or Tiger tiger = new Tiger();
tiger.meow();
Eugene.
[ April 19, 2003: Message edited by: Eugene Kononov ]
Nick Delauney
Ranch Hand

Joined: Sep 28, 2002
Posts: 43
I read your the tutorial at http://www.javaranch.com/campfire/StoryPoly.jsp
Let me ask then, is it not always better to just say Cat tiger = new Tiger();
or is there a better time to use:
Tiger tiger = new Tiger();
Also, how would I use a method of the superclass
from an object in a child class that has that method overridden ?
Thanks, anyones reply is welcome
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Nick,

Let me ask then, is it not always better to just say Cat tiger = new Tiger();
or is there a better time to use:
Tiger tiger = new Tiger();

That really depends on what you are trying to do. From a purely design point of view it is usually better to use a more general class but that's not always practical. If you only need the behavior defined in the parent class Cat then the better design choice would be to declare the reference as a Cat instead of a Tiger. But if Tiger has defined methods or publically available state not in the parent class and you need to use that extra behavior then you must define the reference as a Tiger.

Also, how would I use a method of the superclass
from an object in a child class that has that method overridden ?

If as Eugene said, the Cat class has a meow() method and Tiger overrides the method then you can access Cat.meow() from a Tiger object by calling super.meow().


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If as Eugene said, the Cat class has a meow() method and Tiger overrides the method then you can access Cat.meow() from a Tiger object by calling super.meow().
Yes - if you're writing code inside the Tiger class. If you're writing code in some other class, and are referring to a Tiger instance, there is no way you can ever invoke Cat's meow() method - you can only invoke Tiger's meow(). Unless the person who wrote Tiger decided to write some code which calls super.meow() for you.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dot Syntax