starkk92
starkk92

Reputation: 5924

virtual method invocation-polymorphism in java

package polymordemo1; 

class animal{
    public void getanimal(animal anim) {
        anim.ganim();
    }

    private void ganim() {
        System.out.println("this is the animal");
    }
}

class cat extends animal{
    public void ganim() {
        System.out.println("I am cat");
    }
}

class bat extends animal{
    public void ganim() {
        System.out.println("I am bat");
    }
}
public class PolymorDemo1 {
public static void main(String[] args) {
        animal a=new animal();
        cat c=new cat();
        bat b=new bat();

        a.getanimal(c);
        a.getanimal(b);

    }
}

I am newbie to Java and I am working on the Polymorphism.

The output for this code turns out to be:

this is the animal

this is the animal.

Infact I was expecting this output:

this is the cat

this is the bat

What seems to be the mistake in the code.

Upvotes: 1

Views: 2219

Answers (7)

amphibient
amphibient

Reputation: 31358

You need to make the Animal class abstract and declare the ganim() method abstract and also not private (which cannot be overridden) so that it is invoked from the implementation:

public abstract class Animal
{

    public void getanimal() 
    {
        this.ganim();
    }

    abstract void ganim() ;
}

Note also that you do not need to be passing the actual implementation instance to the getanimal() method.

Upvotes: 2

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

Just as side-note:

This is why you should always use the @Override annotation while overriding a method. It's just a good practice and saves you from this kind of pitfalls.

public class Sup {

    private void method() { }
}


public class Sub {

    @Override
    public void method() { }
}

That will result in a compilation failure.

Upvotes: 3

dreamcrash
dreamcrash

Reputation: 51603

Make the method ganim() public instead of private.

You can't override a private method, but you can introduce one in a derived class without a problem. This compiles fine:

class Base
{
   private void foo()
   {
   }
}

class Child extends Base
{
    private void foo()
    {
    }
}

We can not override private method in Java, just like we can not override static method in Java.

Like static methods, private method in Java is also bonded during compile time using static binding by Type information and doesn't depends on what kind of object a particular reference variable is holding.

Since method overriding works on dynamic binding, its not possible to override private method in Java.

private keyword provides highest level of Encapsulation in Java. Though you can hide private method in Java by declaring another private method with same name and different method signature. (source)

Upvotes: 1

Bhavik Ambani
Bhavik Ambani

Reputation: 6657

I have modified you code such a way that you can perform your functionality by the following way.

You can do the thing by the following way

class animal{

    public void getanimal(animal anim) {
        anim.ganim();
    }

    public void ganim() {
        System.out.println("this is the animal");
    }
}

class cat extends animal{
    public void ganim() {
        System.out.println("I am cat");
    }
}

class bat extends animal{
    public void ganim() {
        System.out.println("I am bat");
    }


}
public class PolymorDemo1 {
public static void main(String[] args) {
        animal a=new animal();
        animal c=new cat();
        animal b=new bat();

        a.ganim();
        c.ganim();
        b.ganim();

    }
}

Output

this is the animal
I am cat
I am bat

Upvotes: 0

Vikram
Vikram

Reputation: 4116

Method ganim() in animal class is private. Hence, cannot be overriden.

Change it to public or protected.

Upvotes: 0

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340993

The method in base class is private:

private void ganim()

thus you can't override it. Make it public or protected and it will work.

Upvotes: 2

Petar Minchev
Petar Minchev

Reputation: 47403

Change private void ganim() to public void ganim(). Private methods cannot be overriden in this way.

Upvotes: 6

Related Questions