-1

I am learning Java and I don't understand the difference between these assignments:

Superclass x = new Superclass();
Superclass x = new Subclass();
Subclass x = new Subclass();

Where Subclass extends Superclass. I mean that i know that the the first assignment could take the reference of all the subclasses that extends Superclass and also that the last can accept only the reference of the specific Subclass. I also know that in the second assignment Superclass is the "static type" ( don't know if it's correct to say it) which is checked at compile time and Subclass is the "dynamic type" which is checked at runtime. But I cannot imagine a use case in which is better to choose the second assignment instead of the other two. So if you can explain in what these assignments are different it would be great! Thanks :)

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 5
    Related: [What does it mean to “program to an interface”?](https://stackoverflow.com/q/383947) – Pshemo Jan 07 '18 at 17:36
  • 1
    If you want a use case, go read someone else's code, on GitHub or somewhere else. You will see this _a lot_. – Sweeper Jan 07 '18 at 17:37
  • Anyway that is what *polymorphism* is all about. You can have method which describe some steps for general type and it will work for any *specific* instances of type which extend the general one. For instance you can have `void move(Shape s, int x, int y){s.setX(s.getX()+x); s.setY(s.getY()+y); }` and invoke it like `move(rectangle, 10, 20)` or `move(triangle, 20, 30)`. So we can generalize our code, and at the same time we have flexibility in case when we need to change actual arguments passed to methods without braking it. – Pshemo Jan 07 '18 at 17:49

1 Answers1

0

Your first and last line of code are assigning constructor reference to the same class type and it called as Static Binding i.e Compile-time binding.While the second one is typically dynamic binding or runtime binding.

//First Line
Superclass x = new Superclass();
//Third Line
Subclass x = new Subclass();

In this first and third line of code referring to same class constructor.This signifies that in this reference it always points to current instance of the class and Reference static methods and variables.

//Second Line
Superclass x = new Subclass();

Here this approach we call as dynamic binding in java and this is an inheritance property in java to allow child class object referred by parent class reference.This approach is mainly used when you have multiple child classes and all is abstracted to its parent class.So you don't need to bother about creating multiple child references and one can achieve runtime polymorphism.

Below is the example which clear your concept

class Superclass {
   public static void getClassName()
   {
       System.out.println("Superclass static method called");
   }
}
class Subclass extends Superclass {
   public static void getClassName(){
       System.out.println("Subclass ");
   }
   public static void main( String args[]) {
       /* Reference is of Superclass type and object is
        * Subclass type
        */
       Superclass obj = new Subclass();
       /* Reference is of Superclass type and object is
        * of Subclass type.
        */
       Superclass obj2 = new Superclass();
       obj.getClassName();
       obj2.getClassName();
   }
}

//The output will be
Superclass static method called
Superclass static method called

As the reference always refers to parent static method