Sprawdzenie poprawności dziedziczenia i polimorfizmu

0

Witam. Po raz pierwszy mam styczność z językiem obiektowym, w ogóle słabo u mnie z programowaniem gdyż miałem tylko styczność (trochę) z C, który nie jest obiektowy.

// ANIMAL class:

public abstract class Animal implements Comparable<Animal>
{
    private String name;
    private int age;
    private long uniqueId;
    private boolean have_fun;
    
    public abstract void Sound();
    public abstract void Play();
    public abstract int compareTo(Animal other);
    public static long numberOfAnimals = 0;
    
    public Animal(String aName, int aAge)
    {
        this.name = aName;
        this.age = aAge;
        this.uniqueId = ++numberOfAnimals;
        this.have_fun = false;
    }
    public String getName() {return this.name;}
    public int getAge() {return this.age;}
    public long getUniqueId() {return this.uniqueId;}
    public boolean getStatus() {return this.have_fun;}
    public void setStatus(boolean status) {this.have_fun = status;}
}

// CAT class

public class Cat extends Animal implements Comparable<Animal>
{
    public Cat(String aName, int aAge)
    {
        super(aName, aAge);
    }
    public void Sound()
    {
        System.out.println("Niszczyc, niszczyc!");
    }
    public void Play()
    {
        if(getStatus() == false) setStatus(true);
        System.out.println("Playing with " + getClass().getName() + " " + this.getName());
    }
    public int compareTo(Animal other)
    {
        return Long.compare(getUniqueId(), other.getUniqueId());
    }
}
// DOG class

public class Dog extends Animal implements Comparable<Animal>
{
    public Dog(String aName, int aAge)
    {
        super(aName, aAge);
    }
    public void Sound()
    {
        System.out.println("Waff Waff");
    }
    public void Play()
    {
        if(getStatus() == false) setStatus(true);
        System.out.println("Playing with " + getClass().getName() + " " + this.getName());
    }
    public int compareTo(Animal other)
    {
        return Long.compare(getUniqueId(), other.getUniqueId());
    }
}
// MAIN CLASS

class JavaStart
{
    public static void main(String[] args) 
    {
        Animal burek = new Dog("Burek", 12);
        Animal leon = new Cat("Leon", 7);
        
        Dog x = new Dog("Doggie", 14);
        
        Animal animals[] = new Animal[3];
        animals[0] = burek;
        animals[1] = leon;
        animals[2] = x;
        
        for(Animal a : animals)
        {
            a.Play();
            a.Sound();
        }
        
        if(burek.compareTo(leon) < 0)
            System.out.println("Burek < x");
        else if(burek.compareTo(leon) == 0)
            System.out.println("Burek == x");
        else
            System.out.println("Burek > x");
    }
}

http://4programmers.net/Pastebin/3343

Zawiera:
Abstrakcyjną klasę Animal, po której dziedziczą klasy Dog i Cat.
Abstrakcyjne funkcje Sound i Play oraz compareTo (polimorfizm ?)

Głównie czarna magia dla mnie to interfejsy oraz dziedziczenie klas. Czy dobrze jest zrealizowane w tym przykładzie (czy to dziedziczenie ma sens itd) ? Wiem, że to podstawy podstaw, jednak chciałem by ktoś to sprawdził pod względem poprawności.

dodanie kodu do posta - furious programming

0

Zastosowanie dziedziczenie ma sens. Ale dlaczego metoda compareTo w klasie Animal jest abstrakcyjna? Implementacje tej metody w klasach Cat i Dog są przecież identyczne.
Btw, zgodnie z konwencją nazwy metod powinny się zaczynać małą literą (sound, play).

1 użytkowników online, w tym zalogowanych: 0, gości: 1