I know this has been asked, but is generally explained in context with something else and feels a little complicated.
Inheritance is when classes inherit properties from a super class, correct? What exactly is composition?
I know this has been asked, but is generally explained in context with something else and feels a little complicated.
Inheritance is when classes inherit properties from a super class, correct? What exactly is composition?
Class Bird;
Class Duck;
Class Wing;
Duck "is a" bird <- inheritance.
Duck "has a" wing <- composition.
Hopefully it has two wings.
Inheritance is as you said when classes inherited properties and methods from parent class. Compasition is when a class has an instance of another class.
public class Person{
private Car car;
}
Person
class is related to Car
class by compositon because it holds an instance of Car
class