Aly
Aly

Reputation: 3

ERROR: class not abstract and does not override abstract method

I'm having difficulties on debugging this code. I've tried a lot of alternatives to get rid of the error but I can't seem to point out what's wrong since I'm new to Java.

public abstract class Animal {

    private String name;    
    private String type;

    public Animal(String name, String type) {       
       this.name = name;
       this.type = type;    
    }       

   public String getName() {        
        return this.name;
   }

   public String getType() {        
       return this.type;    
   }        

   public abstract void speak();

 }



public class Dog extends Animal{


    public String getName() {
        return super.getName();     }

    public String getType() {       
       return super.getType();  }

    public void speak(String name, String type){
        System.out.println("arf!");     }

    }


public class Ark{
    static void main(String[] args){

        Dog cookie = new Dog();         
        cookie.speak();

    }
}

Thank you!

Upvotes: 0

Views: 1459

Answers (4)

kirti
kirti

Reputation: 4609

public abstract class Animal {

    private String name;    
   private String type;

    public Animal(String name, String type) {       
       this.name = name;
        this.type = type;   }       

   public String getName() {        
        return
        this.name;  }

    public String getType() {       
         return this.type;  }       

   public abstract void speak(String name, String type);    
 // if you want parameters in the Dog class for this method you must include them here otherwise it wont work
}



 public class Dog extends Animal{


    public String getName() {
        return super.getName();     }

    public String getType() {       
       return super.getType();  }

    public void speak(String name, String type){
        System.out.println("arf!");     }

 }

Upvotes: 0

Jake Huang
Jake Huang

Reputation: 140

You need to override speak method in Dog class, change it to

public void speak()
{ 
   System.out.println("arf!"); 
}

Upvotes: 0

Mitesh Pathak
Mitesh Pathak

Reputation: 1211

public abstract void speak();

You need to implement this in Dog

What you have implemented

public void speak(String name, String type)
{ System.out.println("arf!"); }

Hence Dog is abstract but you haven't specified that, niether you implemented speak() without parameters. Remove the parameters, anyways you are not using them.

@Override
public void speak()
{ System.out.println("arf!"); }

Upvotes: 2

DejaVuSansMono
DejaVuSansMono

Reputation: 797

The Abstract Speak Method doesn't have the same arguments that your implemented Dog.speak(String String) has.

Upvotes: 0

Related Questions