Define polymorphism and its types. How compile time polymorphism is different from run time polymorphism? Explain polymorphism with suitable examples.
Q.) Define polymorphism and its types. How compile time polymorphism is different from run time polymorphism? Explain polymorphism with suitable examples.
Subject: Object Oriented Programming and MethodologyPolymorphism
Polymorphism is a programming language feature that allows a variable, function, or object to take on multiple forms depending on the context in which it is used. This allows for greater flexibility and extensibility in code, as it enables the creation of generalized data structures and functions that can be used in a variety of different situations.
There are two main types of polymorphism:
- Compile-time polymorphism refers to the ability of a programming language to resolve the type of a variable, function, or object at compile time. This means that the compiler can determine which implementation of a function or method to call based on the static type of the object that it is being called on.
- Runtime polymorphism refers to the ability of a programming language to resolve the type of a variable, function, or object at runtime. This means that the runtime system can determine which implementation of a function or method to call based on the dynamic type of the object that it is being called on.
Compile-time Polymorphism
Compile-time polymorphism is achieved through the use of generics. Generics allow you to define a class or function that can work with different types of data without having to rewrite the code. The compiler will then generate different versions of the class or function for each type of data that it is used with.
For example, the following Java code defines a generic class called List
that can store any type of data:
public class List {
private T[] items;
public List(T[] items) {
this.items = items;
}
public T get(int index) {
return items[index];
}
public void set(int index, T item) {
items[index] = item;
}
}
You can then use the List
class to store any type of data, such as strings, integers, or even other lists. For example, the following code creates a list of strings:
List names = new List<>(new String[] { "Alice", "Bob", "Carol" });
You can then access the elements of the list using the get()
and set()
methods. For example, the following code prints out the first element of the list:
System.out.println(names.get(0)); // prints "Alice"
Runtime Polymorphism
Runtime polymorphism is achieved through the use of inheritance. Inheritance allows you to create new classes that are based on existing classes. The new classes can then inherit the properties and methods of the existing classes. This allows you to create a hierarchy of classes that can be used to represent different types of objects.
For example, the following Java code defines a base class called Animal
:
public class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
You can then create new classes that inherit from the Animal
class. For example, the following code defines a new class called Dog
that inherits from the Animal
class:
public class Dog extends Animal {
public Dog(String name) {
super(name);
}
public void bark() {
System.out.println("Woof!");
}
}
The Dog
class inherits the name
property and the getName()
method from the Animal
class. It also defines a new method called bark()
.
You can then create objects of the Dog
class and call the bark()
method on them. For example, the following code creates a new Dog
object and then calls the bark()
method on it:
Dog dog = new Dog("Fido");
dog.bark(); // prints "Woof!"
Examples of Polymorphism
Polymorphism is used in a variety of different situations in programming. Some common examples include:
- Object-oriented programming: Polymorphism is essential for object-oriented programming, as it allows objects of different types to be treated in a uniform way.
- Function overloading: Function overloading allows a function to have multiple implementations with different parameter types. This allows you to call the same function with different types of arguments.
- Virtual method invocation: Virtual method invocation allows a method call to be resolved at runtime based on the dynamic type of the object that it is being called on. This allows you to write code that can work with different types of objects without having to rewrite the code.
Polymorphism is a powerful programming language feature that can be used to improve the flexibility, extensibility, and maintainability of your code.