• 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

Super class and subclass access methods

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
susan waters
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all your postings.
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
susan waters
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I completely understand the concept with your postings. Thanks to all of you.
 
reply
    Bookmark Topic Watch Topic
  • New Topic