#What is the purpose of dynamic binding/dynamic polymorphism?

1 messages · Page 1 of 1 (latest)

strong saddle
#

I don't understand what's the use of referencing a superclass to a reference of a subclass. From most materials that I've read online. The main point they highlight is that with dynamic binding, the overriden method gets called rather than the superclass method. But this is true with normal instantiation as well. Suppose the following code

package rmit.cosc2081.Test;

class Animal {
    protected String species;
    protected int population;

    void reproduce() {
        population += 1;
    }

    public int getPopulation() {
        return population;
    }
}

class Frog extends Animal{
    @Override
    void reproduce() {
        population += 10;
    }
}

public class Main {
    public static void main(String[] args) {
        //Normal Polymorphism
        Frog goldenPoisonFrog = new Frog();
        goldenPoisonFrog.reproduce();
        System.out.println(goldenPoisonFrog.getPopulation());

        //Dynamic Polymorphism
        Animal toad = new Frog();
        toad.reproduce();
        System.out.println(toad.getPopulation());
    }
}

The method goldenPoisonFrog.reproduce() and toad.reproduc() both invoke the overriden method.

I think what I'm really asking here is what is the difference between this code:

Frog goldenPoisonFrog = new Frog()

... and this code

Animal goldenPoisonFrog = new Frog()

Like what is the difference between these two and when would I use one and the other?

jagged egretBOT
#

<@&987246399047479336> please have a look, thanks.

jagged egretBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

strong saddle
#

I think what I'm really asking here is what is the difference between this code:

Frog goldenPoisonFrog = new Frog()

... and this code

Animal goldenPoisonFrog = new Frog()

and when would I use one as opposed to the other?

fierce frigate
#

Not much

#

if there are no extra methods on Frog that aren't on Animal, they are equivalent

#

you'd prefer Animal if you had

#
Animal thing;
if (...) {
    thing = new Frog();
}
else {
    thing = new Cat();
}
#

but otherwise its not important

#

it matters with generics - you can't assign List<Frog> to List<Animal> - but thats hard explain and doesn't come up much

#

i'd prefer

#
var goldenPoisonFrog = new Frog();
strong saddle
#

Because even with extra methods on Frog. The methods on the Frog class would be unique to that class and cannot be called via the Animal class

fierce frigate
#

yes

#

i meant that if there are extra methods, you'd want to use Frog on the lhs

#
Frog f = new Frog();
f.onlyOnFrog();
strong saddle
fierce frigate
#
Animal f = new Frog();
f.onlyOnFrog(); // won't work
fierce frigate
strong saddle
#

I still don't get the distinction between these 2 ways of instatiating a reference type

fierce frigate
#

the only distinction is the type given to the variable

#

thats it

#

think about it this way

#
Object frog = new Frog();
strong saddle
fierce frigate
#

i am doing that

strong saddle
#

Alright

fierce frigate
#

so when you assign new Frog(); to a variable whose type is Object, you still have a frog there

#

just the compiler "forgot"

#

so now through that refrence you can only call methods on Object

#

equals, hashCode, toString

strong saddle
#

What do you mean the compiler "forgot"???

fierce frigate
#

like, if you try to call .reproduce()

#

it won't know that the Object stored in the frog variable has that method

#

because Object doesn't

#

you make a frog, but then you assign it to a variable that holds a "supertype"

#

you can still recover the fact that you have a Frog with instanceof or whatever

#

but from that line on, all the java compiler knows is that some Object is there

#

its the same difference between Animal and Frog

#
Animal frog = new Frog();
#

here you forget that you have a frog here, but you know that you are some Animal

#

that means through this reference you can call any method that exists on Animal or Object

#

(Object -> Animal -> Frog)

#

but you can't use methods unique to frog

#
package rmit.cosc2081.Test;

class Animal {
    protected String species;
    protected int population;

    void reproduce() {
        population += 1;
    }

    public int getPopulation() {
        return population;
    }
}

class Frog extends Animal{
    @Override
    void reproduce() {
        population += 10;
    }

    void doSomethingSpecial() {

    }
}

public class Main {
    public static void main(String[] args) {
        //Normal Polymorphism
        Frog goldenPoisonFrog = new Frog();
        goldenPoisonFrog.reproduce();
        System.out.println(goldenPoisonFrog.getPopulation());

        //Dynamic Polymorphism
        Animal toad = new Frog();
        toad.reproduce();
        System.out.println(toad.getPopulation());
    }
}
#

so you could call .doSomethingSpecial() with the goldenPoisonFrog but not the toad

#

but this specific situation Frog does not have any more methods than Animal

#

so it does not matter which you pick for the variable

#

it comes down to personal taste

#

whether something is "normal" or "dynamic" polymorphism isn't really ever important

#

not to mention it isn't really how we talk about that situation

cinder lance
#

Population shouldn't be static? :V