Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In java can super() be used to call any parent method or just the parent constructor

Is it possible to call any parent class methods using super() from child class method or super is used just for calling parent constructor

like image 394
Mona Dhar Avatar asked Sep 05 '25 17:09

Mona Dhar


1 Answers

For calling methods the syntax is super.methodName(). Just super() will call the constructor.

It's very similar to this keyword but for parent.

this() calls this classes constructor from another constructor. super() calls the parents constructor from childs constructor.

this.methodName() calls the method of the current class, super.methodName() calls the method of parent class.

EDIT: As @harry has mentioned in the comment, the parent's method should be visible to the child to actually be able to use super.methodName(). Private methods in the parent cannot be accessed.

like image 114
Codebender Avatar answered Sep 07 '25 12:09

Codebender