Polymorphism and Inheritance
Polymorphism and Inheritance
• This occurs when multiple methods have the same name but differ in
the number or type of their parameters. The correct method is chosen at
compile time based on the method signature.
class Calculator {
// Method to add two integers
public int add(int a, int b) {
return a + b; }
// Overloaded method to add three integers
public int add(int a, int b, int c) {
return a + b + c; }
// Overloaded method to add two doubles
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("Sum of 10 and 20: " + calc.add(10, 20)); // Calls the first method
System.out.println("Sum of 10, 20, and 30: " + calc.add(10, 20, 30)); // Calls the second method
System.out.println("Sum of 10.5 and 20.5: " + calc.add(10.5, 20.5)); // Calls the third method
}
Code explanantion
• Class Calculator
• This class has three overloaded methods named add. Method overloading allows the same method name to be used, but
with different parameters (number or type). This is a feature of compile-time polymorphism.
• This method takes two integers as parameters and returns their sum. The return type is int (an integer).
return a + b;
• This method is overloaded. It takes three integers as parameters and returns their sum. It has the same method name,
but the parameters are different from the first one (this one has an extra integer).
• This method is also overloaded but it takes two double values as parameters and returns their sum as a double. This
method is different because it uses double type parameters and return type.
• Class Main
• The Main class contains the main method, where the program begins its execution.
A new instance of the Calculator class is created and assigned to the variable calc.
• This calls the method add that takes two integers as parameters. The method signature is public int add(int a, int
b), so it adds 10 and 20, resulting in 30.
• This calls the method add that takes three integers as parameters. The method signature is public int add(int a, int
b, int c), so it adds 10 + 20 + 30, resulting in 60.
• This calls the method add that takes two doubles as parameters. The method signature is public double
add(double a, double b), so it adds 10.5 + 20.5, resulting in 31.0.
o add(10, 20) matches the first method (int add(int a, int b)).
o add(10, 20, 30) matches the second method (int add(int a, int b, int c)).
o add(10.5, 20.5) matches the third method (double add(double a, double b)).
• Final Output:
• Conclusion:
The program demonstrates method overloading where the method add is defined multiple times with different
parameter lists, and the appropriate method is called based on the arguments passed.
Compile-time polymorphism occurs here, meaning the method that gets executed is determined at compile time
based on the method signature.
Runtime Polymorphism (Method Overriding)
• Runtime polymorphism occurs when a subclass provides a specific
implementation of a method that is already defined in its superclass. This
is known as method overriding. In this case, the method that gets
executed is determined at runtime based on the object type.
class Animal {
// Method in the superclass
public void sound() {
System.out.println("Animal makes a sound");
}
}
• However, the Dog class overrides the sound() method with its own version, which prints "Dog barks" instead of
the default message from Animal.
1. Polymorphism:
o In the main method, we are demonstrating runtime polymorphism.
o The variables animal1 and animal2 are references of type Animal, but they are pointing to objects of type Dog
and Cat respectively.
3. Method Calls:
o animal1.sound();: Since animal1 is referencing a Dog object, the overridden sound() method in the Dog class
is executed, which prints "Dog barks".
o animal2.sound();: Similarly, since animal2 is referencing a Cat object, the overridden sound() method in the
Cat class is executed, which prints "Cat meows".
Explanation of Runtime Polymorphism:
At compile time, Java knows that both animal1 and animal2 are references of type Animal.
However, at runtime, it sees that animal1 actually points to an object of type Dog and animal2 points to an object of
type Cat.
Based on the actual object types, Java calls the overridden methods in the Dog and Cat classes, not the one in the
Animal class.
• This is runtime polymorphism because the method that is invoked (sound()) is decided during execution based on the
type of the object (Dog or Cat), not the reference type (Animal).
Conclusion
1. Superclass (Parent Class): The class whose properties and methods are inherited by another class.
2. Subclass (Child Class): The class that inherits the properties and methods from the superclass.
// Constructor
public Animal(String name) {
this.name = name;
}
It has a field name, a constructor to initialize name, and two methods: sound() (which prints a generic sound)
and display() (which prints the name of the animal).
• Subclass (Dog):
The Dog class inherits from the Animal class. It uses the super keyword to call the superclass constructor,
initializing the name.
The Dog class overrides the sound() method from the Animal class to give a specific implementation (i.e.,
printing "Dog barks").
dog.display() is called, which is inherited from Animal, and it prints the name of the animal.
dog.sound() is called, which calls the overridden sound() method from Dog and prints "Dog barks".
// Superclass
Example of Multilevel Inheritance
class Animal {
public void sound() {
System.out.println("Animal makes a sound");
}
}
// Subclass
class Dog extends Animal {
public void sound() {
System.out.println("Dog barks");
}
}
• Puppy squeaks
• In this case, Puppy is a subclass of Dog, and Dog is a subclass of Animal. This is an example of multilevel
inheritance.
• Conclusion:
Inheritance helps create a hierarchy between classes, allowing for code reuse, extensibility, and better
organization of classes.
Java supports single, multilevel, and hierarchical inheritance, and provides the super keyword for accessing
superclass members.
It's important to remember that constructors are not inherited, but methods can be inherited or overridden
by subclasses.
Multiple inheritance