0% found this document useful (0 votes)
13 views1 page

Polymorphism

Polymorphism in Java allows objects of different classes to be treated as objects of a common superclass, achieved through method overloading and overriding. Compile-time polymorphism involves multiple methods with the same name but different parameters, while runtime polymorphism allows subclasses to provide specific implementations of superclass methods. The benefits include flexibility, reusability, maintainability, and extensibility of code.

Uploaded by

11210208
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views1 page

Polymorphism

Polymorphism in Java allows objects of different classes to be treated as objects of a common superclass, achieved through method overloading and overriding. Compile-time polymorphism involves multiple methods with the same name but different parameters, while runtime polymorphism allows subclasses to provide specific implementations of superclass methods. The benefits include flexibility, reusability, maintainability, and extensibility of code.

Uploaded by

11210208
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

In Java, polymorphism, meaning "many forms," allows objects of different

classes to be treated as objects of a common superclass, enabling a single


action to be performed in different ways. This is achieved through method
overloading and overriding.
Types of Polymorphism:
• Compile-time Polymorphism (Method Overloading): Multiple methods with
the same name but different parameters (signatures) within the same
class. The compiler determines which method to call based on the arguments
provided.
class Calculator {

public int add(int a, int b) { return a + b; }

public double add(double a, double b) { return a + b; }

• }Runtime Polymorphism (Method Overriding): A subclass provides a


specific implementation of a method already defined in its superclass. The
method to be executed is determined at runtime based on the object's actual
type.
o Example:
Java
class Animal {
public void makeSound() { System.out.println("Generic animal
sound"); }
}

class Dog extends Animal {


@Override
public void makeSound() { System.out.println("Woof!"); }
}

class Cat extends Animal {


@Override
public void makeSound() { System.out.println("Meow!"); }
}

Benefits of Polymorphism:
• Flexibility: Code can work with objects of multiple related types without
knowing their specific subclass.
• Reusability: Methods can be written once and used with different object
types.
• Maintainability: Code becomes easier to understand and modify.
• Extensibility: New classes can be added to the hierarchy without modifying
existing code.

You might also like