This site requires JavaScript, please enable it in your browser!
Greenfoot back
jeffca51
jeffca51 wrote ...

2014/12/5

constructor tile in class tile cannot be applied to given types

jeffca51 jeffca51

2014/12/5

#
I'm very new to Java but I'm trying to help me son learn it. Currently we have these classes:
1
2
3
4
5
6
7
8
9
10
public class tile extends GreenfootImage
{
  
    public tile(String filename) {
         
        super(filename);
         
    }
    
}
1
2
3
4
5
6
7
8
9
10
public class water extends tile
{
 
    public void water() {
         
        super("water.png");
         
    }
        
}
1
2
3
4
5
6
7
8
9
10
public class ground extends tile
{
     
    public void ground() {
         
        super("ground.png");
         
    }
 
}
Now my expectation is that elsewhere in the code we can say new water() or new land() to create those tiles. However, when compiling the "class" part of "public class water extends tile" highlights, and the error says:
constructor tile in class tile cannot be applied to given types; required: java.long.String found: no arguments reason actual and formal arguments lists differ in length
Now I've tried changing the code to say:
1
2
3
4
5
6
7
8
9
10
public class water extends tile
{
 
    public void water(String whatever) {
         
        super("water.png");
         
    }
        
}
and elsewhere saying "new water("");". But that doesn't appear to have any effect. I'm sure it's a simple Java thing that I don't know, but what's wrong here?
danpost danpost

2014/12/5

#
As you have it, the water and ground class do not have constructors in them; so, the compiler looks to the tile constructor which requires you supply a String. What you have in the water and ground classes are methods -- methods have return types and constructors do not. Remove the 'void' from line 4 in both classes and it should work as expected (this removal make the blocks constructors instead of methods).
jeffca51 jeffca51

2014/12/5

#
There, we go: the bit of Java I didn't know. Methods have return types, constructors don't. Alright, everything works as expected now. Thank you.
You need to login to post a reply.