wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Super class and subclass access methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Super class and subclass access methods" Watch "Super class and subclass access methods" New topic
Author

Super class and subclass access methods

susan waters
Ranch Hand

Joined: Feb 21, 2006
Posts: 68
Please go thru this code.

Animal Class
=======================
class Animal {
public void eat() {
System.out.println("Generic Animal Eating Generically");
}

public void eatanimal() {
System.out.println("In Animal class eatanimal method");
}


}

Horse Class
===============

class Horse extends Animal {
public void eat() {
System.out.println("Horse eating hay, oats, "
+ "and horse treats");
}
public void buck() { }

public void eathorse() {
System.out.println("In Horse class eathorse method");
}




}


Test Animal class
====================
public class TestAnimals {
public static void main (String [] args) {

Animal b = new Horse();

b.eathorse();


}
}


Error:

testAnimals.java:7: cannot resolve symbol
symbol : method eathorse ()
location: class Animal
b.eathorse();
^
1 error


My QUestion: I am trying to access eathorse(), which is in the class Horse. When I say

Animal b = new Horse(); it is Animal reference and Horse object right ? but why it is looking in class Animal ? Surprisingly if I write the same method in Animal class I am not getting error and I am getting Horse method in Horse class. which is the output " In Horse class eathorse method "

Can anybody pl explain what's going on here ? Thanks very much.
Mushkan Desai
Greenhorn

Joined: Mar 03, 2006
Posts: 9
Hi Susan,

U had created a Subclass of Animal. Now Animal Class doesnt know anything abt its subclass(Horse Class is not visible to Animal Class) so when u create a object it will look for tht method in Animal Class itself. And Animal Class doesnt contain tht method so it will give u error and as u say if u move that method in Animal Class it will work properly.
Leonardo Luiz
Ranch Hand

Joined: Mar 24, 2006
Posts: 51
Hi,

The b reference variable exposes only the methods of the declared type:

Animal b

However, the referenced object will be of the type used after new.

New Horse();

The compiler can �see� only Animal methods. So you can use only Animal methods through b variable. Any way in run-time the JVM will create an instance of horse.

�Surprisingly if I write the same method in Animal class I am not getting error and I am getting Horse method in Horse class. which is the output " In Horse class eathorse method "

It is because the object is a Horse. In despite of reference variable type (Animal), the JVM will execute the object type implementation (Horse).


Leonardo Luiz

[ April 27, 2006: Message edited by: Leonardo Luiz ]
[ April 27, 2006: Message edited by: Leonardo Luiz ]
Dick Eimers
Ranch Hand

Joined: Apr 15, 2006
Posts: 40
Thet type of 'b' is Animal and therefore you can only call methods known to an Animal and the eathorse() method is only known to Horse.

You can assign a Horse to an Animal because a Horse IS-A(n) Animal. That is, it has all abilities an animal has and thus it can be treated as one. The reverse is not correct-- not all animals are horses. We could declare yet another class Donkey which extends Animal and declares the method bray(). Look at the following, I'm sure you'll get it:



<a href="http://eimers.blogspot.com" target="_blank" rel="nofollow">eimers.blogspot.com</a>
susan waters
Ranch Hand

Joined: Feb 21, 2006
Posts: 68
Thanks for all your postings.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Well in other words:

Compiler associates methods with the reference type, while JVM does this with the object type.

So in your code, compiler try to look eathorse() method in Animal class, which is not present there so it gives an error.

And when you put eathorse() method in Animal class. Compiler finds it perfectly so no problem here. And while execution, JVM execute method of Horse class (because object belongs to Horse class).

Hope it is clear.
Joshua Smith
Ranch Hand

Joined: Aug 22, 2005
Posts: 193
Also, if you did want to access the horse methods, you can do that. You just need a cast.

Animal b = new Horse();

if (b instanceof Horse) {
((Horse)b).eathorse();
}

That basically tells Java that you know it's a horse, even though it only sees an Animal, and so you'll take the risk and interact with it as a horse.

Josh


Rational Pi Blog - Java, SCJP, Dev Bits- http://rationalpi.wordpress.com
susan waters
Ranch Hand

Joined: Feb 21, 2006
Posts: 68
Now I completely understand the concept with your postings. Thanks to all of you.
 
Don't get me started about those stupid light bulbs.
 
subject: Super class and subclass access methods