Polymorphism by a simple example

I think I'm starting to understand this topic, but not completely. Can someone explain this example to me:

public class Solution { public static void main(String[] args) { Cow cow = new Whale(); System.out.println(cow.getName()); } public static class Cow { public String getName() { return "Im cow"; } } public static class Whale extends Cow { public String getName() { return "Im whale"; } } } 

What is the difference when it is called:

 Cow cow = new Whale(); System.out.println(cow.getName()); 

and this:

 Whale whale = new Whale(); 

System.out.println (whale.getName ());

I will have the same conclusion, but in what cases, or maybe when we will call the methods of the Cow class, as well as in the form of the Whale class. Sorry if I gave a too stupid or too simple example. I hope you get rid of what I wanted to say. Thank you in advance.

+5
source share
4 answers

A good question is to understand Polymorphism. I'm not sure Whale should extend Cow;), but I can show you a slightly different structure:

 public class Solution { public static void main(String[] args) { Animal animal1 = new Cow(); Animal animal2 = new Whale(); ArrayList<Animal> myAnimals = new ArrayList<Animal>(); myAnimals.add(animal1); myAnimals.add(animal2); //show all my animals for (Animal animal : arrayList) { System.out.println(animal.getName()); } } public static class Animal { public String getName() { return "Im general animal"; } } public static class Cow extends Animal { public String getName() { return "Im cow"; } } public static class Whale extends Animal { public String getName() { return "Im whale"; } } } 

In the above example, I created an Animal class that extends Cow and Whale. I can create a list of my animals and display their names. In your example, it makes no difference to make an object for Cow cow = new Whale (); and whale whale = new whale (); Is that clear to you?

+3
source

When using Cow cow = new Whale() object is created as Whale , but has access only to Cow .

For example, if you add a method to the Whale class:

 public int getDeepestDive() { // some code } 

This code will work:

 Whale whale = new Whale(); System.out.println(whale.getDeepestDive()); 

This code will tell you that Cow does not have a method called getDeepestDive() :

 Cow whale = new Whale(); System.out.println(whale.getDeepestDive()); 
+1
source

In your example, you assigned a Whale instance to Cow . However, the object is still Whale . That is why you get the same result. Now consider this Whale class:

 public static class Whale extends Cow { public String getName() { return "Im whale"; } public void submerge() { System.out.println("Hunting submarines"); } } 

Overriding a method will always call a method for the type of object that is created, unless forcibly explicitly doing otherwise (with cast). However, this new Whale implementation has a new method that is not defined on Cow . Therefore, you can call:

 Whale whale = new Whale(); whale.submerge(); 

But you cannot write:

 Cow mascaradedWhale = new Whale(); mascaradedWhale.submerge(); 

In the second example, you will get a compilation error for calling a method, since this method is not defined in the Cow class.

+1
source

Or maybe the difference will become clearer when you create a method that processes cows, say one that collects the names of all cows, whales, dogs, horses, etc.

This method should not be aware of whales or any other subclass of cow. Nevertheless, it should work. Example:

 public static List<String> collectNames(List<Cow> cows) { return cows.stream().map(c -> c.getName()).collect(Collectors.toList()); } 

And I'm not sure why the whale is expanding the cow. If so, we should see him on the NG channel. A more correct example would be:

Animal β†’ Cow, Dog, Cat, Whale

Person β†’ Student, driver, developer, etc.

+1
source

Source: https://habr.com/ru/post/1233404/


All Articles