22
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.

user1050548
  • 371
  • 1
  • 5
  • 16

5 Answers5

15

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.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • 4
    The critical thing here is that `SubClass` *is* a `SuperClass`. – Louis Wasserman Apr 16 '12 at 15:59
  • 1
    Did this behavior change in Java 8? I just tested the case where a method takes in a superclass parameter `void method(SuperClass param)` but I am able to put in a subclass instance `SubClass sub = new SubClass();` when I call it, as in `method(sub);`. This means that upward compatibility is now allowed? – Gerardo Figueroa Dec 02 '16 at 02:16
15

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

axcdnt
  • 14,004
  • 7
  • 26
  • 31
9

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.

twain249
  • 5,666
  • 1
  • 21
  • 26
1

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.

duffymo
  • 305,152
  • 44
  • 369
  • 561
1

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.

Jonathan Henson
  • 8,076
  • 3
  • 28
  • 52
  • Isn't indeed the same used with Interfaces? E.g. ``` List mList = new ArrayList<>();``` - we use only the known methods of a List defined in the Interface and don't care about the concrete implementation from the ArrayList. – CodiClone May 22 '21 at 14:07