• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

super

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class BaseClass {
public void amethod() {
System.out.println("I am a base class");
}
}
public class SubClass extends BaseClass {
public void amethod() {
System.out.println(" I am im subclass");
super.amethod();
}
public static void main(String args[]) {
SubClass sc=new SubClass();
sc.amethod();
}
}
I thought the above code will not compile cause super is not the first line.But compiles fine.Can anybody explain me why?
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Susmita ,
Acc to RHE ,it is important to know that the superclass constructor must be called before any reference is made to any part of the object from where the call is placed.In your pgm,you are just printing out a statement and hence it works fine!
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only in the constructor, the call to super() ot this() has to be the first line(if you want one).
--Farooq
 
reply
    Bookmark Topic Watch Topic
  • New Topic