简体   繁体   中英

Java inheritance hierarchy animal classes

I'm supposed to have a hierarchy of animals in my java project, but I'm confused on what should extend what. Here are the instructions:

Write classes or interfaces to represent the following:

  • Adoptable
  • Animal
  • Bat
  • Bird
  • BlueWhale
  • Dog
  • Emu
  • Fish
  • Goldfish
  • Mammal
  • Otter
  • Parakeet
  • Reptile
  • Turtle
  • WaterDweller
  • Whale
  • Winged

You need to decide on the structure of the classes/interfaces. Consider:

Which should be an abstract class? a concrete class? Which should be an interface? How should the classes be related through inheritance? In what classes should methods be placed? What methods should be overridden? What information should be taken in as a parameter and what information can be hard-coded into a class? Some additional details/requirements:

All animals have a method "isWarmBlooded" that returns a boolean. The method can be in the class directly or inherited. All animals have a name. All classes have a toString method that returns the animal's name, whether the animal is warm blooded, and a list of all animal names that apply to the animal. The toString method can be in the class directly or inherited. Animals that can be adopted as pets have a method "getHomeCareInstructions" that returns a description of how to care for the animal. Animals that live in water (are water dwellers) have a method "livesOnLand" that returns a boolean of whether the animal also can live on land. Animals that have wings have a method "flies" that returns a boolean of whether the animal can fly. This part of the assignment isn't necessarily difficult from a programming perspective. What you should spend time on is carefully considering the design of you classes and how they should be related through inheritance or interfaces

I'm not sure how to design this because I know a bird is winged, but so is a bat. Therefor bat would extend winged, but bats are also mammals. I can't have winged extend mammal because birds are not mammals. Also, whales and otters are watter dwellers, but are also waterdwellers. I can't have waterdwellers extend mammals (because whales/otters are mammals), but fish are not mammals and are waterdwellers. How would I make it so a bat is both winged and a mammal? The programming is the easy part, just struggling with the structure of the project. How can I structure this so it could work?

So in your modelling you have to think:

Which are actual animals?

eg whales, otters -> classes

which are a type of animal ?

eg a bird. -> abstract class

Since every emu is a bird.
This is called the Liskov Substitution Principle https://en.wikipedia.org/wiki/Liskov_substitution_principle

Which are characteristics of an animal?

eg WaterDweller, Winged -> those are interfaces.

Every class can have one superclass, it can have many characteristics, like being winged, or being a waterdweller, or adoptable

one addition for your bat example:

It is a mammal -> therefore it's superclass is mammal. It is winged - which is a characteristic - so it implements the winged interface.

class Bat extends Mammal implements Winged{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM