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
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.
Subtype polymorphism relies on upcasting and late binding. Upcasting is a form of casting where you cast up the inheritance hierarchy from a subtype to a supertype. No cast operator is involved because the subtype is a specialization of the supertype. For example, Shape s = new Circle(); upcasts from Circle to Shape .
Subtyping describes type relationships, and subtype polymorphism enables operations defined for supertypes to be safely substituted with subtypes. Concretely, imagine the relation between a 'Cat' class and an 'Animal' class. (Remember: classes create data types in JS++.)
Object-Oriented Programming focuses on four basic concepts i.e. abstraction, encapsulation, inheritance, and polymorphism.
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With