Page 1 of 1

About Question enthuware.ocajp.i.v7.2.952 :

Posted: Mon Apr 17, 2017 7:09 am
by Chandu
Which of the following lines of code that, when inserted at line 1, will make the overriding method in SubClass invoke the overridden method in BaseClass on the current object with the same parameter.
class BaseClass{
public void print(String s) { System.out.println("BaseClass :"+s); }
}
class SubClass extends BaseClass{
public void print(String s){
System.out.println("SubClass :"+s);
super.print(s);
}
public static void main(String args[]){
SubClass sc = new SubClass();
sc.print("location");
}
}

//How come super statement being right here even though it isnt the first line of the constructor?

Re: About Question enthuware.ocajp.i.v7.2.952 :

Posted: Mon Apr 17, 2017 7:41 am
by admin
Only the call to super class's constructor need to be the first statement. The syntax for that is super(<params>). You can use super dot syntax to call a method of the super class anywhere.