0% found this document useful (0 votes)
7 views3 pages

submission prep

The document explains compile-time polymorphism through method overloading, where multiple methods with the same name but different parameters are resolved at compile time. It also covers runtime polymorphism via method overriding, where a subclass provides a specific implementation of a method from its parent class, determined at runtime. Example code illustrates both concepts with outputs demonstrating the functionality of each type of polymorphism.

Uploaded by

xbu029
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)
7 views3 pages

submission prep

The document explains compile-time polymorphism through method overloading, where multiple methods with the same name but different parameters are resolved at compile time. It also covers runtime polymorphism via method overriding, where a subclass provides a specific implementation of a method from its parent class, determined at runtime. Example code illustrates both concepts with outputs demonstrating the functionality of each type of polymorphism.

Uploaded by

xbu029
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/ 3

1.

Compile-time Polymorphism (Method Overloading)

• Achieved using method overloading, where multiple methods have the same name but
different parameters.

• The method to be executed is decided at compile time.

Code:
class CompileTimePoly {

// Method Overloading

void display(int a) {

System.out.println("Integer value: " + a);

void display(double a) {

System.out.println("Double value: " + a);

void display(String a) {

System.out.println("String value: " + a);

public class PolymorphismExample1 {

public static void main(String[] args) {

CompileTimePoly obj = new CompileTimePoly();

obj.display(10); // Calls display(int)

obj.display(3.14); // Calls display(double)

obj.display("Hello"); // Calls display(String)

}
Output:
Integer value: 10

Double value: 3.14

String value: Hello

2. Runtime Polymorphism (Method Overriding)


• Achieved using method overriding, where a subclass provides a specific implementation of a
method already defined in its parent class.

• The method to be executed is determined at runtime based on the actual object.

Code:
class Parent {

void show() {

System.out.println("Parent class method");

class Child extends Parent {

@Override

void show() {

System.out.println("Child class method (Overridden)");

public class PolymorphismExample2 {

public static void main(String[] args) {

Parent obj1 = new Parent(); // Parent reference, Parent object

obj1.show(); // Calls Parent's show()

Parent obj2 = new Child(); // Parent reference, Child object

obj2.show(); // Calls Child's overridden show() at runtime

}
}

Output:
Parent class method

Child class method (Overridden)

You might also like