Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dot Syntax

 
Nick Delauney
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic