What is polymorphism vs inheritance java javatpoint example
What is polymorphism vs inheritance java javatpoint example
I'd be happy to explain polymorphism and inheritance in Java using examples from Javatpoint.
Inheritance
Inheritance is a mechanism in object-oriented programming that allows one class (the child or subclass) to inherit the properties and behavior of another class (the parent or superclass). This allows for code reuse and facilitates the creation of a hierarchy of classes.
In Java, inheritance is implemented using the extends
keyword. For example:
public class Animal {
public void sound() {
System.out.println("The animal makes a sound");
}
}
public class Dog extends Animal {
@Override
public void sound() {
System.out.println("The dog barks");
}
}
In this example, the Dog
class inherits from the Animal
class. The Dog
class has its own implementation of the sound()
method, which overrides the one in the Animal
class.
Polymorphism
Polymorphism is the ability of an object to take on multiple forms. This can be achieved through method overriding or method overloading.
Method overriding is when a subclass provides a different implementation of a method that is already defined in its superclass.
For example:
public class Animal {
public void eat() {
System.out.println("The animal eats");
}
}
public class Dog extends Animal {
@Override
public void eat() {
System.out.println("The dog eats dog food");
}
}
In this example, the Dog
class overrides the eat()
method of the Animal
class.
Method overloading is when multiple methods with the same name can be defined as long as they have different parameter lists.
For example:
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
}
In this example, the Calculator
class has two methods with the same name add
, but they have different parameter lists.
Example from Javatpoint
Here is an example of polymorphism and inheritance from Javatpoint:
public class Animal {
public void sound() {
System.out.println("The animal makes a sound");
}
}
public class Dog extends Animal {
@Override
public void sound() {
System.out.println("The dog barks");
}
}
public class Cat extends Animal {
@Override
public void sound() {
System.out.println("The cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.sound(); // Output: The dog barks
animal = new Cat();
animal.sound(); // Output: The cat meows
}
}
In this example, the Animal
class is the superclass and the Dog
and Cat
classes are subclasses. The sound()
method is overridden in each subclass to provide its own implementation.
The Main
class creates instances of the Dog
and Cat
classes and assigns them to a variable of type Animal
. When the sound()
method is called on these objects, the correct implementation is executed based on the actual class of the object. This is an example of polymorphism.
In summary, inheritance allows one class to inherit the properties and behavior of another class, while polymorphism is the ability of an object to take on multiple forms through method overriding or method overloading.
What is the difference between inheritance and polymorphism in Java Javatpoint?
I'd be happy to help you with that!
Inheritance and polymorphism are two fundamental concepts in object-oriented programming (OOP) in Java, as well as other languages. While they are related, they serve different purposes.
Inheritance:
Inheritance is a mechanism where one class can inherit the properties and behavior of another class. The child class inherits all the fields and methods of the parent class, including any constructors, finalizers, etc. In Java, inheritance allows you to create a hierarchy of classes that share common characteristics or behaviors.
For example, consider an Animal class with a sound() method:
public abstract class Animal {
public void sound() {
System.out.println("The animal makes a sound");
}
}
You can then create a sub-class like Dog, which inherits the properties of Animal and adds its own specific behavior:
public class Dog extends Animal {
@Override
public void sound() {
System.out.println("The dog barks");
}
}
In this example, Dog is said to inherit from Animal. The Dog class has access to all the methods and fields of the Animal class, including the sound() method.
Polymorphism:
Polymorphism, on the other hand, refers to the ability of an object to take on multiple forms or behaviors. This can be achieved through method overriding or method overloading.
Method Overriding: When a subclass provides its own implementation for a method that is already defined in its superclass, this is called method overriding.
public class Animal {
public void sound() {
System.out.println("The animal makes a sound");
}
}
public class Dog extends Animal {
@Override
public void sound() {
System.out.println("The dog barks");
}
}
In this case, the Dog class overrides the sound() method of the Animal class.
Method Overloading: When multiple methods with the same name but different parameter lists are defined in a class, this is called method overloading.
public class Calculator {
public int add(int x, int y) {
return x + y;
}
public double add(double x, double y) {
return x + y;
}
}
In this example, the Calculator class provides multiple implementations of the add() method for different data types.
Key differences:
Purpose: Inheritance is primarily used to establish a hierarchy of classes that share common characteristics or behaviors. Polymorphism is used to provide multiple forms or behaviors of an object. Relationship: Inheritance involves creating a subclass from another class, whereas polymorphism involves providing alternative implementations for methods or constructors. Behavioral inheritance vs. behavioral polymorphism: Inheritance allows you to inherit the behavior of the superclass, while polymorphism enables you to change the behavior of a method or constructor.In summary, inheritance is about establishing a hierarchy of classes with common characteristics or behaviors, whereas polymorphism is about providing multiple forms or behaviors for an object. Both concepts are essential in Java programming and help create reusable and maintainable code.