The post should better be in a Code Review Forum or at least merged with it as OP is asking for suggestions about his code. Hope some staff member will look in it.

Coming on your code:
1.) I understand that post is created in a beginners forum still you should consider using some Testing framework for the stuff you have done in main method, Test Driven Development(TDD) is itself a wide topic so will not talk much about here.

2.)Why do you think Animal is instantiable, In my opinion Animal should be a abstract class.

Look at this lineI don't think there is any animal with no name, no breed, no color...

3.) You have extensively used toString() in println. no need to to do so as println implicitly calls the String.valueOf(argument) of the argument passed. if in doublt look at the Documentation in the println API.

4.) Try to be consistent with the indentation, it will make your code look better and readable. their should be a space between () and { in method declarations. overall, as a beginner you have done good job here but removing this nitpicks will make your code more readable.

5.) getMessage() in animal should be abstract as it seems like it should have a different behaviour for different implementations.

6.)I think you should consider using String#format method in Animal#toString as the escape sequence '\n' is not portable and is Environment dependent plus you are just doing some extensive formatting there:

7.) you have not use the @Override annotation uniformly on the overriden methods like toString.

8.) look at this this method:
which is same as
'return name' has a same effect try using this keyword when the field you are trying to access is shadowed like generally in constructors or methods by same parameter name as the instance fields.

these are some of the nitpicks in your code in my opinion, as far as inheritance is concerned Animal heirarchy is the classic example...more on it later i have to attend someone.

Try to enjoy your work while doing it,it will Automatically convert in Hard Work...

Coming to design of the classes:
1.) have a look on one of the usecase of your class:
What do you think the code should does above?
it will not throw a NullPointerException, why - is your job to find. but it will create an animal having name null, age 0, having breed null and eats nothing.
Is such an instance acceptable/possible in real world. No! That is a design error in your class.

You have not made check on the parameters which are part of the public API. but first you need to have the contract for the class. for more details take a look at Design by contract.
one more thing you should take a look at handling situations when more than 2 parameters are needed in constructors. i think it's something advance at a beginner level but if you are comfortable go and google 'Builder pattern'.

2.) Another thing i will point at is setters in Cat and Dog. first thing first, you have not validated the parameters as explained earlier. next in Cat it seems digestable to have the favToy as mutable through the setter as their is a good chance of change in taste for favourite for the same cat later on. but in Dog do you think it will bark in a different style/sound someday as compared to earlier way. if you will not overthink about it, it should be a good idea to make it immutable. how - is your job.

Try to enjoy your work while doing it,it will Automatically convert in Hard Work...

PK has mentioned the biggest problem: I think the Animal class should be made abstract.
Another thing I am suspicious about is creating new methods in subclasses. Nor do I like adding more fields. If you look here, you will see the sort of awkward code you can get. Who want...when you can give the Animal class an abstract makeSound() method?
Also think whether you can get the favourite toy into the Animal class.

Some people like using this. on every occurrence of a field name; I am neutral about that myself. Eclipse tends to add this.automatically, but it is one of those features one can configure to one's own taste.

Kevin O'Sullivan

Ranch Hand

Posts: 43

posted 2 weeks ago

Thanks for the replies and feedback, I had a hard time understanding abstract if I'm honest, when too use it etc, I'll read up on it again.

Campbell Ritchie

Marshal

Posts: 62881

203

posted 2 weeks ago

2

Kevin O'Sullivan wrote:Thanks

That's a pleasure

. . . hard time understanding abstract . . .

You can walk around town and country and never see an animal as such. You see dogs, cats, foxes, worms, flies. etc. Try making Animal an abstract class and see what happens; that might be an easier way to learn than reading about it. And if the code goes horribly wrong, who cares? You probably learn more from code which fails than from code which runs beautifully.

All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad: