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 ?
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
Sign up to join this communityGiven 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 ?
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.
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.