简体   繁体   中英

Polymorphism - object and type

I'm a bit confused on the concept of 'Subclass referenced as Superclass' within polymorphism. (referencing here: https://stackify.com/oop-concept-polymorphism/ )

Lets say we have the superclass animal and the subclass dog, where dog extends animal. The following work:

  1. animal testSuper = new animal();
  2. dog testDog = new dog();
  3. animal testSuperDog = new dog();

Could anyone explain a bit further on whats happening behind the scenes for #3? When we do 'new dog()', are we creating an object of the dog class but when we do 'animal testSuperDog' we cast it to the superclass animal? Or is it the other way around - 'animal testSuperDog' creates an animal object but we're casting it down to the subclass dog when we do 'new dog()'?

I've tried the 4th permutation to explore and I get a type mismatch error saying that it can't convert from animal to dog. So that's why I'm assuming there's some conversion going on. 4. dog testSubdog = new animal();

If we can dig a bit deeper, since we know #3 works, whats the benefit / use case of this?

  1. testDog.noise();
  2. testSuperDog.noise();

Both these would use the 'noise' method from the subclass dog.

Polymorphism, the property of an object to take on many different forms. To put this more precisely, a Java object may be accessed using a reference with the same type as the object, a reference that is a superclass of the object, or a reference that defines an interface the object implements, either directly or through a superclass.

So for your first question animal testSuperDog = new dog(); java creates a dog object and the reference is an animal s you can only use methods in animal object.

side note: class name should start with a capital letter

For the second question you created a dog object and must behave as a dog so it use the overridden method and noway to use method in superclass

An example to understand polymorphism:

public class Reptile {
  public String getName() {
     return "Reptile";
  }
}
public class Alligator extends Reptile {
  public String getName() {
    return "Alligator";
  }
}
public class Crocodile extends Reptile {
  public String getName() {
    return "Crocodile";
  }
}
public class ZooWorker {
  public static void feed(Reptile reptile) {
    System.out.println("Feeding reptile "+reptile.getName());
  }

  public static void main(String[] args) {
    feed(new Alligator());
    feed(new Crocodile());
    feed(new Reptile());
  }
}

This code compiles and executes without issue, yielding the following output:

Feeding: Alligator
Feeding: Crocodile
Feeding: Reptile

If we can dig a bit deeper, since we know #3 works, whats the benefit / use case of this?

Assume that you have a Person class:

public class Person {
    String name;
    Animal pet;

    public Person(String name, Animal pet) {
        this.name = name;
        this.pet = pet;
    }
}

Pet can be dog, cat or other

Animal dog = new Dog();
Animal cat = new Cat();
Persion john = new Persion("john", dog);
Persion lia = new Persion("lia", cat);

Class Cat and Dog must be extend Animal

If you want have an animal list and set it in the loop?

Animal dog = new Dog();
Animal cat = new Cat();
List<Animal> list = new ArrayList<>();
list.add(cat);
list.add(dog);

for(Animal animal : list)
    animal.noise();

And there are many other cases. You can reseach for:

  1. Solid design principles
  2. Dependency Injection of Spring framework

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM