This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes a burning question of reference type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "a burning question of reference type" Watch "a burning question of reference type" New topic
Author

a burning question of reference type

addy sharma
Greenhorn

Joined: Apr 17, 2011
Posts: 16


there is question

here s1 is a superclass and s2 is a subclass of s1....

s1 ss=new s2();

we know that here the reference variable is of type s1 and it is refering to the object of class s2..and it is known that this object will b used for the polymorphic call...

but somebody asked me that what do you mean by "type" of reference variable...

and the diff between

s1 ss= new s2();

and

s2 sss= new s2();

so please help me to explain about the "type"....


java is best because i can even drink it.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

The type of the reference is what the compiler will check when it's asked to do something with the variable.

For example, if you have a method that is defined in s2 but not s1. Then you could call the method in your second case, but not your first case.

(If the method exists in both classes then it will, of course, use polymorphism to make sure the correct version is called according to the object type. But whether the call is allowed depends on the reference type.).
addy sharma
Greenhorn

Joined: Apr 17, 2011
Posts: 16

yes the same answer i told to the answer seeker....that answer seeker was not satisfied..

and i will remove the duplicate
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

Why was the answer seeker not satisfied? Did you ask him what he or she did not understand about your answer?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
addy sharma
Greenhorn

Joined: Apr 17, 2011
Posts: 16

well i have made her understand the same thing now..thanks for to all the people who posted reply
 
GeeCON Prague 2014
 
subject: a burning question of reference type