Results 1 to 7 of 7
Thread: question about abstract classes
- 07-07-2009, 02:37 AM #1
question about abstract classes
Hi all. happy to be with u here.
I had an error while trying to use abstract classes, here u are the code i wrote:
Now i will make a subclass Dog :Java Code:public abstract class Animal { private String Name; public Animal(String strname) { Name=strname; } public abstract void Speak(); }
i had the error :Java Code:public class Dog extends Animal { public void Speak() { System.out.print("Hiii, I'm a dog!"); } }
Cannot find symbol
symbol : constructor Animal()
location: class animall.Animal
Waiting for answers......
thanks in advance.
-
Dog's default constructor will try to call Animal's default constructor, but it doesn't exist and hence the error. Solution:
either give Animal a default constructor without a parameter or create a constructor for Dog that call's Animal's current constructor as its super constructor.
- 07-07-2009, 07:01 AM #3
Member
- Join Date
- Jul 2009
- Posts
- 2
- Rep Power
- 0
It might be worth adding that Java will provide a default constructor unless you define one, even if the one you define takes an argument.
- 07-07-2009, 09:39 AM #4
thanks for ur answers.
I have to add a constructor to class Dog. it should be :
but it doesn't work, because constructor Animal is not seen in the class Dog. what should i do?:(Java Code:public Dog(String Nm) { Animal(Nm); // calls the constructor of the super class. }
- 07-07-2009, 09:45 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
- 07-07-2009, 01:04 PM #6
thanks, it works now.:)
- 07-08-2009, 07:19 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
Abstract class question
By McChill in forum New To JavaReplies: 4Last Post: 02-27-2009, 06:52 AM -
interface vs abstract classes
By rosh72851 in forum New To JavaReplies: 7Last Post: 11-16-2008, 08:22 PM -
Abstract Class question
By maa11235 in forum New To JavaReplies: 1Last Post: 01-05-2008, 10:30 PM -
Inverfaces vs Abstract Classes
By ravian in forum New To JavaReplies: 1Last Post: 11-28-2007, 09:53 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks