What is polymorphism vs inheritance Java?

Selma 168 Published: 08/08/2024

What is polymorphism vs inheritance Java?

I'd be happy to explain the concepts of polymorphism and inheritance in Java.

Inheritance

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class to inherit properties, methods, and behaviors from another class. In Java, a child class inherits attributes and methods from its parent or superclass using the extends keyword. The child class inherits all public and protected members of the superclass.

For example, consider a hierarchy where a Car is a subclass of a Vehicle. The Vehicle class might have common attributes like color and speed, which would be inherited by the Car class:

public abstract class Vehicle {

public String color;

public int speed;

public Vehicle(String color, int speed) {

this.color = color;

this.speed = speed;

}

}

public class Car extends Vehicle {

public Car(String color, int speed) {

super(color, speed);

}

}

In this example, the Car class inherits the color and speed attributes from its superclass Vehicle. The child class can also add new attributes or methods specific to itself.

Polymorphism

Polymorphism is the ability of an object or a method to take on multiple forms. In Java, there are two types of polymorphism: compile-time (static) and runtime (dynamic).

Compile-time Polymorphism (Method Overloading)

Compile-time polymorphism occurs when multiple methods with the same name but different parameter lists are defined in a class. The correct method is called based on the number and types of arguments passed to it at compile time:

public class Calculator {

public int sum(int x, int y) { return x + y; }

public double sum(double x, double y) { return x + y; }

public static void main(String[] args) {

Calculator calc = new Calculator();

int result1 = calc.sum(2, 3); // calls int sum(int, int)

double result2 = calc.sum(2.5, 3.7); // calls double sum(double, double)

}

}

In this example, the Calculator class has two methods named sum, but with different parameter types. The correct method is called based on the argument types at compile time.

Runtime Polymorphism (Method Overriding)

Runtime polymorphism occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. The child class's method overrides the parent class's method:

public abstract class Animal {

public void sound() { System.out.println(" generic animal sound"); }

}

public class Dog extends Animal {

@Override

public void sound() { System.out.println("Woof!"); }

}

public class Cat extends Animal {

@Override

public void sound() { System.out.println("Meow!"); }

}

public class Main {

public static void main(String[] args) {

Animal myPet = new Dog();

myPet.sound(); // outputs "Woof!"

myPet = new Cat();

myPet.sound(); // outputs "Meow!"

}

}

In this example, the Dog and Cat classes override the sound() method of their superclass Animal. The correct implementation is called based on the object's actual class at runtime.

In summary, inheritance allows a child class to inherit attributes and methods from its parent class, while polymorphism enables objects or methods to take on multiple forms. Both concepts are essential in Java programming and help create reusable code and improve modularity.

What is polymorphism in Java with realtime example?

I'm happy to help!

Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows for more flexibility and dynamic behavior in your code. In Java, polymorphism enables you to treat objects of different classes as if they were of the same class.

There are two main types of polymorphism: compile-time polymorphism (method overloading) and runtime polymorphism (method overriding).

Method Overloading (Compile-time Polymorphism)

Method overloading is when multiple methods with the same name but different parameters can be defined in a single class. This allows for more flexibility in how you interact with an object, as the correct method to call will be determined based on the number and types of parameters passed.

Here's an example:

public class Calculator {

public int add(int x, int y) { return x + y; }

public double add(double x, double y) { return x + y; }

}

Calculator calc = new Calculator();

int result1 = calc.add(2, 3); // calls the int-based method

double result2 = calc.add(2.5, 3.5); // calls the double-based method

System.out.println(result1); // prints 5

System.out.println(result2); // prints 6.0

In this example, we have a Calculator class with two methods named add, one for integers and one for doubles. The correct method to call is determined based on the types of parameters passed.

Method Overriding (Runtime Polymorphism)

Method overriding is when a subclass provides a different implementation of a method that is already defined in its superclass. This allows for more specialized behavior at runtime, as the correct method to call will be determined based on the object's actual class.

Here's an example:

public abstract 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!"); }

}

Animal myPet = new Dog();

myPet.sound(); // outputs "Dog barks!"

myPet = new Cat();

myPet.sound(); // outputs "Cat meows!"

In this example, we have an Animal abstract class with a sound() method. Two concrete subclasses, Dog and Cat, override the sound() method to provide their own implementation. When we create objects of these classes and call the sound() method, the correct overridden method will be called based on the object's actual class.

In summary, polymorphism in Java allows for more flexibility and dynamic behavior by enabling you to treat objects of different classes as if they were of the same class. Method overloading provides compile-time polymorphism, while method overriding provides runtime polymorphism.