polymorphic arrays and branching logic in Java

Posted on 2007-08-08
Last Modified: 2013-12-29
Hi experts,
                Can anybody tell me what are polymorphic arrays and branching logic in Java ? Thanx in advance.
Question by:royalcyber
    LVL 23

    Accepted Solution

    >> "polymorphic arrays"?

    I hope it means something like this .

    BaseClass[] bc = new BaseClass[100];
     bc = new SubClass1[100];
     bc = new SubClass2[100];

    See the same base class object is used to point to all the different arrays. which have common base class.

    >>"branching logic in Java"

    LVL 2

    Expert Comment

    Polymophic means: can contain different kinds of objects. So an array defined as having Objects (in Java) can contain Strings and Numbers mixed.

    Branching logic are the if and select statements.
    LVL 12

    Assisted Solution

    Check it out here:

    Arrays of Subclasses

    You may also benefit from checking into Groovy, which handles mixed types more flexibly:
    LVL 23

    Expert Comment


    >> Polymophic means: can contain different kinds of objects. So an array defined as having Objects (in Java) can contain Strings and Numbers mixed.

    I don't think thats polymorphic whats so great about a object array containing different object, how can you use that array, you may land up getting many exception like ClassCastExcpetion, ArrayStorageException etc etc. when you are trying to extract those objects out of the array.  

    If i am wrong experts please correct me. :)
    LVL 2

    Expert Comment

    I am sorry, but an polymorphic array is one that contains multiple objects. And yes, it is tricky to work with and that is why they added generics to Java 1.5 (to improve it, still not perfect). No one said polymorphic meant "no problems".
    LVL 2

    Expert Comment

    Strange points division.

    You realize that Number, Strings and any other class in java (BaseClass, SubClass)  is a subclass of Object? Hence an array of Objects can contain instances of any class.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Suggested Solutions

    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now