0

Given the following classes:

abstract public class Animal{
    abstract public void greeting();
}

public class Dog extends Animal {
    public void greeting() {
        System.out.println("Woof!");
    }
    public void greeting(Dog another) {
        System.out.println("Wooooooooof!")
    }
}

public class BigDog extends Dog {
    public void greeting() {
        System.out.println("Woow!")
    }
    public void greeting(Dog another) {
        System.out.println("Woooooooooooow!")
    }
}

What is the difference between Dog animal = new Dog() and Animal animal = new Dog()? Because animal.greeting() produces the exact same result.

I know theoretically that later creates an instance of Dog but storing that in the superclass (or abstract in this class). But what does that really do? They both will check Animal class since Dog is extended to Animal.. Need some enlightenment here.

What is the difference between Animal animal = new BigDog() and Dog animal = new BigDog()? Won't they eventually check Animal class? Since BigDog is extended to Dog and Dog is extended to Animal.

Please provide concrete examples to explain this. I understand this in theory but having a hard time understanding what is actually happening behind the scene. Thanks for taking your time to read this. I only know Java so I'd appreciate if you explain using Java..

**EDIT: I have seen the duplicate but still only touching the surface of polymorphism and its relationship with abstract class. Please go read it. My question is specifically asks for the link between abstract and regular classes and between each regular class. I don't see how this is duplicate because I am asking for what is actually happening behind the scene: BigDog extends Dog and Dog extends Animal. So can someone explain what is actually happening here instead of flagging posts like a machine?

  • Related: https://stackoverflow.com/a/53841298/1079354 – Makoto Dec 19 '18 at 04:54
  • @Makoto I already know if the superclass does not have that method it won't work but that has zero relevance to my question. Can you explain what is related to my question? –  Dec 19 '18 at 05:10
  • @Dark Knight, the duplicate doesn't help me at all UGHHH. That is like the very basic polymorphism. Did you even read my question..? :(( –  Dec 19 '18 at 05:12
  • I'm confused as to why you believe the duplicate doesn't satisfy. Several answers point out that this is independent of whether or not the class is abstract or a regular class. Additionally the answer I linked to above (as a comment, not as a dupe) explains why it's possible at all. – Makoto Dec 19 '18 at 05:26
  • I think you're confusing the class of an object with the type of a variable. Once you've done `new BigDog()`, it doesn't make too much difference whether you assign the resulting reference to a variable of type `Animal`, a variable of type `Dog`, or a variable of type `BigDog`. – Dawood ibn Kareem Dec 19 '18 at 05:28
  • > Given the inheritance hierarchy, an Object has no clue about any of its children's specific methods, nor can it. The post you linked along with the answer you posted only look at the aspect of why that specific "children" as you call it does not work. Well of course it won't work because that is not specified in the super class and as you an Object has no clue about it. That has nothing to do with my question. My abstract class provides methods that needs to be implemented and yes it is implemented in the subclasses. –  Dec 19 '18 at 05:31
  • But I am asking for the difference between Animal animal = new and Dog animal = new along with Animal animal = new BigDog() (subclass of subclass) and Dog animal = new BigDog(). How these intricacies actually work? I am not asking for that basic easy example. Why would I ask that in the first place? @Makoto –  Dec 19 '18 at 05:32
  • @DawoodibnKareem Hello, can you confirm that it does not matter? Then why would anyone use Animal type over Dog type for BigDog if they are going to produce the exact same result. I was wondering THAT. –  Dec 19 '18 at 05:35
  • It matters when you pass the variable as a parameter to a method, but not when you call a method on the variable. That's the whole point of polymorphism. And you need to read up about programming to an interface, to answer the question in your last comment. – Dawood ibn Kareem Dec 19 '18 at 05:37
  • "this is independent of whether or not the class is abstract or a regular class." I was not asking that.......... Please read the question @Makoto –  Dec 19 '18 at 05:37
  • @DawoodibnKareem, Hello Thanks for your quick reply. So those Animal, Dog,Bigdog variable does not really do anything? -- only when when passing as a parameter -- then you would downcast it –  Dec 19 '18 at 05:39

0 Answers0