Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Default 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:
    Java Code:
    public  abstract class Animal
    {
        private String Name;
        public Animal(String strname)
        {
            Name=strname;
        }
        public  abstract void Speak();
    
    }
    Now i will make a subclass Dog :
    Java Code:
    public class Dog extends Animal
    {
       
       public  void Speak()
        {
            System.out.print("Hiii, I'm a dog!");
        }
    
    }
    i had the error :
    Cannot find symbol
    symbol : constructor Animal()
    location: class animall.Animal


    Waiting for answers......
    thanks in advance.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    derrikc is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Default

    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.

  4. #4
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Default

    thanks for ur answers.
    I have to add a constructor to class Dog. it should be :
    Java Code:
      public Dog(String Nm)
       {
            Animal(Nm); // calls the constructor of the super class.
       }
    but it doesn't work, because constructor Animal is not seen in the class Dog. what should i do?:(

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

  6. #6
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Default

    thanks, it works now.:)

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

Similar Threads

  1. Abstract class question
    By McChill in forum New To Java
    Replies: 4
    Last Post: 02-27-2009, 07:52 AM
  2. interface vs abstract classes
    By rosh72851 in forum New To Java
    Replies: 7
    Last Post: 11-16-2008, 09:22 PM
  3. Abstract Class question
    By maa11235 in forum New To Java
    Replies: 1
    Last Post: 01-05-2008, 11:30 PM
  4. Inverfaces vs Abstract Classes
    By ravian in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 10:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •