Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymorphism on simple example

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

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 is called this:

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 output, but in what cases or maybe when we should call the methods for the Cow class, and when form Whale class. Sorry if I gave too stupid or too simple example. I hope you undeerstood what I wanted to say. Thanks in advance.

like image 773
t_sologub Avatar asked Oct 10 '15 08:10

t_sologub


People also ask

What is polymorphism in simple words?

We may define polymorphism, in simple words, the capacity of a communication to be shown in more than one shape. An example of polymorphism in real life: An individual may have distinct characteristics at the same time. Like a man, he is a father, a husband, and an employee at the same time.

How to achieve polymorphism in Java?

We can achieve polymorphism in Java using the following ways: During inheritance in Java, if the same method is present in both the superclass and the subclass. Then, the method in the subclass overrides the same method in the superclass.

What are the types of polymorphism in C++?

This is divided into compile time polymorphism and runtime polymorphism in C++. An example of compile time polymorphism is function overloading or operator overloading. An example of runtime polymorphism is function overriding. An example of polymorphism using function overloading in C++ is given as follows.

How many methods of polymorphism are there in Computer Science?

To solve problems in computer science and programming, there are several common methods of applying polymorphism. Five different ways of polymorphism that are widely used in different programming languages are briefly listed below.


Video Answer


2 Answers

It's good question to understand Polymorphism. I am not sure that Whale should extends Cow ;), but I can show you a bit of 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 above example I created Animal class which is extended by Cow and Whale. I can create list of my animals and display they names. In your example is no difference to make a object for Cow cow = new Whale(); and Whale whale = new Whale(); Is it clear for you?

like image 81
Roman Barzyczak Avatar answered Oct 03 '22 18:10

Roman Barzyczak


When using Cow cow = new Whale(), the object is created like a Whale but only has access to Cow methods.

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

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 doesn't have a method called getDeepestDive():

Cow whale = new Whale();
System.out.println(whale.getDeepestDive());
like image 23
SteeveDroz Avatar answered Oct 03 '22 18:10

SteeveDroz