Different object and reference

℡╲_俬逩灬. 提交于 2020-01-04 05:47:09

问题


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 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!");
        }
    }
}



回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!