• 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

Chapter 8 - Question 5 - Page 695

 
Ranch Hand
Posts: 176
Netbeans IDE Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Guys,



Compilation fails because of an error on line 10.

I do not understand the explaination by the book. Could someone please elaborate on it.

"If you use a reference variable of type Object, you can access only those members defined in class Object."

Is this saying that the code fails to compile as the Object class does not have a public String variable named 'horse'?

Thanks.
 
Rancher
Posts: 1776
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The concept is that when you use your base class reference to refer child class instance polymorphically, you can call only those methods defined in the base class using the base class reference.
Though the base class reference is holding a child class instance it's not possible and allowed to call child class specific methods.

See below program, a football player is a person and so is a tennis player. you cannot ask a football player to play tennis and vice versa.

 
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. You understood it right. Since the reference is of classtype Object.

So, using this reference we can only access the fields (or methods) that are defined in class Object.

Though the actual object is of classtype Horse, the compiler doesn't know this, so it gives error!

In order to access name, our reference must be first type-casted to Horse, and then name can be accessed.



Again, here type-casting works (without compilation error or Exception at runtime) because the actual object is of classtype Horse.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Object obj = new Horse("Zippo");


Here is the reference type is Object and hence you can only access members of object and not horse.
If you want to use a access a method/property of Horse, you should simple use the horse reference type itself.
Look at it this way, the only reason Java allows you have to have a base class refer to a sub class type is to make use of polymorphism. Conceptually, a base class reference is used to access a property that is common to all subclasses and not a property pertaining to a particular sub class(here name belongs only to horse).
Common properties of several classes are identified and are put in a common class called the Base class. That is the idea behind inheritance.
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hiral Jhaveri wrote:


Check your brackets. That won't compile either.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic