more silly questions that are probably elementary to most but.....



Here's a class...pretty classy eh?

package chapter08;

import java.util.ArrayList;

public class AnimalTester {
public static void main(String [] args) {
Wolf aWolf = new Wolf();
Animal aHippo = new Hippo();
ArrayList<Object> myList = new ArrayList<Object>();
myList.add(aWolf);
myList.add(aHippo);
Cat c = new Cat();
myList.add(c);
for (Object o : myList) {
System.out.println(o.getClass());

}

}
}

I have an Animal class, Feline, Canine, Hippo and Cat, Wolf, Dog... well you can guess the rest.

If I run this program I get this:

class chapter08.Wolf
class chapter08.Hippo
class chapter08.Cat

Which is what I expected..sort of... But my question is, if getClass() tells me exactly what class the object is when I added it to the ArrayList why can't I access the instance variables of Wolf, Hippo and Cat once the object is extracted. Based on what I see from code completion, the only instance variables and methods I have access to are the ones that are inherited from the class Object...but if I have a Cat class

package chapter08;

public class Cat extends Feline {
void makeNoise() {

}
void eat(){

}
@Override
public void roam() {
// TODO Auto-generated method stub

}

}


why can't I say o.eat() or o.makeNoise()

It seems to me that the JVM knows what the class of the object is when I extract it from the ArrayList so why can't I get at the instance variables (is there a shorter way to say this?) and the methods. I know I can't, but I would like to know why... a short, one or two sentences would be splendid.


Thanks...
.



Relevant Pages

  • Cant apply XmlType attribute when impl. IXmlSerializable
    ... I can't apply the XmlType attribute when I implement IXmlSerializable. ... generating the XML document. ... public class Start: Point3D ... public override void ReadXml ...
    (microsoft.public.dotnet.xml)
  • Re: Help with my first Java program
    ... Metto go = new Metto; ... public void windowClosing ... private void populateMettoPane() ... public class press1 extends JPanel ...
    (comp.lang.java.programmer)
  • Re: Logic Question about Public Method I created.
    ... public class clsUserControl ... foreach(ListDictionary MyListRow in MyListDictionary) ... private void DoSomething() ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: overriding inner classes
    ... > public class ClassA{ ... > public void doit(){ ... > public class ClassB extends ClassA{ ...
    (comp.lang.java.programmer)
  • About the remoting event
    ... callback port in the client side, but told that these was an exception on the ... static void Main ... Cat cat = new Cat; ...
    (microsoft.public.dotnet.languages.csharp)