IntelliJ Java IDE
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Chapter 8 - Question 5 - Page 695 Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Chapter 8 - Question 5 - Page 695" Watch "Chapter 8 - Question 5 - Page 695" New topic
Author

Chapter 8 - Question 5 - Page 695

Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 69

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.


OCPJP 6
John Jai
Rancher

Joined: May 31, 2011
Posts: 1372
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.

Hiral Jhaveri
Greenhorn

Joined: Nov 23, 2011
Posts: 5
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.
Joseph Arnold
Ranch Hand

Joined: Oct 05, 2010
Posts: 42
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.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 2686
Hiral Jhaveri wrote:

Check your brackets. That won't compile either.


Joanne
 
IntelliJ Java IDE
 
subject: Chapter 8 - Question 5 - Page 695
 
Threads others viewed
Inner Class
Inner classes
method-local inner class
chapter 2 Object orienation polymorphism
Java noob needs help
Two Laptop Bag

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture