-2

Given three classes Apple, Fruit and Seed ;

Apple is a kind of Fruit and it has Seed in it. With the knowledge of Apple Class uses directly or indirectly the properties of Fruit and Seed. Can we say Apple is inherited from Fruit and Seed ?

10
  • 9
    Animal, vegetable and mineral examples are seldom illuminating, because inheritance usually deals with more abstract concepts. You build a car using composition, not inheritance. May 30, 2018 at 18:10
  • 1
    Is an Apple a fruit? yes. Is an apple a seed? no. The seed is an apple factory, since it grows a tree to produce more apples. An apple contains seeds and the cycle continues. So back to what @RobertHarvey said. May 30, 2018 at 18:27
  • 2
    A common mistake is to attempt to model based on real life with no consideration of what you're actually modelling. You can't blindly say anything about whether it should be inheritance or composition without having an understanding of the actual model and how objects should interact with one another. May 30, 2018 at 18:42
  • 3
    Is your actual question about how inheritance works, or the difference between 'is a' and 'has a' relations? Inheritance is an 'is a' relation, 'has a' relations are modeled by composition/containment.
    – esoterik
    May 30, 2018 at 18:44
  • 2
    @DirkBoer: askers on this site are expected to do some research on their own before asking, and tell us what they tried and why it did not suit their needs. This question, however, does not show any research effort (and that is exactly what tool tip on the downvote button starts with). And if you read the accepted answer, it basically says that pathological examples don't really help to understand software engineering.
    – Doc Brown
    May 31, 2018 at 7:02

2 Answers 2

8

That would be one way to represent the relationship, but this sounds like almost a classical case for composition over inheritance. Going from the description alone, Apple could be a Fruit that contains a Seed.

However, there's only so much you can say from class names alone. Program architecture being based off the real-world relation of the concepts involved is often used an example, but in practice the code should be designed based on how it will be used. Something should be derived when it's desirable to be able to use instances of it interchangeably with the parent, not just because the concepts are taxonomically related IRL.

1
  • 1
    Technically, the seeds are what biologically separates fruits from veggies. I know hair splitting. May 30, 2018 at 18:29
1

Can we say Apple is inherited from Fruit and Seed?

I would suggest to consider better example/s if you trying to understand inheritance or composition concepts. If you already understand them then also understand that Apple can not really be a class in first place. Yes it "is-a" fruit but technically you would just create an instance of fruit and name it as apple. So the class you need is Fruit and not Apple, unless you are convinced that Apple can really have a specialized form which cannot be abstracted into Fruit.

class Fruit 
{
   string Name;
   string Color;
   bool Seedless;
   ...
}

And various fruits:

var apple = new Fruit { Name = "Apple", Seedless = false }
var grape = new Fruit { Name = "Grape", Seedless = true }

This is just like you defining a Person class and work with wuzzu and nikhil instances of it if you wanted to refer both of us.

If you are not convinced with having just a Fruit you can further consider defining specialized forms of Fruit based on fruit categories. Say,

class Berry : Fruit // ex: grape, banana
class Pome : Fruit // ex: apple, pear
// and so on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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