File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q? From Valiveru's Site

 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to compile this program and was expecting a compilation error, but it did not give.
class BaseClass
{
public void aMethod()
{
System.out.println("I am a BaseClass method");
}
}
public class Cals extends BaseClass
{
public void aMethod()
{
System.out.println("I am a SubClass method");
super.aMethod();
}
public static void main(String [] arg)
{
Cals sc = new Cals();
sc.aMethod();
}
}
I was under the impression that the super.aMethod() is called after System.out.println() statement so it will give an error. Can anybody please explain why this is happening?
Thanks,
Subba Rao
 
Vlad G
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought this restriction only applies to constructors.
VG.
 
bill bozeman
Ranch Hand
Posts: 1070
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, it is only with constructors that super or this have to be the first line of the body. For other methods, they can be anywhere.
Bill
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
Yea! i do agree with you and Vlad.
Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic