• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

the constructor(contain arguments)

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
It is the value you are passing to a method in the parent class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!