Click here to Skip to main content
14,695,615 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I cant understand what the author mean by this can someone explain it to me with code In java?

You can have polymorphic arguments and return types.If you can declare a reference variable of a supertype, say, Animal, and assign a subclass object to it, say, Dog, think of how that might work when the reference is an argument to a method..

What I have tried:

Can someone explain it with java code please
Posted
Updated 10-Jul-20 0:06am

A Dog is an Animal, and so is a Cat.
So if you create a method that takes a Animal parameter, you can pass it either a Cat or a Dog instance - but inside the method you can only use methods and properties that are applicable to both species - you can't call the Meow or Bark methods for example. You can call Feed and the (hopefully) overloaded version from the appropriate species will be used.

Similarly, you can return an Animal, a Cat, or a Dog - but if you do then you can only store it in a variable of the right type, or the supertype: a Cat returning method can only be stored in a Cat or Animal method; a Dog returning method requires a Dog or an Animal variable. If your method returns an Animal, it can't be stored in a Cat or a Dog variable even if the actual type of the instance is correct, as the system doesn't know that it always will be.

Code is up to you: it's not my homework!
   
Griff's answer is spot on but here's some code too:
class A
{
    public void output() { System.out.println("Called A."); }
}

class B extends A
{
    /* Override isn't required. It's just for clarity purposes and depending on
     * your dev software it might give you a warning if you override something
     * without specifying @Override just in case you've done it accidentally.
    */
    @Override
    public void output() { System.out.println("Called B."); }

    public void somethingSpecificToB() { System.out.println("B only."); }
}

class C extends A 
{
    public void somethingSpecificToC() { System.out.println("C only."); } 
}

class Test
{
    public static void main(String[] args)
    {
        polymorphicParam(new A());
        polymorphicParam(new B());
        polymorphicParam(new C());

        A value = polymorphicReturn(true); //Return value is actually type 'A'
        value.output();
        value = polymorphicReturn(false); //Return value is actually type 'B'
        //Can only call output(), since we're treating what was returned as
        //an instance of class A
        value.output();
    }

    private static void polymorphicParam(A param)
    {
        //Even if the actual input class is B or C, you can still only
        //use the members defined in class A since that's what the parameter
        //type is.
        param.output();
    }

    private static A polymorphicReturn(bool createA)
    {
        if (createA)
           return new A();
        return new B();
    }
}

//Output:
// Called A.
// Called B.
// Called A.
// Called A.
// Called B.


So here, class B and C can both still satisfy calls to output() so therefore can be "up-cast" and treated like instances of class A. Class B uses its own implementation of output() while class C inherits class A's implementation.

In fact, because Java forces either overriding or inheriting public members from super-classes, this sub-type polymorphism always works. Java won't let you break it by, for example, changing output() to private in a subclass.
   
v2
One of the best ways to learn is to follow the language tutorials: Trail: Learning the Java Language (The Java™ Tutorials)[^].
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900