2

So is the difference between inheritance and composition is that of a fridge and iceman?

class Man {
fun greet(){return "hi"}
}

class IceMan extends Man{
override fun greet(){return "hi i am iceman"}
fun getIce(){return "ice"*Random.nextInt()}
}

class Fridge{
private Man manAi;
private String s = "ice";
fun greet(){return manAi.greet()}
fun getIce(){return s*Random.nextInt()}
}

}

=========
Basically , in first case, a class is getting features by inheriting from parent while in another case, a class is getting features by internally having objects of other classes that could provide those features?

Comments
  • 1
    Every fridge has a man inside?
    I guess you could change what iceman the fridge has, that's a difference.
  • 2
    you got it. It's exactly what it sounds like. Inheritance inherits properties of parent objects, while composition composes several objects into one essentially.
  • 0
    @c3r38r170 i equated man with voice feature, so yeah ^_^!
  • 1
    Ice man approves
  • 2
    Not possible to add an image via edit 😣
  • 0
    the difference between inheritance and composition ia that inheritance is "is a (subtype of)" relationship, while composition is "has properties, behaviors and functionality of" relationship.
Add Comment