0

I struggle to observe the difference of creating objects with reference to the parent class data type or child class data type (as custom to polymorphism)

Creating two objects without referencing derived class as the data type:

class Main {
  public static void main(String[] args) {
    spaceship Spaceship1 = new spaceship(34.6, "Apollo 11", 1600);
    car Car1 = new car(100.1, "Tesla", "OPEN");
    Spaceship1.go();
    Car1.go();
  }
}
class Vehicle{
  protected double speed;
  protected String name;

  public Vehicle(double speed, String name){
    this.speed = speed;
    this.name = name;
  }
  public void go(){}
}
class spaceship extends Vehicle{
  private int altitude;
  public spaceship(double speed, String name, int altitude){
    super(speed, name);
    this.altitude = altitude;
  }
  public void go(){
    System.out.println(this.name + " is going at the speed of " + this.speed +" at the altitude of " + this.altitude);
  }
}
class car extends Vehicle{
  private String window;
  public car(double speed, String name, String window) {
    super(speed, name);
    this.window = window;
  }
  public void go(){
    System.out.println(this.name + " is going at the speed of " + this.speed + " with its windows " + this.window);
  }
} 

Creating two objects referencing derived class as the data type:

class Main {
  public static void main(String[] args) {
    Vehicle Spaceship1 = new spaceship(34.6, "Apollo 11", 1600);
    Vehicle Car1 = new car(100.1, "Tesla", "OPEN");
    Spaceship1.go();
    Car1.go();
  }
}
class Vehicle{
  protected double speed;
  protected String name;

  public Vehicle(double speed, String name){
    this.speed = speed;
    this.name = name;
  }
  public void go(){}
}
class spaceship extends Vehicle{
  private int altitude;
  public spaceship(double speed, String name, int altitude){
    super(speed, name);
    this.altitude = altitude;
  }
  public void go(){
    System.out.println(this.name + " is going at the speed of " + this.speed +" at the altitude of " + this.altitude);
  }
}
class car extends Vehicle{
  private String window;
  public car(double speed, String name, String window) {
    super(speed, name);
    this.window = window;
  }
  public void go(){
    System.out.println(this.name + " is going at the speed of " + this.speed + " with its windows " + this.window);
  }
} 

Is the difference only intended to be on a syntax level or are there some practical differences?

  • Possibly related: [What does it mean to "program to an interface"?](https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface). – Slaw Nov 14 '21 at 07:00
  • Thank you, the stack was very useful, but it is talking about interfaces, so is the concept of polymorphism related to abstraction? – Displayname Nov 14 '21 at 07:12
  • 1
    We may not always be able to determine at compile-time which (concrete) implementation is returned. Let us Imagine, for example, that we write a program to manage `Pet`s. Depending on the user's input, either a `Dog`, `Cat`, `Hamster`, `Bunny`, ... is created. On the calling side, we only know that we create a `Pet`, hence we can only use a variable of type `Pet`. – Turing85 Nov 14 '21 at 07:21
  • Thank you for the explanation. And that is an application of polymorphism, correct? Not interface? Because that sounds a lot like the explanation of an interface from the link above? – Displayname Nov 14 '21 at 07:33
  • 3
    May also help: https://en.wikipedia.org/wiki/Polymorphism_(computer_science) – Slaw Nov 14 '21 at 07:50
  • Think about this. You want to ship `Vehicle`s, so you get a "container, which can contain `Vehicle`s. The "container" doesn't care about what type of "vehicle" you put in it, only that they are of a type of `Vehicle`. When the "container" arrives at the destination, the "unpacker" wants to send `Car`s to the dealerships and `SpaceShips` to NASA or SpaceX. It will inspect each `Vehicle` in turn to see what actual type it is and send it onto the appropriate destination. – MadProgrammer Nov 14 '21 at 20:20
  • You can also expand this concept beyond what you know (or control). A system/API may "expose" certain types. You as the consumer, don't really care how those types are implemented, on that the conform to a specific set of requirements. – MadProgrammer Nov 14 '21 at 20:21
  • `interface` and `abstract` class are a part of how polymorphism can be implemented in Java, you can do it directly via `class`, but you will get more benefit from using `interface`s in terms of flexibility. – MadProgrammer Nov 14 '21 at 20:23
  • Thank you for these wonderful explanation’s I think it really does clear it up now. I am starting to work on this project where a user can grow bacteria and am using an abstract class for the bacteria types as well as an interface for spells which can boos the growth speed etc. , to really nail it in :) – Displayname Nov 15 '21 at 01:10

0 Answers0