Java inheritance and override method

This is an example I found online while studying inheritance.

 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
   }
}

      

My main confusion is the line in the main method: Animal b = new Dog();

I understand:

  • Animal is the name of the class
  • b - ref object
  • new to allocate memory
  • Dog - constructor call

But where does "b" mean too accurately? What does it do Animal b = new Dog();

? If the dog is extending Animal, why should he Animal b = new Dog

not Dog b = new Dog();

? If I replace this line with Dog b = new Dog();

what is the difference betweenAnimal b = new Dog();

+3


source to share


3 answers


Animal b;

      

declares a named variable b

that stores the object Animal

. Is it Dog

kind Animal

? Yes. Therefore, the dog can be put in b

. Excellent Good.

If Dog is extending Animal, why is it Animal b = new Dog

and notDog b = new Dog();

There is no obvious reason if all the code. The person who wrote the code thought, let him do it this way, so he did.



Given the larger context, the person who wrote the code might want to save other animals later, for example. Cat

... If he declares it as Dog b

, he won't be able to save Cat

in it later.

Another reason could be simply to demonstrate polymorphism. This demonstrates that "yes, you can actually assign a dog to an animal variable. That's because a dog expands an animal!"

So what's the difference between Animal b

and Dog b

?

There is no difference with the current code. But if you declare a few more members in Dog

, you will see the difference. Suppose you wrote a method bark

in Dog

. With, Animal b

you cannot access bark

, but with, Dog b

you can. This is because with the first one the compiler thinks which b

is Animal

, and not Dog

. (It's actually hard)

+4


source


There is nothing wrong with using Dog b = new Dog (). This just creates a Dog object. Now, Animal a = new Animal (); Animal b = new Dog ();



The b object, although it has a reference to Animal, the Dog object is created at runtime. This way you can create subclass objects using the superclass reference. So this is just used to display the method override, since you can call the "move" subclass method using the superclass reference.

+2


source


The left hand "=" means the class of the variable declaration "b", the right hand is the exact class.

The Dog class has extended the Animal class, one variable can be declared as a parent class and implemented by a subclass.

Run the code below, you can get "true".

System.out.println (b instanceof Animal); // true System.out.println (b instanceof Dog); // true

-1


source







All Articles