Polymorphism is the ability of an object to take on many forms.
The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
Any Java object that can pass more than one IS-A test is considered to be polymorphic.
In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.
It is important to know that the only possible way to access an object is through a reference variable.
A reference variable can be of only one type.
Once declared, the type of a reference variable cannot be changed.
The reference variable can be reassigned to other objects provided that it is not declared final.
The type of the reference variable would determine the methods that it can invoke on the object.
A reference variable can refer to any object of its declared type or any subtype of its declared type.
A reference variable can be declared as a class or interface type.
Compile time polymorphism: It is also known as static polymorphism. This type of polymorphism is achieved by
function overloading or operator overloading.
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.
##Aggregation and Composition
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
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:
rather than making your code fail at run time). Suppose you want to create an ArrayList that store name of students and if by mistake programmer
adds an integer object instead of string, compiler allows it. But, when we retrieve this data from ArrayList, it causes problems at runtime.
then we need not to typecast it every time.
they are type safe too.