Home/oops
- Recent Questions
- Most Answered
- Answers
- No Answers
- Most Visited
- Most Voted
- Random
- Bump Question
- New Questions
- Sticky Questions
- Polls
- Followed Questions
- Favorite Questions
- Recent Questions With Time
- Most Answered With Time
- Answers With Time
- No Answers With Time
- Most Visited With Time
- Most Voted With Time
- Random With Time
- Bump Question With Time
- New Questions With Time
- Sticky Questions With Time
- Polls With Time
- Followed Questions With Time
- Favorite Questions With Time
Define polymorphism in Object-Oriented Programming using examples.
Polymorphism is a fundamental concept in Object-Oriented Programming (OOP) that allows objects to be treated as instances of their parent class rather than their actual class. This enables a single function or method to operate on objects of different classes, which can result in more flexible and mRead more
Polymorphism is a fundamental concept in Object-Oriented Programming (OOP) that allows objects to be treated as instances of their parent class rather than their actual class. This enables a single function or method to operate on objects of different classes, which can result in more flexible and maintainable code. There are two main types of polymorphism in OOP: compile-time (or static) polymorphism and runtime (or dynamic) polymorphism.
Compile-time Polymorphism (Method Overloading)
Compile-time polymorphism is achieved through method overloading, where multiple methods have the same name but differ in the type or number of their parameters. The correct method to call is determined at compile time.
Example in Java:
class Calculator {
// Method to add two integers
public int add(int a, int b) {
return a + b;
}
// Method to add three integers
public int add(int a, int b, int c) {
return a + b + c;
}
// Method to add two double values
public double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(2, 3)); // Output: 5
System.out.println(calc.add(2, 3, 4)); // Output: 9
System.out.println(calc.add(2.5, 3.5)); // Output: 6.0
}
}
Runtime Polymorphism (Method Overriding)
Runtime polymorphism is achieved through method overriding, where a subclass provides a specific implementation of a method that is already defined in its superclass. The method to be called is determined at runtime.
Example in Java:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal(); // Animal reference and object
Animal myDog = new Dog(); // Animal reference but Dog object
Animal myCat = new Cat(); // Animal reference but Cat object
myAnimal.sound(); // Output: Animal makes a sound
myDog.sound(); // Output: Dog barks
myCat.sound(); // Output: Cat meows
}
}
What is function overloading and constructor overloading in Java?
Function overloading (or method overloading) in Java allows a class to have more than one method with the same name, provided their parameter lists are different. It helps to increase the readability of the program. Key Points: Method Name: Must be the same. Parameter List: Must differ (either in thRead more
Function overloading (or method overloading) in Java allows a class to have more than one method with the same name, provided their parameter lists are different. It helps to increase the readability of the program.
Key Points:
Constructor overloading in Java allows a class to have more than one constructor with different parameter lists. This enables the creation of objects in different ways.
Key Points: