Different object and reference

2019-07-10 05:10发布

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?

2条回答
老娘就宠你
2楼-- · 2019-07-10 05:12
  • Animal a = new Animal(); --> creates an instance of Animal referenced as an Animal. Only Animal methods will be available to invoke from a.
  • Animal b = new Dog(); --> since Dog extends Animal, creates an instance of Dog referenced as an Animal. Only Animal methods (i.e. no methods pertaining only to Dog) will be available to invoke from b, but the virtual method invocation mechanism will resolve method invocation to Dog's implementations at runtime, when overriding Animal'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!");
        }
    }
}
查看更多
迷人小祖宗
3楼-- · 2019-07-10 05:20

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.

查看更多
登录 后发表回答