• 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

the constructor(contain arguments)

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yesterday I saw a code ,I have something to ask,hope to help me ,thank you!
The code is as follows:
class Game{
Game(int i){
System.out.println("Game constructor");
}
}
class BoardGame extends Game{
BoardGame(int i){
supper(i); //what does super mean,how to use it,if we can not write it here.
System.out.println("BoardGame constructor");
}
}
public class Chess extends BoardGame{
Chess(){
super(11);//the argument'11'here which I am confused about
System.out.println("Chess constructor");
}
public static void main(String[] args){
Chess x=new Chess();
}
}///:-
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When using 'super', you MUST place it in your definition BEFORE anything else... in your example, the super() must go above the line of code it is currently under. What 'super' does is call the default constructor of the class that it inhereted from (hence, it must be the first call made - to create the object). Sometimes 'super' will have arguments, sometimes not. For example,

In the above code, the call to 'super' is a call to the JFrame class, and constructs a framed window with the title "Digital Clock".
 
lang lang
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in super(11),what the '11'represent?
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is the value you are passing to a method in the parent class.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic