 |
BOOK: Beginning Java 2  | This is the forum to discuss the Wrox book Beginning Java 2, SDK 1.4 Edition by Ivor Horton; ISBN: 9780764543654 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning Java 2 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

December 7th, 2003, 07:16 PM
|
|
Registered User
|
|
Join Date: Dec 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
basic error on Polymorphism
I have found this code from the web.Which looks perfect to me regarding simple polymorphism.
There is an Animal Class as under.And the polymorphism class after creating an array object is trying to execute the selected Animal.
The code compiles perfectly .But on executing the executable I am getting this error :
Exception in thread main
java.lang.NoSuchMethodError: void Animal.<init>()
at Dog.<init>(Dog.java:11)
at TryPolymorphism.main(TryPolymorphism.java:8)
Kindly guide me with this error.
Thanks
Aniaml Class
-------------
public class Animal
{
public Animal(String aType)
{
type = new String(aType);
}
public String toString()
{
return "This is a " + type;
}
// Dummy method to be implemented in the derived classes
public void sound()
{}
private String type;
}
TryPolymorphism.java
------------------
import java.util.Random;
public class TryPolymorphism
{
public static void main(String[] args)
{
// Create an array of three different animals
Animal[] theAnimals = {
new Dog("Rover", "Poodle"),
new Cat("Max", "Abyssinian"),
new Duck("Daffy","Aylesbury")
};
Animal petChoice; // Choice of pet
Random select = new Random(); // Random number generator
// Make five random choices of pet
for(int i = 0; i < 5; i++)
{ // Choose a random animal as a pet
petChoice = theAnimals[select.nextInt(theAnimals.length)];
System.out.println("\nYour choice:\n" + petChoice);
petChoice.sound(); // Get the pet's reaction
}
}
}
Dog.java
---------
public class Dog extends Animal
{
public Dog(String aName)
{
super("Dog"); // Call the base constructor
name = aName; // Supplied name
breed = "Unknown"; // Default breed value
}
public Dog(String aName, String aBreed)
{
super("Dog"); // Call the base constructor
name = aName; // Supplied name
breed = aBreed; // Supplied breed
}
// Present a dog's details as a string
public String toString()
{
return super.toString() + "\nIt's " + name + " the " + breed;
}
// A barking method
public void sound()
{
System.out.println("Woof Woof");
}
private String name; // Name of a Dog
private String breed; // Dog breed
}
There are other object classes Cat.java, Duck.java
|
|

December 8th, 2003, 08:15 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The error message is telling you that java can't find the appropriate constructor. Check the spelling of the constructor in Animal, as it needs to match the class name exactly. Also, whilst not directly related to your problem; shouldn't your Animal class and it's associated sound() method be declared as abstract?
Cheers
Martyn
|
|

December 8th, 2003, 03:11 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I've just got home and loaded your code, which compiled and ran fine, so the problem must be with your local implementation. As I mentioned in my previous post: you should check the spelling of the constructor in class Animal, as I'm pretty certain that the problem will be there.
cheers
Martyn
|
|

December 8th, 2003, 03:41 PM
|
|
Registered User
|
|
Join Date: Dec 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you very much, But I have cut & pasted the code above exactly from where I am executing it.The Animal.java file name above I miss typed here, not in the code.
And as you can see above the Animal class has the required String Type constructor.But it amazes me the fact that it successfully executed on your machine.Kindly guide me with some more possibilities.
Thanks
|
|

December 8th, 2003, 04:16 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
You will find below all of the classes that I implemented. These were taking from the old Wrox website a couple of years ago. They mimic yours, and all compiled and ran fine. The only apparent difference id I have added the abstract modifier to Animal, however, the code will compile fine without it, if that is what you prefer. Let me know how you get on.
Cheers
Martyn
public abstract class Animal
{
public Animal(String aType)
{
type = new String(aType);
}
public String toString()
{
return "This is a " + type;
}
// Dummy method to be implemented in the derived classes
public abstract void sound();
private String type;
}
public class Dog extends Animal
{
public Dog(String aName)
{
super("Dog"); // Call the base constructor
name = aName; // Supplied name
breed = "Unknown"; // Default breed value
}
public Dog(String aName, String aBreed)
{
super("Dog"); // Call the base constructor
name = aName; // Supplied name
breed = aBreed; // Supplied breed
}
// Present a dog's details as a string
public String toString()
{
return super.toString() + "\nIt's " + name + " the " + breed;
}
// A barking method
public void sound()
{
System.out.println("Woof Woof");
}
private String name; // Name of a Dog
private String breed; // Dog breed
}
public class Cat extends Animal
{
public Cat(String aName)
{
super("Cat"); // Call the base constructor
name = aName; // Supplied name
breed = "Unknown"; // Default breed value
}
public Cat(String aName, String aBreed)
{
super("Cat"); // Call the base constructor
name = aName; // Supplied name
breed = aBreed; // Supplied breed
}
// Return a String full of a cat's details
public String toString()
{
return super.toString() + "\nIt's " + name + " the " + breed;
}
// A miaowing method
public void sound()
{
System.out.println("Miiaooww");
}
private String name; // Name of a cat
private String breed; public class Duck extends Animal
{
public Duck(String aName)
{
super("Duck"); // Call the base constructor
name = aName; // Supplied name
breed = "Unknown"; // Default breed value
}
public Duck(String aName, String aBreed)
{
super("Duck"); // Call the base constructor
name = aName; // Supplied name
breed = aBreed; // Supplied breed
}
// Return a String full of a duck's details
public String toString()
{
return super.toString() + "\nIt's " + name + " the " + breed;
}
// A quacking method
public void sound()
{
System.out.println("Quack quackquack");
}
private String name; // Duck name
private String breed; // Duck breed
}// Cat breed
import java.util.Random;
public class TryPolymorphism
{
public static void main(String[] args)
{
// Create an array of three different animals
Animal[] theAnimals = {
new Dog("Rover", "Poodle"),
new Cat("Max", "Abyssinian"),
new Duck("Daffy","Aylesbury")
};
Animal petChoice; // Choice of pet
Random select = new Random(); // Random number generator
// Make five random choices of pet
for(int i = 0; i < 5; i++)
{ // Choose a random animal as a pet
petChoice = theAnimals[select.nextInt(theAnimals.length)];
System.out.println("\nYour choice:\n" + petChoice);
petChoice.sound(); // Get the pet's reaction
}
}
}
}
|
|

December 8th, 2003, 04:19 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorry the code I posted seems to have got mixed up. Here it is again:
public abstract class Animal
{
public Animal(String aType)
{
type = new String(aType);
}
public String toString()
{
return "This is a " + type;
}
// Dummy method to be implemented in the derived classes
public abstract void sound();
private String type;
}
public class Dog extends Animal
{
public Dog(String aName)
{
super("Dog"); // Call the base constructor
name = aName; // Supplied name
breed = "Unknown"; // Default breed value
}
public Dog(String aName, String aBreed)
{
super("Dog"); // Call the base constructor
name = aName; // Supplied name
breed = aBreed; // Supplied breed
}
// Present a dog's details as a string
public String toString()
{
return super.toString() + "\nIt's " + name + " the " + breed;
}
// A barking method
public void sound()
{
System.out.println("Woof Woof");
}
private String name; // Name of a Dog
private String breed; // Dog breed
}
public class Cat extends Animal
{
public Cat(String aName)
{
super("Cat"); // Call the base constructor
name = aName; // Supplied name
breed = "Unknown"; // Default breed value
}
public Cat(String aName, String aBreed)
{
super("Cat"); // Call the base constructor
name = aName; // Supplied name
breed = aBreed; // Supplied breed
}
// Return a String full of a cat's details
public String toString()
{
return super.toString() + "\nIt's " + name + " the " + breed;
}
// A miaowing method
public void sound()
{
System.out.println("Miiaooww");
}
private String name; // Name of a cat
private String breed; // Cat breed
}
public class Duck extends Animal
{
public Duck(String aName)
{
super("Duck"); // Call the base constructor
name = aName; // Supplied name
breed = "Unknown"; // Default breed value
}
public Duck(String aName, String aBreed)
{
super("Duck"); // Call the base constructor
name = aName; // Supplied name
breed = aBreed; // Supplied breed
}
// Return a String full of a duck's details
public String toString()
{
return super.toString() + "\nIt's " + name + " the " + breed;
}
// A quacking method
public void sound()
{
System.out.println("Quack quackquack");
}
private String name; // Duck name
private String breed; // Duck breed
}
import java.util.Random;
public class TryPolymorphism
{
public static void main(String[] args)
{
// Create an array of three different animals
Animal[] theAnimals = {
new Dog("Rover", "Poodle"),
new Cat("Max", "Abyssinian"),
new Duck("Daffy","Aylesbury")
};
Animal petChoice; // Choice of pet
Random select = new Random(); // Random number generator
// Make five random choices of pet
for(int i = 0; i < 5; i++)
{ // Choose a random animal as a pet
petChoice = theAnimals[select.nextInt(theAnimals.length)];
System.out.println("\nYour choice:\n" + petChoice);
petChoice.sound(); // Get the pet's reaction
}
}
}
|
|

December 8th, 2003, 05:22 PM
|
|
Registered User
|
|
Join Date: Dec 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you very much again, Sir.But the amaizing part is that reason why the system was erroring out is not clear, yet.After I successfully executed your forwarded code above, I couldn't find anything different in my code, just the abstract definition of Animal class and Sound menthod which was not a definite requirement.
So I cut and pasted my code above back into my files to generate the error.But to my big surprise, I am unable to do that anymore now , i am also getting the perfect out put with the code I had pasted above.Looks like an impossible possibility, But thank you very much for all your help.
|
|

December 8th, 2003, 06:09 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I'm gald that you managed to get everything working OK. In the meantime I've been 'playing' around with your code. The only way that I could recreate your error was if I compiled all of the programs and then removed the constructor from Animal and then recompiled the class. It can't seem to occur any other way. I'm not suggesting that you did that, but do you have any other incomplete versions of class Animal on your system that might exist higher up in your class path?
If you are curious, try what I'm suggesting and see if that causes the original problem. By-the-way, I was wrong about suggesting that you might have not correctly declared the constructor, that would have been picked up by the compiler, whereas all of your programs compiled OK.
Cheers
Martyn
|
|
 |