Inheritance is an important pillar of OO. It is the mechanism in java by which one class is allow to inherit
the
features(fields and methods) of another class.
Important facts about inheritance in Java
Polymorphism
Polymorphism is considered as one of the important features of Object Oriented Programming. Polymorphism allows
us to perform a single action in different ways. In other words, polymorphism allows you to define one interface
and have multiple implementations. The word “poly” means many and “morphs” means forms, So it means many forms.
In Java polymorphism is mainly divided into two types:
Compile time Polymorphism
Runtime Polymorphism
Compile time polymorphism: It is also known as static polymorphism. This type of polymorphism is achieved by
function overloading or operator overloadin.
are said to be overloaded. Functions can be overloaded by change in number of arguments or/and change in type of arguments.
string class to concatenate two strings. We know that this is the addition operator whose task is to add two operands. So a single
operator ‘+’ when placed between integer operands, adds them and when placed between string operands, concatenates them.
Runtime polymorphism: It is also known as Dynamic Method Dispatch. It is a process in which a function call to the overridden
method is resolved at Runtime. This type of polymorphism is achieved by Method Overriding.
That base function is said to be overridden.
When we think of Object-oriented nature we always think of Objects, class (objects blueprints) and the relationship between them. Objects are related and interact with each other via methods. In other words the
object of one class may use services/methods provided by the object of another class. This kind of relationship is termed as association.
Aggregation and Composition are subsets of association meaning they are specific cases of association.
In both aggregation and composition object of one class "owns" object of another class.
But there is a subtle difference. In Composition the object of class that is owned by the object of it's owning class cannot live on it's own
(Also called "death relationship"). It will always live as a part of it's owning object where as in Aggregation the dependent object is standalone
and can exist even if the object of owning class is dead.
So in composition if owning object is garbage collected the owned object will also be which is not the case in aggregation.
Composition Example : Consider the example of a Car and an engine that is very specific to that car (meaning it cannot be used in any other car).
This type of relationship between Car and SpecificEngine class is called Composition. An object of the Car class cannot exist without an object
of SpecificEngine class and object of SpecificEngine has no significance without Car class. To put in simple words Car class solely "owns" the
SpecificEngine class.
Aggregation Example : Now consider class Car and class Wheel. Car needs a Wheel object to function. Meaning the Car object owns the Wheel object but we cannot say the Wheel object has no significance without the Car Object. It can very well be used in a Bike, Truck or different Cars Object.
Generics in Java is similar to templates in C++. The idea is to allow type (Integer, String, … etc and user defined types) to be a parameter to
methods, classes and interfaces
Advantages of Generics: