0

main class->>>

public class scoreMain { public static void main(String[] args) { // Football Score board Score scoreObject = new Score(); Score scoreObject1 = new Score(1); Score scoreObject2 = new Score(1,2); Score scoreObject3 = new Score(1,2,3); } } 

and constructor class -->>>

public class Score { public void Score() { Score(0,0,0); } public void Score(int x) { Score(x,0,0); } public void Score(int x,int y) { Score(x,y,0); } public String Score(int x,int y,int z) { Score(x,y,z); return String.format("%d/%d%d",x,y,z); } } 

but it shows error when creating objects ... the constructor score(int) is undefined the constructor score(int int ) is undefined the constructor score(int int int ) is undefined

0

3 Answers 3

4

Constructors do not return anything. Not String nor void or anything else. You should just change the constructors as follows:

public class Score { public Score() { this(0,0,0); } public Score(int x) { this(x,0,0); } public Score(int x,int y){ this(x,y,0); } public Score(int x,int y,int z) { Score(x,y,z); // Not sure what's this - you can't do a recursive constructor call. Doesn't make any sense return String.format("%d/%d%d",x,y,z); // Remove the return statment. } } 

Also notice to not only not do a return on any value, but also you have a recursive call for the constructor in the last overloaded constructor. It doesn't make any sense and won't work.

BTW - the correct way of overloading constructors is by calling this() inside the overloads and have only one implementation. Look at this question for further details.

Sign up to request clarification or add additional context in comments.

Comments

1

In your code, there are only methods, no constructors. Constructors have no return type.

Example:

public class Score { public Score() { this(0,0,0); } public Score(int x) { this(x,0,0); } public Score(int x,int y) { this(x,y,0); } public Score(int x,int y,int z) { //?? //constructors cannot return anything! //return String.format("%d/%d%d",x,y,z); } } 

Comments

0

Also, to invoke other constructor of the same class, you should use keyword this, not class name.

So, first constructor would look like this

public void Score() { this(0,0,0); } 

Also, what you have here is a method, not a constructor. Constructors don't have return types

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.