13
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

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
user133466
  • 3,391
  • 18
  • 63
  • 92
  • Did you mean `Computer[] labComputers = new Notebook;` in the last line of code? – ForceMagic Oct 14 '12 at 02:47
  • @ForceMagic ok, if `Computer[] labComputers = new Notebook[10];` is the case, what kind of error would i get? – user133466 Oct 14 '12 at 02:50
  • Somewhat related and _great_ to read: http://stackoverflow.com/q/383947/778118 – jahroy Oct 14 '12 at 02:59
  • Since the answer was longer than I could do in a comment I posted an Answer for it. I tried to explain you more about polymorphism, I hope I was clear enough :) – ForceMagic Oct 14 '12 at 03:05

3 Answers3

17

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.

Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
9

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();
    }
}
ForceMagic
  • 6,230
  • 12
  • 66
  • 88
6

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.

arshajii
  • 127,459
  • 24
  • 238
  • 287