views:

64

answers:

4

I'm creating some random classes to understand Polymorphism better. Coding is as follows:

Poly1:

public abstract class Poly1 {
    int comPoly;
}

SubPoly1:

public class SubPoly1 extends Poly1 {
    String testPoly;
}

SubPoly2:

public class SubPoly2 extends Poly1 {
    int x;
}

testPoly:

public class testPoly {
public static void main(String[] args) {
    Poly1[] testObj = new Poly1[2];
    testObj[0] = new SubPoly1();
    testObj[1] = new SubPoly2();
    testObj[1].x = 1;
    testObj[1].comPoly = 2;
    System.out.println("Test Output : " + testObj[1].x+ " and " + testObj[1].comPoly);
    testObj[0].testPoly = "Hello";
    testObj[0].comPoly = 8;
    System.out.println("Test Output : " + testObj[0].testPoly+ " and " + testObj[1].comPoly);
   }
}

But the program is not getting past the compilation stage as I get the symbol not found error whenever I try to access the variables from SubPoly1 or SubPoly2(for example testObj[1].x would return an error).

Any help would be appreciated.

+1  A: 

The primary usage of polymorphism in industry (object-oriented programming theory) is the ability of objects belonging to different types to respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior.

The keyword here would be same name.

You could modify your example, and include a method in your base class, e.g. public void sayHello(). The subclasses, as they inherit from the base class, can respond to sayHello, but in case they are not implementing this method themselves, the call gets dispatched to the superclass' sayHello. Polymorphism happens, if the subclasses implement own sayHello methods themselves.

Maybe you find it easier to come acquainted with this concept, if you choose some more speaking examples. One prominent illustration of polymorphism are shapes: You have one, possible abstract, basic shape and different subclasses, e.g. a line, a rectangle, a circle, etc. Now each shape will have a draw method, but of course these shapes will use different methods to draw themselves.

Some starting points:

The MYYN
+1  A: 

from a parent reference, you can refer to the common methods only. you are using the parent reference to access a property defined in the sub class

Pangea
+5  A: 

That's because you've declared testObj to be Poly1[] while x is not definied in Poly1, but in SubPoly2. On a Poly1 reference you can only access comPoly.

To fix this, you need to move x to Poly1 or to use SubPoly2[] instead or to cast a Poly1 reference to SubPoly2 (which is only possible when the instance is actually a SubPoly2). Which one the best solution is depends on the functional requirements.

See also:

BalusC
+3  A: 

You have declared Poly1[] testObj = new Poly1[2]; which means whatever you put into that array, regardless of the actual type, can only access the members of the Poly1 class. Polymoprhism comes from methods, not variables.

For example:

public class X
{
    public void foo()
    {
        System.out.println("hello from X");
    }
}

public class Y
    extends X
{
    public void foo()
    {
        System.out.println("hello from Y");
    }
}

public class Main
{
    public static void main(final String[] argv)
    {
        final X[] array;

        array = new X[2];
        array[0] = new X();
        array[1] = new Y();

        System.out.println(array[0].foo());
        System.out.println(array[1].foo());
    }
}

will do what you expect.

TofuBeer
Though polymorphism is mainly related to methods, the concept applies to instance variables as well.
The MYYN
You cannot have polymorphic variables in Java.
TofuBeer