Polymorphism:
Java Polymorphism is the ability of an object to take on many forms. It allows you to write flexible and extensible code that can work with objects of different classes in a uniform way. Polymorphism is one of the four fundamental concepts of object-oriented programming (OOP) along with inheritance, encapsulation, and abstraction.
There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism.
For example:
public class MathUtils {
public int sum(int a, int b) {
return a + b;
}
public double sum(double a, double b) {
return a + b;
}
}
In this example, the MathUtils class has two methods with the same name sum, but with different parameter types. The first method takes two integers as arguments, while the second method takes two doubles. The correct method to call is determined at compile time based on the types of the arguments passed to the method.
For example:
public class Animal {
public void makeSound() {
System.out.println("Animal is making a sound");
}
}
public class Dog extends Animal {
public void makeSound() {
System.out.println("Dog is barking");
}
}
In this example, the Animal class has a method makeSound, which is overridden in the Dog class with a specific implementation for a dog’s sound. When the makeSound method is called on a Dog object, the implementation in the Dog class is called instead of the implementation in the Animal class.
In summary, polymorphism is a powerful feature of Java that allows you to write flexible and extensible code. It helps to create code that is easier to maintain and modify by allowing objects of different classes to be treated uniformly.
Method Overloading in Java:
Method Overloading is a feature in Java that allows a class to have two or more methods with the same name, but with different parameters. In other words, when two or more methods in a class have the same name but different parameters, it is called method overloading.
Method overloading is useful when we want to perform similar operations on different types of data. It helps us to reuse the same method name and provide different functionalities based on the input parameters.
The rules for method overloading are as follows:
Let’s take an example of method overloading:
public class Calculator {
public int add(int x, int y) {
return x + y;
}
public double add(double x, double y) {
return x + y;
}
public String add(String x, String y) {
return x + y;
}
}
In this example, the Calculator class has three add methods with the same name but different parameter types. The first method takes two integers as parameters, the second method takes two doubles, and the third method takes two strings. Depending on the input parameters, the corresponding add method will be called.
Calculator calculator = new Calculator();
int result1 = calculator.add(1, 2);
double result2 = calculator.add(1.0, 2.0);
String result3 = calculator.add("Hello", "World");
In this example, we are calling the add method with different parameter types. The first call will invoke the add method that takes two integers, the second call will invoke the add method that takes two doubles, and the third call will invoke the add method that takes two strings.
In summary, method overloading is a powerful feature of Java that allows us to reuse the same method name and provide different functionalities based on the input parameters. It makes the code more readable, maintainable, and flexible.
Method Overriding in Java:
Method overriding is a feature in Java that allows a subclass to provide its own implementation of a method that is already defined in its superclass. In other words, when a subclass provides its own implementation of a method that is already defined in its superclass, it is called method overriding.
The method in the superclass must be declared with the protected or public access modifier to be overridden in a subclass. Also, the method in the subclass must have the same method signature (name, return type, and parameter list) as the method in the superclass.
Let’s take an example of method overriding:
public class Animal {
public void makeSound() {
System.out.println("Animal is making a sound");
}
}
public class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Dog is barking");
}
}
In this example, the Animal class has a method named makeSound that prints “Animal is making a sound”. The Dog class extends the Animal class and overrides the makeSound method to print “Dog is barking”.
Animal animal = new Animal();
animal.makeSound(); // prints "Animal is making a sound"
Dog dog = new Dog();
dog.makeSound(); // prints "Dog is barking"
In this example, we are creating an instance of the Animal class and calling its makeSound method. The output is “Animal is making a sound”. We are also creating an instance of the Dog class and calling its makeSound method. The output is “Dog is barking”.
In summary, method overriding is a powerful feature of Java that allows a subclass to provide its own implementation of a method that is already defined in its superclass. It helps us to achieve polymorphism and make our code more flexible and reusable.
Learners TV is a website that is designed to educate users and provide instructional material on particular subjects and topics.