-1
\$\begingroup\$

I am supposed to write a small java program. The program must contain 3 different kind of dogs. The dogs can bark and move, these methods can be represented just by System.out.println();. Some dogs can have common way for moving or barking. Here is what I wrote so far but I am not really sure that its satisfies the requierements, please give me any suggestions how can I improve it(OOP concepts or just how can have better OOD):

public class Dog {

    public void move(){

    }
    public void bark(){

    }
}
public class TypeOne extends Dog{

    @Override
    public void move(){
        System.out.println("Moving");
    }
    @Override
    public void bark(){
        System.out.println("Wao wao wao");
    }
}
public class TypeTwo extends Dog{
    @Override
    public void move(){
        System.out.println("Moving slowly");
    }
    @Override
    public void bark(){
        System.out.println("Wao wao wao");
    }
}

public class TypeThree extends Dog{
    @Override
    public void move(){
        System.out.println("Moving fast");
    }
    @Override
    public void bark(){
        System.out.println("Haw haw haw");
    }
}
\$\endgroup\$
  • \$\begingroup\$ change Dog class to interface \$\endgroup\$ – Zulfi Jul 6 '16 at 12:51
2
\$\begingroup\$

You can make Dog an interface since it doesn't have to contain any behaviour.

public interface Dog {
    void move();
    void bark();
}

I also recommend to mark the classes TypeOne, TypeTwo and TypeThree with the final modifier so that the behaviour you put in them can never be altered by someone trying to inherit these classes.

public final class TypeOne implements Dog {
    //Implementation
}
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

General

The example you gave is totally fine. I won't suggest you to make "Dog" and interface or an abstract class etc. The effects on the example of one or the other are negligible.

But what you really should know is that this example is not more than an excercise to understand the language mechanics of classes, abstraction and inheritance (here in JAVA). To ask for advice to get a better OO design in this case is like asking to learn gear shifting on an expensive car after you learned it on a cheap car. It remains gear shifting and doesn't make sense.

Is your implementation example for inheritance/abstraction fine?

Definitely.

Would this be a design in real life?

I don't think so.

In real life you would have one class "Dog" with some properties when you think of a software for veterinarians. Properties may be the owner, the dog breed, birthday etc.

An other application can be the usage in biological systematics. Although there you would not use inheritance to model all types of dogs. You would go with composition and relational objects.

What is my suggestion?

There is no real world application I can think of that would model this case as presented. If you have one I like to see it.

As long as you haven't: Keep this example for what it is meant for: Understanding mechanics of the JAVA language.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.