aspose file tools*
The moose likes Java in General and the fly likes reference variable declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "reference variable declaration" Watch "reference variable declaration" New topic
Author

reference variable declaration

Saumyaraj Zala
Ranch Hand

Joined: Aug 06, 2012
Posts: 56

I am aware of the essence of polymorphism i.e I can declare a reference variable of type superclass and assign it to a subclass object but why i am not able to declare a reference variable of type subclass and refer it to superclass object....plzzz help





Why do we fall? So that we can learn to pick ourselves up.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Suppose its allowed and consider Deer and Lion are subclasses of Animal. You could then write like below which will make lion reference pointing to a Deer instance.

Now suppose you have hunt() or roar() methods which are specific to Lion, and you call that using Deer instance what do you think will happen?
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Saumyaraj Zala
Ranch Hand

Joined: Aug 06, 2012
Posts: 56

Please answer my question... I am not getting it
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14153
    
  18

The most important thing to understand about inheritance is that it means "specialization", and that there's an "is a" relationship going from the subclass to the superclass.

In other words: if Sub extends Sub, then that means that a Sub is a special kind of Sup.

This becomes much more clear if you use concrete words such as Animal, Lion, Deer for the superclass and its subclasses instead of abstract words such as Sub and Sup, as the others have already done above.

A Lion is an Animal. A Deer is an Animal. So it's logical that you can assign a specific kind of Animal to a reference of type Animal:


Note that it does not go the other way around: an Animal is not always a Lion, or not always a Deer. Because of that, you can't assign any arbitrary Animal to a reference of type Lion or Deer:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Saumyaraj Zala
Ranch Hand

Joined: Aug 06, 2012
Posts: 56

But Dear has all Animal methods plus its own methods so why it cant call Animals method??
Why this is not allowed?
please help
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

Think about the following sentences.

"A deer is an animal"
"An animal is a deer"

The first is true. The second is not necessarily - "an animal" could be a dog, a tiger, an elephant, whatever. But that's what you're effectively trying to do there - taking "an animal" and telling the compiler "it's a deer". The compiler thinks "that might not be true, I won't allow it".

Edit: note, that's not the same as you just said "so why it cant call Animal's method?". It can. If you declare Deer deer = new Deer(), you'll be able to call Animal method on it. The bit of code you have there is trying to allow Deer methods to be called on an Animal, and that isn't allowed.
Saumyaraj Zala
Ranch Hand

Joined: Aug 06, 2012
Posts: 56

I understood the is-a and has-a relationship.But sir, as i have read uptil now according to head first java,the reference variable is only like a remote control to an object and by using this line my remote control is programmed with all methods of animal and deer..
Saumyaraj Zala
Ranch Hand

Joined: Aug 06, 2012
Posts: 56

Sir, can you please explain this in a more detail?
The bit of code you have there is trying to allow Deer methods to be called on an Animal, and that isn't allowed.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

saumyaraj zala wrote:I understood the is-a and has-a relationship.But sir, as i have read uptil now according to head first java,the reference variable is only like a remote control to an object and by using this line my remote control is programmed with all methods of animal and deer..

That is precisely the point. You've got a remote control programmed with all methods of Animal and Deer. But it's pointing at something that only supports the methods of Animal. What is going to happen when you try and use a method that's only in Deer? It can't work.

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

To bottom line this, you have to stop thinking about it in terms of "allow calling of methods" -- that's not even what this is about. It's really about this rule:

An object reference must be assignment compatible to the type of the variable it is being assigned to. That is, if you have a variable x declared to be of type T, then the assignment x = y is legal if and only if the type of y is T or a subclass of T.

The assignment statement Deer d = new Animal() does not obey this rule: the reference to the new Animal object is neither of type Deer or a subclass of Deer. Animal is a superclass of Deer. Therefore, the assignment is illegal.

It goes back to what Matthew said before about is-a relationships. You can't assign a value y to variable x if the type of y is more general than the type of x. Value y has to have either the same type or a more specific type than the variable x for an assignment to be legal.

Junilu - [How to Ask Questions] [How to Answer Questions]
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

saumyaraj zala wrote:Sir, can you please explain this in a more detail?
The bit of code you have there is trying to allow Deer methods to be called on an Animal, and that isn't allowed.

Hello Saumyaraj,
I think you should re-read what Jesper has posted.. He has quite clearly explained what can be the problem if you were allowed to do that..
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

let's go back to the remote control analogy. you can have a basic TV (correlates to Animal) and a fancy TV (correlates to a Deer) that supports picture-in-picture, and a hi-def TV (correlates to a lion) that does NOT support pic-in-pic, but does support a 'use-hi-resolution' button .

The basic tv remote has standard buttons.
The fancy tv remote has all the standard buttons, plus a picture-in-picture button.
The hi-def tv remote has all the standard buttons, plus a 'use hi resolution' button.

if you point your fancy tv remote at a hi-def TV and press the 'picture in picture' button...what would happen? So, the salesman at the electronic store says "you have a hi-def TV, so I'm not going to let you buy a fancy TV remote because it won't work'. That is the JVM telling you you can't point a Deer reference to a Lion object. or even a Deer reference to an Animal, since you can't point a fancy TV remote at a basic TV, because you will have buttons that don't work.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

saumyaraj zala wrote:I understood the is-a and has-a relationship.But sir, as i have read uptil now according to head first java,the reference variable is only like a remote control to an object and by using this line my remote control is programmed with all methods of animal and deer..


The left side of that statement (Deer d =) says "The 'd' reference variable will hold a reference to an object that IS-A Deer." However, the right side of that statement (new Animal()) evaluates to a reference to an Animal, not a reference to a Deer, so it's not allowed. This is because, even though some Animals are Deer, it is NOT the case that all Animals are Deer. So the LHS can't be sure that the RHS IS-A Deer.

Now lets turn it around.



The left side of that statement (Animal a =) says "The 'a' reference variable will hold a reference to an object that IS-AN Animal." The right side of that statement (new Deer()) evaluates to a reference to a Deer, and since every Deer IS-AN Animal, it is allowed. The LHS can be sure that the RHS IS-AN Animal.
 
jQuery in Action, 2nd edition
 
subject: reference variable declaration