1

This is an example I found online while studying about 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 :

  1. Animal is the class name
  2. b is object ref
  3. new for memory allocation
  4. Dog is constructor call

But where is 'b' referring too exactly? What is Animal b = new Dog(); doing? If Dog extends Animal, why is it Animal b = new Dog and not Dog b = new Dog();? If I had substituted that line with Dog b = new Dog();, what would be the difference from Animal b = new Dog();

6
  • 1
    Animal b = new Dog() means, Animal reference and Dog Object? Commented Jul 11, 2017 at 6:44
  • 2
    b is referencing an instance of Dog, but b is acting as an instance of Animal ... wolf in sheep's clothing - welcome to polymorphism Commented Jul 11, 2017 at 6:46
  • 2
    Why do you use List<String> list = new ArrayList<>()? To decouple your code from a specific implementation. Same with Animal and Dog. When your dog becomes a Cat, only 1 change is required in your code because using Animal is still correct. Commented Jul 11, 2017 at 6:46
  • @MaccenWright What does that mean internally? How it works? It's referencing to Animal class and creating a object for dog class? I cannot understand what's going on internally. Commented Jul 11, 2017 at 6:46
  • This is polymorphism and upcasting. Animal b = new Dog(); this line means that you can use Animal class's inteface that is you can use Animal's methods in compile time. But in runtime Dog class's methods run. Commented Jul 11, 2017 at 6:50

3 Answers 3

3
Animal b;

declares a variable called b that an store an Animal object. Is Dog a kind of Animal? Yes. So a dog can be put into b. Perfectly OK.

If Dog extends Animal, why is it Animal b = new Dog and not Dog b = new Dog();

There is no apparent reason if that's all of the code. The person who wrote the code thought, let's do it this way, so he did.

Given more context, the person who wrote the code might want to store other animals later on, e.g. Cat. If he declared it like Dog b, he can't store a Cat in it later on.

Another reason might be to just demonstrate polymorphism. It demonstrates that "yes, you can indeed assign a dog to a animal variable. This is because Dog extends Animal!"

So what's the difference between Animal b and Dog b?

With the current code, there is no difference. But if you declare some more members in Dog, you will see the difference. Let's you wrote a method called bark in Dog. With Animal b, you can't access bark, but with Dog b, you can. This is because with the former, the compiler thinks that b is an Animal, not a Dog. (It actually is, tough)

Sign up to request clarification or add additional context in comments.

1 Comment

You need to stress more on good practices like programming to an interface and loose coupling instead of saying "There is no apparent reason if that's all of the code". Good explanation otherwise.
2

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

The object b even though has Animal reference, Dog object gets created during runtime. So basically you can instantiate sub class objects using a super class reference. So, this is just used for showing the method overriding as you can call the sub class method "move" using super class reference.

Comments

-1

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

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

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.