File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt regarding invoking a method using super keyword

 
Rahul Choudhary
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 924
1
Fedora Java Netbeans IDE
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it gurpreet Thanks again!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic