| Author |
Abstract and array
|
John Valiant
Greenhorn
Joined: Mar 01, 2012
Posts: 26
|
|
Hi
My issue is i cant get the arrays correct,with abstract class.Can some one highlight to me the errors in class Example1.I want to create 2 extra pets (iguana & hamster) but i am getting it wrong.
below are the errors from my complier
23.java:37: error: constructor GoldFish in class GoldFish cannot be applied to given types;
owner.pets[2]=new GoldFish();
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
23.java:38: error: constructor Dog in class Dog cannot be applied to given types;
owner.pets[3]=new Dog("Hamster");
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
23.java:39: error: constructor Dog in class Dog cannot be applied to given types;
owner.pets[4]=new Dog("Iguana");
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
3 errors
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2946
|
|
|
From what the error says- your Dog doesn't have a constructor which takes a String and the GoldFish class doesnt have a constructor which takes no arguments. So you need to be adding them.
|
Mohamed Sanaulla | My Blog
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4901
|
|
John Valiant wrote:Can some one highlight to me the errors in class Example1.I want to create 2 extra pets (iguana & hamster) but i am getting it wrong.
Well, your Goldfish class has a contructor that takes a String, and your Dog class doesn't; yet you're trying to add a Dog with a String and a Goldfish without one.
Winston
[Edit] Too slow
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
John Valiant
Greenhorn
Joined: Mar 01, 2012
Posts: 26
|
|
|
ok thanks but how can this be solved then? should i be making changes to array in class example?
|
 |
John Valiant
Greenhorn
Joined: Mar 01, 2012
Posts: 26
|
|
This time 1 error....errrrr any idea how to solve this?
23.java:37: error: constructor GoldFish in class GoldFish cannot be applied to given types;
owner.pets[2]=new GoldFish();
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
1 error
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2946
|
|
|
owner.pets[2]=new GoldFish(); this line is using a constructor which is non existent in the GoldFish class. Either change this to pass a String or add a Default constructor to GoldFish class. Once you provide an overloaded constructor the compiler doesn't add the No-Args (default) constructor, so in such cases one has to provide the default constructor.
|
 |
John Valiant
Greenhorn
Joined: Mar 01, 2012
Posts: 26
|
|
Thanks Mohammad, managed to get things running but a few queries(pardon my ignorance).
1.Why didnt the complier recognize the below code as a constructor?
2.if i want to use the getAction() in ,let say, hamster,how can i go about doing that?
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2946
|
|
John Valiant wrote:Thanks Mohammad, managed to get things running but a few queries(pardon my ignorance).
1.Why didnt the complier recognize the below code as a constructor?
It is recognised as a constructor. And that is why these lines owner.pets[3]=new GoldFish("Hamster"); and the other worked.
John Valiant wrote:2.if i want to use the getAction() in ,let say, hamster,how can i go about doing that?
if you mean invoking getAction() on owner.pets[3]=new GoldFish("Hamster"); then it would be owner.pets[3].getAction();
On a side note: please indent your code so that its more readable.
|
 |
John Valiant
Greenhorn
Joined: Mar 01, 2012
Posts: 26
|
|
ok it worked only when i overloaded it .GoldFish(){
}
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 6109
|
|
John Valiant wrote:ok it worked only when i overloaded it  .GoldFish(){
Of course. That's what people have been telling you all along. You have this:
So since you're calling both a no-arg Goldfish c'tor and a String-arg Goldfish c'tor, you need to define both a no-arg Goldfish c'tor and a String-arg Goldfish c'tor.
One of the rules of constructors that was mentioned above is: If you don't define any c'tors on your class, the compile inserts a no-arg c'tor that does nothing. But if you define one or more, then the compiler no longer inserts one.
If the no-arg Goldfish() {} c'tor doesn't exist--either from the compiler or from you--there's no way that first line can work.
|
 |
 |
|
|
subject: Abstract and array
|
|
|