GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes the constructor(contain arguments) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "the constructor(contain arguments)" Watch "the constructor(contain arguments)" New topic
Author

the constructor(contain arguments)

lang lang
Greenhorn

Joined: Nov 01, 2003
Posts: 8
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();
}
}///:-
David Crossett
Ranch Hand

Joined: Feb 05, 2003
Posts: 102
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".


David Crossett
-nothing important to say, but learnin' plenty-
lang lang
Greenhorn

Joined: Nov 01, 2003
Posts: 8
in super(11),what the '11'represent?
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

It is the value you are passing to a method in the parent class.


Please ignore post, I have no idea what I am talking about.
 
Consider Paul's rocket mass heater.
 
subject: the constructor(contain arguments)