Java error =不是抽象的,不会覆盖抽象方法

时间:2014-03-15 23:13:16

标签: java arrays interface abstract

  

我有和错误Ikan不是抽象的,不会覆盖抽象   方法setName类Ikan扩展Animal实现Pet和Kucing是   不抽象,也不覆盖抽象方法setName类   Kucing扩展了Animal implements Pet

abstract class Animal {
    protected int Legs;
    protected String Eater;
    Animal (int l,String e) {
        Legs = l;
        Eater = e;
    }
    abstract public void walk ();
    abstract public void eat ();
    int getLegs () {
        return Legs;
    }
    String getEater () {
        return Eater;
    }
}

interface Pet {
    void setName();
    String getName();
    void balikNama();
    void play();
}

class Ikan extends Animal implements Pet {
    String name,mainan;
    Ikan (int l,String e) {
        super(l,e);
    }
    void setMainan (String m) {
        mainan = m;
    }
    void getMainan () {
        System.out.println ("Ikan ini bermain"+mainan);
    }
    public void setName (String n) {
        name = n;
    }
    public String getName () {
        return name;
    }
    public void balikNama () {
        for (String part : name.split(" ")) {
            System.out.print(new StringBuilder(part).reverse().toString());
            System.out.print(" ");
        }
    }
    public void play () {
        System.out.println (name+" lagi mainan "+mainan);
    }
    public void walk () {
        System.out.println ("Tidak bisa berjalan bisanya renang");
    }
    public void eat () {
        System.out.println ("Hewan ini memakan "+Eater);
    }
}

class Kucing extends Animal implements Pet {
    String name,mainan;
    Kucing (int l,String e) {
        super(l,e);
    }
    void setMainan (String m) {
        mainan = m;
    }
    void getMainan () {
        System.out.println ("Kucing ini bermain"+mainan);
    }
    public void setName (String n) {
        name = n;
    }
    public String getName () {
        return name;
    }
    public void balikNama () {
        for (String part : name.split(" ")) {
            System.out.print(new StringBuilder(part).reverse().toString());
            System.out.print(" ");
        }
    }
    public void play () {
        System.out.println (name+" lagi mainan "+mainan);
    }
    public void walk () {
        System.out.println ("Berjalan dengan "+Legs+" kaki");
    }
    public void eat () {
        System.out.println ("Hewan ini memakan "+Eater);
    }
}

class Laba extends Animal {
    String name;
    Laba (int l,String e) {
        super(l,e);
    }
    void setName (String n) {
        name = n;
    }
    public String getName () {
        return name;
    }
    public void balikNama () {
        for (String part : name.split(" ")) {
            System.out.println(new StringBuilder(part).reverse().toString());
        }
    }
    public void walk () {
        System.out.println ("Berjalan dengan "+Legs+" kaki");
    }
    public void racun () {
        System.out.println ("Cuma sedikit racunya");
    }
    public void eat () {
        System.out.println ("Hewan ini memakan "+Eater);
    }
}

class Tarantula extends Laba{
    String name;
    Tarantula (int l,String e,String n) {
        super(l,e);
        name = n;
    }
    void setName (String n) {
        name = n;
    }
    public String getName () {
        return name;
    }
    public void balikNama () {
        for (String part : name.split(" ")) {
            System.out.println(new StringBuilder(part).reverse().toString());
        }
    }
    public void walk () {
        System.out.println ("Berjalan dengan "+Legs+" kaki");
    }
    public void racun () {
        System.out.println ("Banyak racunya");
    }

}

class TestAnimal {
    public static void main (String[]args) {
        Ikan I = new Ikan (0,"Manusia");
        Kucing K = new Kucing (4,"Ikan");
        Laba L = new Laba (6,"Kucing");
        Tarantula T = new Tarantula (6,"Burung","Joni");
        I.setName ("Piranha");
        K.setName ("Tiger");
        L.setName ("Spiderman");
        I.setMainan ("Ikan lainnya");
        K.setMainan ("Burung");
        Animal [] A = new Animal [4];
        A[0] = I;
        A[1] = K;
        A[2] = L;
        A[3] = T;
        for (int i = 0;i < 4;i++) {
            if (A[i] instanceof Laba) {
                System.out.println ("Ini bukan hewan peliharaan bro");
                System.out.print ("Laba laba ini bernama "+A[i].getName());
                A[i].balikNama();
                A[i].walk();
                A[i].racun();
                A[i].eat();
            } else {
                A[i].getName();
                A[i].balikNama();
                A[i].walk();
                A[i].eat();
                A[i].getMainan();
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

Pet接口声明setName不应该使用任何参数。这是不正确的;该方法旨在使用一个String代表新名称。

将其更改为

void setName(String name);

并且Java将看到它已经实现。

答案 1 :(得分:0)

Ikan和Kucing类不实现继承的抽象方法Pet.setName()

将界面更改为Pet.setName(String name)