• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a burning question of reference type

 
addy sharma
Greenhorn
Posts: 16
Java Opera Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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"....
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
Java Opera Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why was the answer seeker not satisfied? Did you ask him what he or she did not understand about your answer?
 
addy sharma
Greenhorn
Posts: 16
Java Opera Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i have made her understand the same thing now..thanks for to all the people who posted reply
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic