问题
I was trying to learn Java from Tutorials Point. So, may be this question would be so basic. But I am really stuck here. And I don't know what to google to get it resolved.
Please have a look at this program.
class Animal{
public void move(){
System.out.println("Animals can move");
}
}
class Dog extends Animal{
public void move(){
System.out.println("Dogs can walk and run");
}
}
public class TestDog{
public static void main(String args[]){
Animal a = new Animal();
Animal b = new Dog();
a.move();// runs the method in Animal class
b.move();//Runs the method in Dog class
}
}
Please have a look at the object creation.
Animal a = new Animal();
Animal b = new Dog();
What is the difference between these two? I am familiar with the first one. Could someone explain in simple terms what happens when an object is defined in the second way?
回答1:
Animal a = new Animal();--> creates an instance ofAnimalreferenced as anAnimal. OnlyAnimalmethods will be available to invoke froma.Animal b = new Dog();--> sinceDogextendsAnimal, creates an instance ofDogreferenced as anAnimal. OnlyAnimalmethods (i.e. no methods pertaining only toDog) will be available to invoke fromb, but the virtual method invocation mechanism will resolve method invocation toDog's implementations at runtime, when overridingAnimal's.
Note
Object methods (equals, hashCode, wait overloads, notify, notifyAll, toString and getClass) are available to all objects.
Fully commented example
package test;
public class Main {
public static void main(String[] args) {
/*
* Abstract class - using anonymous idiom here
* Can invoke:
* move
* forAnimalAndChildren
* all Object methods
*/
Animal animal = new Animal(){};
/*
* Instance type is Dog but reference is Animal
* Can invoke:
* move
* forAnimalAndChildren
* all Object methods
* Note that invoking "move" will
* resolve to Dog's "move" implementation at runtime,
* if any is provided in class Dog
*/
Animal dogReferencedAsAnimal = new Dog();
/*
* Instance and reference types are Dog
* Can invoke:
* move
* forAnimalAndChildren
* onlyDog
* all Object methods
*/
Dog dog = new Dog();
}
/*
* Setting this up as an abstract class, as no concrete "animals" can exist - only more specific types.
*/
static abstract class Animal {
void move() {
System.out.println("Animal moving...");
}
void forAnimalAndChildren() {
System.out.println("Anyone extending Animal can invoke me!");
}
}
static class Dog extends Animal {
@Override
void move() {
System.out.println("Dog moving...");
}
void onlyDog() {
System.out.println("Only for dogs!");
}
}
}
回答2:
Dog class inherits from Animal class.
In the example above, the first creates and Animal object while the second creates a Dog object. But because the Dog class inherits from the Animal class, the move() function in Dog overrides the move() function in Animal (since they are they have the same function prototype).
Thus, when you run the b.move(), you run the Dog's move() function instead.
来源:https://stackoverflow.com/questions/35917978/different-object-and-reference