• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reference variable declaration

 
Ranch Hand
Posts: 75
Eclipse IDE AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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



 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Saumyaraj Zala
Ranch Hand
Posts: 75
Eclipse IDE AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please answer my question... I am not getting it
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Saumyaraj Zala
Ranch Hand
Posts: 75
Eclipse IDE AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But Dear has all Animal methods plus its own methods so why it cant call Animals method??
Why this is not allowed?
please help
 
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 75
Eclipse IDE AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 75
Eclipse IDE AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic