Note: This is the conceptual version of my question. My actual question about the application of these concepts is shown below this chunk.
I am trying to figure out when an object is considered an instance of its own class or the class's superclass. Consider the following superclass Animal and Subclass Seahorse:
import java.util.List;
import java.util.ArrayList;
public class Animal
{
public static void main(String[] args)
{
Animal a= new Animal();
/*1. This line prints a's class*/
System.out.println(a.getClass());
List<Animal> animals= new ArrayList<Animal>(1);
animals.add(new Seahorse(1));
//2. Let's see what happens here
animals.get(0).printClass();
for(Animal e : animals)
{
e.printClass(); //3 This is an overriden method
//e.printThisClass(); Compile Time error;
//4. Note this method only exists in subclass
}
Animal hiahiahia = new Seahorse();
hiahiahia.printClass(); //5. Lets see what happens
hiahiahia.printThisClass(); //6.
}
public void printClass()
{
System.out.println("Animal");
}
}
public class Seahorse extends Animal
{
private int y;
public Seahorse(int a)
{
this.y=a;
}
public void printClass()
{
System.out.println("Seahorse");
}
public void printThisClass()
{
System.out.println("Seahorse");
}
}
And the outputs look like this:
class Animal //1.
Seahorse //2.
Seahorse //3.
Seahorse //5.
You can see that Although Seahorse Objects are stored in Animal Objects or Lists, Polymorphism caused the Seahorse methods to be called.
However, when I try to call a method whose name only exists in the subclass on an Seahorse object disguised as a Animal object, the compile throws an error.
QUESTION: Is it true that polymorphism only works when methods override? Is there any time when
Animal animal = new Seahorse(1);
Causes animal to be seen as an animal only(not a seahorse)?
What's the difference between
Animal animal = new Seahorse();
animal.printThisClass();
and
((Seahorse)animal).printThisClass();
Finally, when should I use casting?
I am working on a 2d Game and created the following class:
public class Entity
{
}
and
public class Character extends Entity
{
//With bunch of extra fields and methods
}
I also have a controller class that has two fields:
private List<Entity>;
private List<Character>;
I separate them because Entities(in this case arrows, bullets) only collide with characters, but not with other entities. Here's the problem: I also have a render method that draws the BufferedImage of all my Entities. Now in order for the graph to make sense, the Entities should be rendered in the ascending order of their Y values.(That is, from farthest to nearest). However, this requires all characters and entities to be put in the same collection...
The question is: Should I separate them or not? If I put them in the same List, wouldn't that cause confusion because you can't tell characters from Entities?
Thanks : )