java - Regarding Super keyword? -


what need jvm call superclass constructor?

public class test1{     public void dis()     {         system.out.println("in test1");     } }  public class test2 extends test1{     public void dis()     {         system.out.println("inside test2");         super.dis();     } } 

since dis() instance method, safe assume super pointing parent class object since constructor automatically called?

the keyword super not point "parent class object". name qualifier can refer method defined in parent class for current object. thus, in following:

public class test2 extends test1{     public void dis()     {         system.out.println("inside test2");         super.dis();     } } 

the call super.dis() invokes dis() method defined in parent class for object. not invoke dis() method of other object named super.

slightly different things going on constructors. every constructor must start call constructor in parent class. can explicitly using super keyword:

public class test2 extends test1{     public test2() {         super(); // explicitly invokes parent class default constructor         . . .     } } 

if don't explicitly call particular parent class constructor, compiler automatically inserts call default (no-argument) parent class constructor. call parent class constructor (if present) must first statement in constructor. within method, however, super. can used qualifier name wherever used (provided name member of parent class).


Comments

Popular posts from this blog

javascript - JS causing window size to be bigger than necessary - Dropdown bug -

How to mention the localhost in android -

php - Calling a template part from a post -