Skip to main content
edited title
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Am I doing superclasses right? Animal inheritance hierarchy

Source Link
h3rrmiller
  • 165
  • 2
  • 7

Am I doing superclasses right?

I'm trying to figure out if I'm doing superclasses right. Also, I welcome critique on Java structure/syntax etc.

Animal.java:

public class Animal{
    private static int counter = 0; // how many animals we have created (to demonstrate a static variable)
    private Boolean isWild = true; // is the animal wild?
    private Boolean hasSharpPointyTeeth = false; // does it have sharp, pointy teeth?!
    private int age = 0; // how old

    Animal(){ // default constructor
        this.isWild = isWild;
        this.hasSharpPointyTeeth = hasSharpPointyTeeth;
        this.age = age;
        
        counter++;
    }

    Animal(Boolean isWild, Boolean hasSharpPointyTeeth, int age){
        this.isWild = isWild;
        this.hasSharpPointyTeeth = hasSharpPointyTeeth;
        this.age = age;

        counter++;
    }

    //begin getters/setters
    public void setIsWild(Boolean isWild){
        this.isWild = isWild;
    }
    public void setAge(int age){
        this.age = age;
    }
    public void setHasSharpPointyTeeth(Boolean hasSharpPointyTeeth){
        this.hasSharpPointyTeeth = hasSharpPointyTeeth;
    }
    public Boolean getIsWild(){
        return isWild;
    }
    public Boolean getHasSharpPointyTeeth(){
        return hasSharpPointyTeeth;
    }
    public int getAge(){
        return age;
    }
    public static int getCounter(){
        return counter;
    }
    //end getters/setters
    
    public String speak(){
        return "Grrrr!";
    }
    
    public String toString(){
        String out =    speak() + "\n" + 
                        "Age: " + getAge() + "\n" +
                        "Wild? " + getIsWild() + "\n" + 
                        "Has Sharp Pointy Teeth? " + getHasSharpPointyTeeth() + "\n";
        return out;
    }
}

Dog.java

public class Dog extends Animal{
    private String name = "None";
    private String owner = "None";
    private String breed = "Unknown";
    private String sex = "Unknown";

    Dog(){
        super(false, false, 0); // call the constructor from the Animal class
        this.name = name;
        this.owner = owner;
        this.breed = breed;
        this.sex = sex;
    }

    Dog(Boolean isWild, Boolean hasSharpPointyTeeth, int age, String name, String owner, String breed, String sex){
        super(isWild, hasSharpPointyTeeth, age); // call the constructor from the Animal class
        this.name = name;
        this.owner = owner;
        this.breed = breed;
        this.sex = sex;
    }

    //begin getters/setters
    public void setName(String name){
        this.name = name;
    }
    public void setOwner(String owner){
        this.owner = owner;
    }
    public void setBreed(String breed){
        this.breed = breed;
    }
    public void setSex(String sex){
        this.sex = sex;
    }
    public String getName(){
        return name;
    }
    public String getOwner(){
        return owner;
    }
    public String getBreed(){
        return breed;
    }
    public String getSex(){
        return sex;
    }
    //end getters/setters

    @Override
    public String speak(){
        return "Woof!";
    }
    
    @Override
    public String toString(){
        String out =    super.toString() + // call the toString method of Animal, not the overridden one in Dog
                        "Name: " + getName() + "\n" +
                        "Breed: " + getBreed() + "\n" +
                        "Sex: " + getSex()+ "\n" +
                        "Owner: " + getOwner() + "\n";
        return out;
    }
}

Test.java

public class Test{
    public static void main(String[] args){
        Dog fluffy = new Dog(false,true,3,"Fluffy","Ted","Westie","Male");
        Animal rabbit = new Animal(true,true,8);

        System.out.println("We made " + Animal.getCounter() + " animals.  Here's their info:");
        System.out.println();
        System.out.println(fluffy);
        System.out.println(rabbit);
    }
}