composition meansHAS A
Inheritance meansIS A
Example: Car has a Engine and car is a Automobile
In programming the is represented as:
1 classEngine {}//The engine class.2 3 classautomobile{}//Automobile class which is the parent to Car class.4 5 //car is a Automobile, so Car class extends Automobile class.6 classCarextendsautomobile{7 8 //car has a engine so, car class have an instance of the engine class as its member.9 Privateengine engine;Ten}
encapsulation in Java are a mechanism of wrapping the data (variables) and code acting on the data (methods) Toget Her as as a single unit. In encapsulation the variables of a class would be hidden from other classes, and can is accessed only through the methods of their current class, therefore it is also known as data hiding.
To achieve encapsulation in Java
polymorphism is the ability of a object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference be used to refer to a child class object.
Java inheritance, composition, encapsulation and polymophism