Polymorphism
Polymorphism
Polymorphism
1. Can make it more difficult to understand the behavior of an object, especially if the code
is complex.
2. This may lead to performance issues, as polymorphic behavior may require additional
computations at runtime.
Generics in Java:
Generics means parameterized types. The idea is to allow type
(Integer, String, … etc., and user-defined types) to be a
parameter to methods, classes, and interfaces. Using Generics, it
is possible to create classes that work with different data
types. An entity such as class, interface, or method that operates
on a parameterized type is a generic entity.
Why Generics?
The Object is the superclass of all other classes, and Object
reference can refer to any object. These features lack type safety.
Generics add that type of safety feature. We will discuss that
type of safety feature in later examples.
Generics in Java are similar to templates in C++. For example,
classes like HashSet, ArrayList, HashMap, etc., use generics
very well. There are some fundamental differences between the
two approaches to generic types.
Types of Java Generics
Generic Method: Generic Java method takes a parameter and
returns some value after performing a task. It is exactly like a
normal function, however, a generic method has type parameters
that are cited by actual type. This allows the generic method to
be used in a more general way. The compiler takes care of the
type of safety which enables programmers to code easily since
they do not have to perform long, individual type castings.
Generic Classes: A generic class is implemented exactly like a
non-generic class. The only difference is that it contains a type
parameter section. There can be more than one type of
parameter, separated by a comma. The classes, which accept one
or more parameters, are known as parameterized classes or
parameterized types.
// constructor
Test(T obj1, U obj2)
{
this.obj1 = obj1;
this.obj2 = obj2;
}
obj.print();
}
}
Types of Polymorphism:
1. Ad-hoc Polymorphism, also called as Overloading Ad-hoc
Polymorphism allows functions having same name to act differently
for different types. For example: The + operator adds two integers and
concatenates two strings.
import java.util.*;
3. Coercion Polymorphism, also called as Casting Coersion Polymorphism occurs when an object or
primitive is cast into some other type. It could be either Implicit or Explicit. Implicit casting happens as a
responsibility of Compiler itself. For example: float f=100 (integer implicitly gets promoted to float)
Explicit casting makes use of some type-casting expressions such as const_cast, dynamic_cast, etc. For
example: When a class defines conversion operator for some type, say “int”, then, it could be employed
anywhere in the program where integer type data is expected. Illustration Below could make it more
easier to understand:
class IntClass {
private int num;
public IntClass(int a) {
num = a;
}
class Main {
public static void show(int x) {
System.out.println(x);
}