user133466
user133466

Reputation: 3415

Subtype polymorphism and arrays

Computer[] labComputers = new Computer[10];

with

public class Computer {
...
     void toString(){
     // print computer specs
     }
}
public class Notebook extends Computer{
...
     void toString(){
     // print computer specs + laptop color
     }
}

each subscripted variable labComputers[i] can reference either a Computer object or a Notebook object because Notebook is a subclass of Computer. For the method call labComputers[i].toString(), polymorphism ensures that the correct toString method is called.

I wonder what if we do

Notebook[] labComputers = new Notebook[10];

what kind or error would I get if I reference with Computer object and a Notebook object

Upvotes: 14

Views: 16278

Answers (3)

Amit Deshpande
Amit Deshpande

Reputation: 19185

Since the question specifically asks about kind of error I will explain them with below scenarios

If you do below

Notebook[] labComputers = new Notebook[10];

Now you can only set Notebook objects in array.

labComputers[0]  = new Notebook(); // Fine
labComputers[1]  = new Computer(); // Compilation error 

Now if you do

Computer[] notebooks = new Notebook[10];
notebooks[0] = new Notebook();
notebooks[1] = new Computer(); // <--- ArrayStoreException

Because arrays are covarant,reified in nature ie. if Sub is a subtype of Super, then the array type Sub[] is a subtype of Super[], and arrays enforce their element types at run time it will cause ArrayStoreException

You can read oracle docs about Polymorphism to know more how it works.

Upvotes: 17

ForceMagic
ForceMagic

Reputation: 6378

I think you have to understand how polymorphism works.

Polymorphism is a feature that allow multiples data type to behave the same way through a common interface.

For instance,

      Computer  // Base class
       |    |
 Notebook   Desktop    // Both inherits of Computer

Polymorphism would allow you to manage an array of Computer, no matter if they are a Notebook or a Desktop.

Computer[] computerArray = new Computer[2];

computerArray[0] = new Notebook();
computerArray[1] = new Desktop();

The advantage of this, is that you don't have to know which subtype of computer you are working with. They will behave as computer.

Now comes the big difference, in your Computer class you could have :

public Class Computer 
{
    abstract void MoveMouse();
}

This will give you the opportunity to redefine this method differently in Notebook and Desktop. MoveMouse() will now be available to computeArray because we defined it in Computer.

If you do this:

computerArray[0].MoveMouse(); // which contains a Notebook

computerArray[1].MoveMouse(); // which contains a Desktop

that will call the function that is implemented in Notebook or Desktop.

An example of those function implementation:

public Class Notebook extends Computer
{
    void MoveMouse();
    {
         MousePad.Move();
    }
}

public Class Desktop extends Computer
{
    void MoveMouse();
    {
         USBMouse.Move();
    }
}

Upvotes: 9

arshajii
arshajii

Reputation: 129507

Each subscripted variable labComputers[i] can reference either a Computer object or a Notebook object.

This is technically true, but you must bear in mind that every Notebook is a Computer object, but not every Computer is a Notebook.

Therefore, if you had

Notebook[] labComputers = new Notebook[10];

you would not be able to place Computer instances in the array because not every Computer is a Notebook - and your array can hold only Notebooks.

Upvotes: 6

Related Questions