Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Inheritance example  RSS feed

 
Ranch Hand
Posts: 43
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Learning about inheritance, is the below example a good representation of it










Any feedback appreciated
 
Ranch Hand
Posts: 483
23
AngularJS Eclipse IDE Firefox Browser Java Spring VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
praveen kumaar
Ranch Hand
Posts: 483
23
AngularJS Eclipse IDE Firefox Browser Java Spring VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 62881
203
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 62881
203
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

praveen kumaar wrote:. . . 'return name' has a same effect try using this keyword . . . .

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!