Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

super keyword

 
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i read this statement on javat point website

can anybody confirm if the sentence in bold is right ?

"The super keyword in java is a reference variable which is used to refer immediate parent class object.
Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable."



Is it true that when a subclass instance is created, an instance of the parent is also created ? if yes, can i also know how all this actually works then.

FYI - https://www.javatpoint.com/super-keyword
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is correct.
This means that you have access to all of the public, and protected members of the parent class. You may even have access to the default package private members depending where the class is being defined.
So you could have:
MyParent.java

ChildClass.java

Note that on line 3 of ChildClass I call the method from the parent class using super.myMethod() and on line 4 I print the value of myAge which is also in the parent class.
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ChildClass is calling the instance methods and variables of the parent class using super, due to the parent class being created implicitly.

Note, this works for public access members always. Protected and package private have some rules that they follow which can be difficult to understand.
 
Lilou Laure
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so it means , if the parent had two member fields in it, then even the child class object will have two of those fields created for itself . and this replica of the parent class' members being created in the child class object is what they are referring to as a parent class object , right ?

its not the kind of object that is created using the new operator, right ?
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lilou Laure wrote:if the parent had two member fields in it, then even the child class object will have two of those fields created for itself


Close, not exactly as these super members will act like they are part of the child class, but they are in a slightly different memory space (if you will) and you can override them.
These member fields are still part of the parent and belong to the parent.

If you are inside of the class definition then you access the parent members using super as shown below.
However if you are outside of the class definition then you access these like you would normally and no one would know (or care) where the member came from.
The member could be from any parent along the inheritance path.

Lilou Laure wrote:its not the kind of object that is created using the new operator, right ?


Correct, they just act like normal member methods and variables, which were defined in the class that you are using them in (child class).
But they are not accessed via new.
Everything in the parent which is public is inherited and as long as it's not final or static it can be overwritten.
Or you can access them using the super reference (when in the class).

Override parent members example as seen below, but the super members are still here.
ChildClass.java

If you want, you can take these examples and compile them on your local system to see how they stack up.
Some times the best way to learn is to take a few minutes, edit and compile the code on your local system.
 
Lilou Laure
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks pete .
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies to you Lilou Laure if I seemed rude or I insulted you in any way.
If I did, then please know that I did not mean to.

I do hope that I was truly able to help you out.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lilou Laure wrote:Is it true that when a subclass instance is created, an instance of the parent is also created ? if yes, can i also know how all this actually works then.


No, that's not true! Because if a subclass instance would create a superclass instance as well, then two instances would exist and that's of course not true.

If you create an instance of a class, only one instance will be created but super constructors will of course be executed. So given the following class hierarchythe statement new Cat(); will create one instance of type Cat. And the default constructor of class Cat will invoke the default constructor of class Animal and this constructor will invoke the default constructor of class Object. When the default constructor of class Object finishes, the execution continues with the default constructor of class Animal. Once this constructor is finished, the default constructor of class Cat continues its exection. And once this constructor has finished, the new Cat object is successfully created and ready to use. But there's only one instance/object!
 
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is true that creating a subclass instance does not create a separate superclass instance as well (as this JLS example shows - Example 12.5-1. Evaluation of Instance Creation).

However, that subclass instance can also be seen as an instance of the superclass.

JLS
The form super.Identifier refers to the field named Identifier of the current object, but with the current object viewed as an instance of the superclass of the current class.

 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Cox wrote:However, that subclass instance can also be seen as an instance of the superclass.


True! The instanceof operator on a Cat object will return true for classes Object, Animal, and Cat.
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic