aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt regarding invoking a method using super keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt regarding invoking a method using super keyword" Watch "Doubt regarding invoking a method using super keyword" New topic
Author

Doubt regarding invoking a method using super keyword

Rahul Choudhary
Greenhorn

Joined: Jul 25, 2012
Posts: 22


The output is:
Sample1 const
Sample2 const
Sample3 const
sample1 dostuff

My doubt is that super.doStuff() should invoke Sample2 method doStuff() if present. Why has it "propagated" upwards? I thought that happened only with no-arg super() being put implicitly (if not present) as first line of constructor.


gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

doStuff method is declared in Sample1 class. it gets inherited in Sample2 class. Since Sample3 extends Sample2 , Sample3 also inherits teh doStuff() method. now when you invoke super.doStuff(), it will run the doStuff method of Sample2 class. Since doStuff() is not overridden in Sample2 class the doStuff from Sample1 gets called. actually i think your confusion is because you think that when methods or variables are inherited they get COPIED in the class invisibly. actually this is not the case. there is NO COPYING. the class who inherits the method CAN INVOKE THE INHERITED METHOD THROUGH ITS SUPER CLASS. so the superclass methods/variables gets called unless ofcourse you have overridden the methods (overridding does not apply to instance/class variables).

so here also when you did super.doStuff() the doStuff method of Sample1 is called. even if you have called this.doStuff() in go method, there won't be any change in the output , since again the same thing will happen.


regarding your second question, super is not limited to constructors. you can use super to invoke the superclass methods or access instance variables. for e.g.

Rahul Choudhary
Greenhorn

Joined: Jul 25, 2012
Posts: 22
Since doStuff() is not overridden in Sample2 class the doStuff from Sample1 gets called

Wouldn't it be an error that it's overridden but there is no definition in Sample2?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Rahul Choudhary wrote:
Since doStuff() is not overridden in Sample2 class the doStuff from Sample1 gets called

Wouldn't it be an error that it's overridden but there is no definition in Sample2?


overriding is in context to inheritance. if a method is inherited , it can be overridden. although Sample2 does not define its own doStuff method , it can overriden doStuff from Sample1 , since it inherits one. similarly you can overriden doStuff in Sample3 because doStuff is inherited from Sample2 to Sample3.
Rahul Choudhary
Greenhorn

Joined: Jul 25, 2012
Posts: 22
Got it gurpreet Thanks again!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt regarding invoking a method using super keyword