0

Why is it useful to use a superclass as a reference while creating an object of subclass in Java? superclass obj = new subclass()

I have done a lot of research and all I can find is that in an Array you can declare the Array as type Superclass and then create objects like the above (superclass type referencing subclass object) and insert into the array. However I have tried to create subclass objects without a superclass reference and insert into an array and it works. I am not understanding why someone would ever create superclass obj = new subclass()

See code:

import java.util.*;

 public class Pet
 {
     private String name;
     private String type;

     public Pet(String n, String t)
     {
        name = n;
        type = t;
     }
     public String toString()
     {
        return name + " is a " + type;
     }

     public static void main(String[] args)
     {
         
        Pet[] animals = new Pet [5];
         // This still works to insert into animals array of type Pet. Why?
         Dog p1 = new Dog("Fido");
         //According to various sources the above should not work but the following should
         //Pet p1 = new Dog("Fido);
         animals[0] = p1;

         // This loop will work for all subclasses of Pet
         for(Pet p : animals)
         {
            System.out.println(p);
         }
     }
  }
  class Dog extends Pet
  {
     public Dog(String n)
     {
       super(n, "dog");
     }
  }
Sarah
  • 23
  • 5
  • 1
    You already used `polymorphism` tag. Work through a couple of tutorials and you will come up with the answer. – PM 77-1 Mar 01 '23 at 18:14
  • Even if I have a cat that extends pet, I can still create a cat and I can still insert it into my animals array without having to use a superclass reference. That's exactly my question, even after researching I still don't see a point to using a superclass reference because I can accomplish the same thing without it. – Sarah Mar 01 '23 at 18:17
  • My array is of type Pet. From research everyone says that if array is type Pet the only way to insert an object is if the object is a superclass reference to a subclass but that is just not true. I can insert an object that was created without a superclass reference. – Sarah Mar 01 '23 at 18:20
  • @user16320675 my array is type pet not dog! – Sarah Mar 01 '23 at 18:20
  • I am wondering why anyone would do the following: Pet test = new Dog() Why would I create a superclass reference to a subclass object ever? From research everyone says that the reason you would do that is to be able to insert that into an Array of Pet type, however even if I create my dog as Dog test = new Dog() I can still insert that into my Pet array – Sarah Mar 01 '23 at 18:26
  • Added an example in the code. – Sarah Mar 01 '23 at 18:30
  • I am in an AP course and AP classroom states this as a fact. Additionally here is another link to stack overflow where this came up: https://stackoverflow.com/questions/26265164/purpose-of-assigning-objects-of-subclass-to-a-superclass-reference – Sarah Mar 01 '23 at 18:37
  • Good question. It’s used little, at least where I have looked. I sometimes see it for separating actual implementation from use, as in `Thing th = new SomeVerySpecificImplementationOfThing()` where all you want is a thing. Advantages include: You may later change your mind and use a different implementation and be sure you don’t need to change the rest of the code. It it saves keystrokes. :-) – Ole V.V. Mar 01 '23 at 18:39
  • Thank you, any idea why AP classroom and others say that you would need to create each object as a reference of a superclass when inserting into an Array of superclass type? That's just not true from what I have tried (as exhibited in above code) – Sarah Mar 01 '23 at 18:42
  • I see that it is not true, but am still wondering why you would ever create objects that are referenced by a superclass. I see no point to that (this is my original question!) – Sarah Mar 01 '23 at 18:44
  • 1
    I mentioned two points.... 1) if you do `Dog test = new Dog();` you cannot re-use that variable for `test = new Cat();` - 2) (less strict) you (the developer) wants to *express* that a `Pet` is *needed*, not just a `Dog` (why ever) - Ole [mentioned](https://stackoverflow.com/questions/75607483/using-superclass-as-a-reference-while-creating-an-object-of-subclass#comment133390721_75607483) a third || We also have no idea why your AP classroom said so, better ask there/they – user16320675 Mar 01 '23 at 18:47
  • Back when the `DateFormat` and `SimpleDateFormat` classes were used -- which they certainly should not be anymore -- people would often write `DateFormat df = new SimpleDateFormat("dd.MM.yy");`. It would allow then to change it later to `DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);`, for example, without changing the declared type nor the code using it. Today instead use `DateTimeFormatter`, and you will not have the consideration. – Ole V.V. Mar 02 '23 at 06:04

0 Answers0