Compile time polymorphism in java with example

Howard 78 Published: 08/05/2024

Compile time polymorphism in java with example

I can't respond in English only as per the rules. Here is the answer:

compile-time polymorphism

Compile-time polymorphism, also known as static dispatch or early binding, is a form of polymorphism that happens at compile time. This means that the compiler decides which method to call based on the type of object passed as an argument.

Here's an example in Java:

public class Animal {

public void sound() {

System.out.println("Animal makes a sound");

}

}

public class Dog extends Animal {

@Override

public void sound() {

System.out.println("Dog barks");

}

}

public class Cat extends Animal {

@Override

public void sound() {

System.out.println("Cat meows");

}

}

In this example, Animal is an abstract base class with a method sound. Dog and Cat are subclasses that override the sound method. Now, if you have a variable of type Animal and call the sound method on it:

public static void main(String[] args) {

Animal animal = new Dog();

animal.sound(); // Output: Dog barks

animal = new Cat();

animal.sound(); // Output: Cat meows

}

The compiler knows which method to call based on the type of object assigned to the animal variable. This is an example of compile-time polymorphism, also known as static dispatch or early binding.

This approach has some advantages:

It allows for more flexibility in programming, as you can change the behavior of a method at runtime without changing its signature. It reduces the overhead associated with dynamic dispatch at runtime.

However, this approach also has some limitations:

The compiler needs to know the exact type of the object at compile time. This means that the code becomes less flexible and more rigid. It can be less efficient than dynamic dispatch, as the compiler needs to perform additional checks and jumps at runtime.

In Java, you can achieve compile-time polymorphism using interfaces and abstract classes, as shown in this example.

What is a real life example of polymorphism in Java?

One common and powerful real-life example of polymorphism in Java is the use of abstract classes for shape drawing. Imagine you want to draw different shapes (circles, squares, triangles) on a canvas using Java. You can define an abstract class called Shape with a method called draw(). This method should be implemented differently by each concrete subclass that extends from the Shape class.

Here is a simple example:

public abstract class Shape {

public abstract void draw();

}

public class Circle extends Shape {

@Override

public void draw() {

System.out.println("Drawing a circle.");

}

}

public class Square extends Shape {

@Override

public void draw() {

System.out.println("Drawing a square.");

}

}

public class Triangle extends Shape {

@Override

public void draw() {

System.out.println("Drawing a triangle.");

}

}

Now, imagine you have an array of Shape objects:

Shape[] shapes = new Shape[3];

shapes[0] = new Circle();

shapes[1] = new Square();

shapes[2] = new Triangle();

When you iterate over the array and call the draw() method on each shape, polymorphism takes place. The correct implementation of the draw() method is called based on the actual object type. So, when you do this:

for (Shape s : shapes) {

s.draw();

}

You'll see that each shape is drawn according to its specific drawing needs:

Drawing a circle.

Drawing a square.

Drawing a triangle.

This demonstrates polymorphism because the draw() method behaves differently based on the actual object type. The Shape reference can refer to different subclasses, and Java's compiler will ensure that the correct implementation is called.

This example illustrates how polymorphism helps you write more flexible, reusable, and maintainable code by avoiding explicit switching statements or long if-else chains. It also showcases abstraction as you're focusing on common characteristics of shapes (having a draw method) without worrying about their specific details.