SuperClass object = new SubClass();
Why use a superclass to instantiate a subclass object like above? Because the only way i learnt to instantiate an object is to:
SubClass object = new SubClass();
I'm learning java.
SuperClass object = new SubClass();
Why use a superclass to instantiate a subclass object like above? Because the only way i learnt to instantiate an object is to:
SubClass object = new SubClass();
I'm learning java.
You may have a method that only takes an instance of SuperClass
. Since SubClass
is a SuperClass
, you can use an instance of SubClass
and treat it as SuperClass
.
The same behavior is used when working with interfaces:
List someList = new ArrayList();
That's the beauty of polymorphism. It allows you to change out the implementation of the class' internals without breaking the rest of your code.
This is known as polymorphism. Imagine the following example:
Musician musician = new Pianist();
or
Musician musician = new Drummer();
Suppose Musician
class has a method: play()
. It doesn't matter who the musician is, if you call play method, you'll know how to play the determined instrument, base on concrete class, on this case, no matter if Pianist or Drummer.
Each concrete class, overwriting its play()
method, can play on your own:
e.g. for Pianist
class: play() { playsBeethoven(); }
For detailed information, please check http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html
It's always good to remember to use it with inheritance http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
The main reason to do the former is for function calls that expect the super class. If you have a class A
that has a method like this
public static void insert(SuperClass b) {
//do something
}
and you have the
SuperClass object = new SubClass();
you can do
A.insert(object);
But if you do
SubClass object new SubClass();
you can't do
A.insert(object);
unless you add a method like this to A
public static void insert(SubClass b) {
//do something
}
This helps you cut down on redundant or copied code and keep things cleaner.
All you're doing is telling the JVM what the type of the reference is at compile time.
In the first case, you're saying that the object reference has static type of the super class. When you create a new sub-class object, you can have that reference point to it because a sub-class IS-A super class instance.
That is one of the main reasons you use inheritance. You treat the SuperClass as an abstraction, and at compile time you don't need to know the derived type, or another class doesn't need to know the derived type. This allows you to use references to SuperClass while using polymorphism to call the methods of the derived class.
This enables you to
a.) Create factories so that you don't need to know the type at compile time, and you have some runtime method that creates the derived types. Think COM, GStreamer, DirectShow, Glib, etc...
b.) Hide complexity to other objects by only exposing the base type references even though they are instances of the derived type. Think about the methods that return object or take an object reference as an argument.
c.) many more possibilities, but those are probably the most relevant two uses to you.