Should an Animal be abstract or allow for instantiation to represent a “general” instance? [closed]

After a short discussion in the comments here I have come to wonder whether or not a baseclass like Animal in the traditional OOP example should be marked as abstract or not.

Personally I believe it should: there is no such thing as an “animal”, it is merely a term that groups certain subtypes based on what they are. Any instance of an “animal” should be a subtype, a concrete example.

The comment that followed on this was

There most certainly is such a thing as an Animal. Or would you insist that Dog be broken down into Affenpinscher, IrishWolfhound, SiberianHusky, EnglishSetter, and 600 other breeds?

which is a fair remark but that is different from the original situation where the hierarchy was

enter image description here

And there are no subclasses of Dog in the first place. Should there have been then yes, Dog should be abstract because the tree includes more specific leafs for that specific type.

I can only assume that the reason you wouldn’t make it a specific subtype is when you simply don’t know it. I would instead add a “default” implementation of a dog to account for this so I can keep the Dog class itself abstract so there wouldn’t be any ambiguity possible: you are forced to either create a specific dog or indicate that you don’t know the type.

Furthermore it also allows you to create abstract methods that every dog should implement for themselves because they are dogtype-specific. By using a non-abstract class you can’t do this and you’ll have to remember to override each method in the subclasses.

enter image description here

In the above tree I would make Dog, Mammal and Animal abstract and allow the others to be instantiated.

The question boils down to this: should you be able to instantiate a class that exists to indicate a subspecies or should you defer the creation of such a “general” object to a subtype?

7

Think about the scope of your business logic. If your application records sounds made by your pets, you may want to make a difference between dogs and cats; otherwise, when you call myCat.Bark, something may go wrong. On the other hand, an Irish wolfhound barks, and a Siberian husky barks as well, making this distinction irrelevant.

In this case, Dog and Cat would be actual classes, and Animal will be abstract.

On the other hand, if your application handles different breeds of dogs and cats, the difference between a Siberian husky and an English setter becomes relevant.

Now, Affenpinscher and IrishWolfhound will be actual classes, while Animal or Dog would be abstract.

The comment:

There most certainly is such a thing as an Animal. Or would you insist that Dog be broken down into Affenpinscher, IrishWolfhound, SiberianHusky, EnglishSetter, and 600 other breeds?

doesn’t take scope in account. When all I want is to record the sounds of cats and dogs, business-wise, there is no such a thing as an instance of the Animal class: it’s either a dog or a cat. If it’s a dog, his breed doesn’t matter; it may be a property of Dog class, but certainly not a part of the class hierarchy.

1

Unless each subtype adds distinct behavior, the differences will present themselves as attributes of the various instances, e.g., breed, color, weight, etc. I doubt very much you’ll need to model a TwentyEightPoundBlackIrishWolfhound as a subclass.

If you’re building a Twenty Questions game (Is it an animal, vegetable, or mineral?) a concrete Animal class might make sense.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật