Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java polymorphism creating a subclass object using its superclass variable

So I am a student and in the process of learning Java. There is one concept that I am having a difficult time grasping and am hoping that someone could shed some light on this for me. My question is regarding polymorphism. Let's say for example I have the following code.

Animal a = new Lizard("Lizzy", 6);  //Lizard extends Animal
  1. From what I understand, since the variable type is Animal, a will have all the characteristics of an Animal. But, since the object created is a Lizard, any overridden methods in the Lizard class will be used instead of those in the Animal class. Is this correct>

  2. Also, which classes constructor will be used while creating a?

Thanks for any help. I have looked quite

like image 770
Kevin Goodenough Avatar asked Dec 08 '12 04:12

Kevin Goodenough


People also ask

Can we create object of superclass in subclass?

No. It makes zero sense to allow that. The reason is because subclasses generally define additional behavior. If you could assign a superclass object to a subclass reference, you would run into problems at runtime when you try to access class members that don't actually exist.

Can a superclass variable reference a subclass object?

Yes, the super class reference variable can hold the sub class object actually, it is widening in case of objects (Conversion of lower datatype to a higher datatype).

Can we create object of superclass in Java?

Only a subclass object is created that has superclass variables. This situation is different from a normal assumption that a constructor call means an object of the class is created, so we can't blindly say that whenever a class constructor is executed, the object of that class is created or not. Example: Java.

How do you call a superclass method using a subclass object in Java?

Subclass methods can call superclass methods if both methods have the same name. From the subclass, reference the method name and superclass name with the @ symbol.


1 Answers

1.From what I understand, since the variable type is Animal, a will have all the characteristics of an Animal. But, since the object created is a Lizard, any overridden methods in the Lizard class will be used instead of those in the Animal class. Is this correct>

yes, you are Right.

2.Also, which classes constructor will be used while creating a?

          Animal a = new Lizard("Lizzy", 6);  //Lizard extends Animal

As, Lizard is a subclass of Animal, First, Lizards constructor will be invoked, then from Lizards constructor, there will be a call to Animal constructor as the first line in your Lizard constructor would be super() by default unless you call an overloaded constructor of Lizard using this(). In Animal constructor there will be another call to super() in the first line. assuming Animal doesn't extend any class, java.lang.Object's constructor will be invoked as java.lang.Object is the super class of every object.

  public Object() {

    }
    Class Animal {
     public Animal(){
      //there will be a super call here like super()
    }

    class lizard extends Animal {
    public Lizard(your args) {
       //there will be a super() call here and this call's animal's no-args constructor
     }
    }

 }

The order of execution would be

  1. Lizards constructor will be invoked
  2. unless there is a this() call to an overloaded constructor, a call to super() i.e., call's Animals no-args Constructor
  3. java.lang.Object's Constructor will be invoked from animal using super()
  4. java.lang.Object's constructor code will execute
  5. Animals constructor code will execute
  6. Lizards constructor code will execute
like image 196
PermGenError Avatar answered Jan 01 '23 21:01

PermGenError