0% found this document useful (0 votes)
2 views

Polymorphism Lab in Java

The document outlines a lab exercise in Java focused on understanding and implementing polymorphism, including both compile-time (method overloading) and runtime (method overriding). It provides examples of method overloading in a Calculator class and method overriding in Animal, Dog, and Cat classes, demonstrating how different methods can be invoked based on the object type. The main class, PolymorphismLab, tests both types of polymorphism with expected outputs for each scenario.

Uploaded by

Ebiyo Faf
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Polymorphism Lab in Java

The document outlines a lab exercise in Java focused on understanding and implementing polymorphism, including both compile-time (method overloading) and runtime (method overriding). It provides examples of method overloading in a Calculator class and method overriding in Animal, Dog, and Cat classes, demonstrating how different methods can be invoked based on the object type. The main class, PolymorphismLab, tests both types of polymorphism with expected outputs for each scenario.

Uploaded by

Ebiyo Faf
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Polymorphism Lab in Java

Objective:
To learn and implement polymorphism, both compile-time (method
overloading) and runtime (method overriding), in Java.

Explanation of Polymorphism

1. Polymorphism: The ability of an object to take many forms. In Java, it allows


the same method or property to behave differently based on the object that
calls it.
2. Types of Polymorphism:
- Compile-time polymorphism: Achieved using method overloading, where
multiple methods share the same name but differ in parameters.
- Runtime polymorphism: Achieved using method overriding, where a
subclass provides a specific implementation of a method declared in its parent
class.

Steps for the Lab

1. Compile-Time Polymorphism (Method Overloading)


Create a class with methods having the same name but different parameters.

class Calculator {
// Overloaded methods
public int add(int a, int b) {
return a + b;
}

public double add(double a, double b) {


return a + b;
}

public String add(String a, String b) {


return a + " " + b;
}
}
Method overloading

public class overloading {

public void disp(char c)

System.out.println(c);

public void disp(char c, int num){

System.out.println(c + " "+num);

public static void main(String[] args)

overloading obj1=new overloading();

obj1.disp('a');

obj1.disp('a',5);

Output

public class overloading {

public void disp(char c)


{

System.out.println(c);

public void disp(char b, int num){

System.out.println(b + " " +num);

public static void main(String[] args)

overloading obj1=new overloading();

obj1.disp('a');

obj1.disp('5',6);

}
class DisplayOverloading {

public void disp(char c) {

System.out.println(c);

public void disp(char c, int num) {

System.out.println(c + " "+num);

class sample {

public static void main(String args[]) {

DisplayOverloading obj = new DisplayOverloading();

obj.disp('a',5);

obj.disp('a');

Output
2. Runtime Polymorphism (Method Overriding)
Create a parent class with a method, then override it in a child class.
```java
class Animal {
// Method to be overridden
public void sound() {
System.out.println("Animals make sounds");
}
}

class Dog extends Animal {


// Overriding the sound method
@Override
public void sound() {
System.out.println("Dog barks");
}
}

class Cat extends Animal {


// Overriding the sound method
@Override
public void sound() {
System.out.println("Cat meows");
}
}

Method overriding

package oop;
class Animal{

public void move(){

System.out.println("Animals can move");

class Dog extends Animal{

@Override

public void move(){

System.out.println("Dogs can walk and run");

public class TestDog{

public static void main(String args[]){

Animal a = new Animal(); // Animal reference and object

Animal b = new Dog(); // Animal reference but Dog object

a.move();// runs the method in

//Animal class

b.move();//Runs the method in Dog class

Output
3. Demonstrate Polymorphism
Write a main class to test both types of polymorphism.
```java
public class PolymorphismLab {
public static void main(String[] args) {
// Compile-time Polymorphism
Calculator calc = new Calculator();
System.out.println("Integer addition: " + calc.add(5, 10));
System.out.println("Double addition: " + calc.add(5.5, 10.5));
System.out.println("String concatenation: " + calc.add("Hello", "World"));

// Runtime Polymorphism
Animal myAnimal; // Reference of parent type

myAnimal = new Dog(); // Dog object


myAnimal.sound(); // Calls Dog's sound method

myAnimal = new Cat(); // Cat object


myAnimal.sound(); // Calls Cat's sound method
}
}

Expected Output

Integer addition: 15
Double addition: 16.0
String concatenation: Hello World
Dog barks
Cat meows

Explanation of the Code


1. Compile-Time Polymorphism:
- The Calculator class contains multiple methods named add but with different parameter
types.
- Java decides which method to call based on the number and type of arguments during
compile time.
2. Runtime Polymorphism:
- The Animal class provides a general sound method.
- Subclasses (Dog and Cat) override the sound method with their specific
implementations.
- When a Dog or Cat object is assigned to an Animal reference, the actual object's method
is invoked, not the parent's.

You might also like