0% found this document useful (0 votes)
14 views4 pages

Ava Polymorphism

Polymorphism in Java is a core concept of Object-Oriented Programming that allows objects to be treated as instances of their parent class, enabling methods to behave differently based on the object type. It includes compile-time polymorphism (method overloading) and runtime polymorphism (method overriding), each with distinct characteristics and advantages. While polymorphism enhances flexibility and reusability, it may also introduce performance costs and complexity if misused.

Uploaded by

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

Ava Polymorphism

Polymorphism in Java is a core concept of Object-Oriented Programming that allows objects to be treated as instances of their parent class, enabling methods to behave differently based on the object type. It includes compile-time polymorphism (method overloading) and runtime polymorphism (method overriding), each with distinct characteristics and advantages. While polymorphism enhances flexibility and reusability, it may also introduce performance costs and complexity if misused.

Uploaded by

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

ava Polymorphism

Definition: Polymorphism is one of the core concepts of Object-Oriented Programming (OOP)


that allows objects to be treated as instances of their parent class rather than their actual class.
This concept enables a single action to behave differently based on the object it is acting upon.

Types of Polymorphism in Java:

1. Compile-time Polymorphism (Method Overloading)


2. Runtime Polymorphism (Method Overriding)

1. Compile-time Polymorphism (Method Overloading)

Method Overloading:

 Occurs when multiple methods in the same class have the same name but different
parameters (different type, number, or both).
 The method to be called is determined at compile-time based on the method signature.

class OverloadExample {

void display(int a) {

System.out.println("Argument: " + a);

void display(int a, int b) {

System.out.println("Arguments: " + a + " and " + b);

void display(String a) {

System.out.println("Argument: " + a);

public class Main {

public static void main(String[] args) {


OverloadExample obj = new OverloadExample();

obj.display(10);

obj.display(10, 20);

obj.display("Hello");

Characteristics:

 Methods must have different parameter lists.


 It does not depend on return types.
 Enhances readability and reusability of code.

Runtime Polymorphism (Method Overriding)

Method Overriding:

 Occurs when a subclass provides a specific implementation for a method that is already defined
in its superclass.
 The method call is determined at runtime based on the object's type.

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 myDog = new Dog();

Animal myCat = new Cat();

myDog.sound(); // Outputs: Dog barks

myCat.sound(); // Outputs: Cat meows

Characteristics:

 Methods must have the same name, return type, and parameters.
 Allows a class to inherit the properties of another class and modify them to fit its needs.
 Promotes the use of interfaces and abstract classes.

Important Points:

 The @Override annotation is used to inform the compiler that the method is intended to
override a method in the superclass.
 Method overriding allows the child class to provide a specific implementation of a
method that is already defined in its parent class.
 Polymorphism in Java is achieved through inheritance and interfaces.
Advantages of Polymorphism

1. Flexibility: Allows methods to use objects of different types as if they were objects of the same
type.
2. Maintainability: Reduces code complexity by allowing the same interface to be used for
different underlying forms.
3. Extensibility: New classes can be introduced with minimal changes to existing code.
4. Reusability: Common code can be reused across different classes.

Disadvantages of Polymorphism

1. Performance Cost: May introduce a slight overhead due to the dynamic method resolution at
runtime.
2. Complexity: Can make code harder to understand and maintain if overused or misused.

Conclusion

Polymorphism is a powerful feature in Java that promotes flexibility, maintainability, and


reusability of code. By understanding and applying polymorphism effectively, developers can
write more efficient and robust applications.

You might also like