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);
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{
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With