In inheritance, when an object is created for subclass, is an object also created for its superclass?

StackOverflow https://stackoverflow.com/questions/22556237

Frage

  1. In inheritance, when an object is created for subclass, is an object also created for its superclass?
    I find the answer to the above question as yes, it is created. Am I right?

  2. Based on the assumption that it is right, I solved this question and I found the answer to be 4. Am I right?

  3. Is long a primitive type or wrapper class?

Question : When last brace is reached, how many objects are eligible for the garbage collection?

interface Animal {
   void makeNoise();
 }

class Horse implements Animal {
Long weight = 1200L;//here is Long a primitive variable or a wrapper class??
                    //If it is a wrapper class object, I think the answer to the   
                    // question would be 6  
public void makeNoise() {
System.out.println("whinny");
}
}

public class Icelandic extends Horse {

public void makeNoise() {
System.out.println("vinny");
}

public static void main(String[] args) {
Icelandic i1 = new Icelandic();
Icelandic i2 = new Icelandic();
Icelandic i3 = new Icelandic();

i3 = i1; i1 = i2; i2 = null; i3 = i1;
}
}
War es hilfreich?

Lösung

In inheritance, when an object is created for subclass, is an object also created for its superclass? I find the answer to the above question is yes, its created.Am I right?

No, it won't create a super class object when you create sub class object. Sub class object will have super class functionalists by inheriting from Super Class. Only one object will be created when you create an sub class object

Is long a primitive type or wrapper class??

long is an primitive data type, Long is corresponding Wrapper class.

Question : When last brace is reached, how many objects are eligible for the garbage collection?

Yes, There will be 6 objects, including 3 Long objects

Andere Tipps

  1. It creates a single object which is in both the specific class it was created as and any/all superclasses. Much in the way Fido is both a dog, a mammal and an animal.
  2. Long is a wrapper; long is a primitive (note the case of the first letter).
  3. You've created 3 Icelandics, each of which has a Long: 6 objects. Since you are at the end of the program, I'd expect all 6 to be eligible for collection.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top