1

I'm learning OOP in Java and I'm trying to decide if I can/should use inheritance in this example. Do not pay attention to access modifiers and things like that.

There are two kind of pets:

  • dogs (name, breed, age, owner)
  • cats (name, breed, color, owner)

Dog.toString() should return "Name: name, Breed: breed, Age: age, Owner: owner"

Cat.toString() should return "Name: name, Breed: breed, Color: color, Owner: owner"

My question is: should I implement toString() in an abstract class and override in derived classes? How? I mean, there is only one field. I can't see the gain.

So, I did:

abstract class Pet {

    String name;
    String breed;
    String owner;

    // getters setters

    public String toString() { 

         return ????
    } 

}

class Dog extends Pet {

    int age; 

    public String toString() {

       return String.format("Name: %s, Breed:  %s, Age: %d, Owner: %s", name, breed, age, owner)

    }

}

class Cat extends Pet {

    String color; 

    public String toString() {

        return String.format("Name: %s, Breed:  %s, Color: %s, Owner: %s", name, breed, color, owner)

    }

}


2
  • Why does a Dog have an age and not a Cat? Or a Turtle? Or a Rock? Age should be a property of Pet rather than just Dog. Commented Dec 29, 2020 at 21:23
  • 1
    @NomadMaker it doesn't matter. just an example Commented Dec 30, 2020 at 21:41

2 Answers 2

1

This is a situation where you could consider applying the Template Method design pattern. In this pattern, you define the reusable part of your method in the abstract class, and implement only the variable parts in the subclasses. For example:

abstract class Pet {

// ...

    protected abstract String extra(); // Or, return the empty String

    public final String toString() { 
        return String.format("Name: %s, Breed:  %s, %s, Owner: %s", 
            name, breed, extra(), owner);
    } 
}

In your subclasses:

class Dog extends Pet {

    int age; 

    public String extra() {
        return String.format("Age: %d");
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

A slight variation would be to make extra() not abstract and let it return an empty String. This avoids overriding extra() if it needs not to be overridden at the expense that overriding is not enforced, i.e. the class's author could forget to override extra().
1

I wouldn't write the toString() in the abstract class. It adds very little in terms of reusing code and makes the statement quite a bit harder to read. Having super.toString() in the middle of the statement would be confusing.

1 Comment

yes, that was my initial thought. but i was looking something like the accepted answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.