Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How super is implemented in Java?

Where the "super" is actually defined? [When we're using super.someMethod()]. Is it defined as a field in java.lang.Object class or java.lang.Class class?

When we're calling from a subclass, super contains the reference to it's superclass.In the same manner the super in superclass itself has reference to it's superclass [In this way upto java.lang.Object]. So, how java injects the superclass references to the "super" field which enables us to call superclass methods ?

Is there any under the hood implementaations like the following:

    Class current = this.getClass();
    Class parent = current.getSuperclass();
    System.out.println("name is =" + parent);
like image 261
Debadyuti Maiti Avatar asked Feb 23 '12 15:02

Debadyuti Maiti


People also ask

What is super implementation in Java?

1) super is used to refer immediate parent class instance variable. We can use super keyword to access the data member or field of parent class. It is used if parent class and child class have same fields. class Animal{

What is super implementation?

super like this is a keyword. It has a meaning in Java which is not reflected in the Java byte code. It means my parent classes' implementation (which could be inherited) At the byte code level it will call the method for its parent class explicitly.

Is super () automatically called?

With super(parameter list) , the superclass constructor with a matching parameter list is called. Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass.

What is super in Java with example?

Example 4: Use of super() Here, when an object dog1 of Dog class is created, it automatically calls the default or no-arg constructor of that class. Inside the subclass constructor, the super() statement calls the constructor of the superclass and executes the statements inside it.


2 Answers

super like this is a keyword. It has a meaning in Java which is not reflected in the Java byte code. It means my parent classes' implementation (which could be inherited)

At the byte code level it will call the method for its parent class explicitly.

like image 155
Peter Lawrey Avatar answered Sep 28 '22 04:09

Peter Lawrey


super is an object-reference specific keyword that ascends the object hierarchy beginning from the calling object until it finds an ancestor object that has a matching method signature or property/field. Refer to Java documentation.

like image 23
Web User Avatar answered Sep 28 '22 02:09

Web User