• 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

calling super class constructor?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi the follwing program gives me compile time error saying that
"super() method calls only allowed in constructors"

Please exaplin me the cause for error.

class Sup1{
public Sup1(){
System.out.println("Hai");
}
private Sup1(String str){
System.out.println(str);
}
}

public class Test5 extends Sup1{
private Test5(String str){
System.out.println(str);
super(); //This line causes problem
}
public static void main(String[] args) {
Test5 t5 = new Test5("HI");
}
}
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A call to super(...) can only be the first line of the constructor.

If the first line of a constructor is neither super(...) nor this(...), the compiler inserts "super();" at the beginning of that constructor.
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also notice that, any constructor call MUST be placed at the 1st line of caller constructor.

Nick
 
Ramana Uppala
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohh i got the point.
Thanku
reply
    Bookmark Topic Watch Topic
  • New Topic