Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
Polymorphism is one of the four fundamental principles of object-oriented programming (OOP), alongside encapsulation, inheritance, and abstraction. Polymorphism allows objects of different classes to be treated as objects of a common superclass, which enables you to write more flexible and reusable code. In this tutorial, we will cover the basics of polymorphism in Java, including method overriding and interfaces.
Method overriding is a form of polymorphism where a subclass provides its own implementation for a method that already exists in its superclass. This allows the subclass to inherit methods and fields from the superclass and customize its behavior.
Example:
class Animal { void makeSound() { System.out.println("The animal makes a sound"); } } class Dog extends Animal { @Override void makeSound() { System.out.println("The dog barks"); } } class Cat extends Animal { @Override void makeSound() { System.out.println("The cat meows"); } }
In this example, the Dog
and Cat
classes override the makeSound()
method from the Animal
class. This allows each class to provide its own implementation of the method.
With method overriding, you can use a reference of the superclass type to refer to objects of the subclass type, and the appropriate method implementation will be called at runtime based on the actual object type.
Example:
public class Main { public static void main(String[] args) { Animal myAnimal = new Animal(); Animal myDog = new Dog(); Animal myCat = new Cat(); myAnimal.makeSound(); // Output: The animal makes a sound myDog.makeSound(); // Output: The dog barks myCat.makeSound(); // Output: The cat meows } }
Interfaces in Java provide another way to achieve polymorphism. A class can implement one or more interfaces, and objects of that class can be treated as instances of the interface.
Example:
interface Drawable { void draw(); } class Circle implements Drawable { @Override public void draw() { System.out.println("Drawing a circle"); } } class Square implements Drawable { @Override public void draw() { System.out.println("Drawing a square"); } }
In this example, both Circle
and Square
classes implement the Drawable
interface and provide their own implementation of the draw()
method.
With interfaces, you can use a reference of the interface type to refer to objects of the implementing class, and the appropriate method implementation will be called at runtime based on the actual object type.
Example:
public class Main { public static void main(String[] args) { Drawable[] drawables = new Drawable[2]; drawables[0] = new Circle(); drawables[1] = new Square(); for (Drawable drawable : drawables) { drawable.draw(); } } }
Output:
Drawing a circle Drawing a square
In this tutorial, we covered the basics of polymorphism in Java, including method overriding and interfaces. Polymorphism allows objects of different classes to be treated as objects of a common superclass or interface, which enables you to write more flexible and reusable code in your Java applications.
Java Method Overriding and Polymorphism:
class Animal { void sound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { void sound() { System.out.println("Dog barks"); } }
Compile-time vs Runtime Polymorphism in Java:
// Compile-time polymorphism (method overloading) class MathOperations { int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } }
Polymorphism in Java with Examples:
Animal myDog = new Dog(); myDog.sound(); // Calls Dog's overridden sound method
Java Polymorphism vs Encapsulation:
class EncapsulationExample { private int data; public int getData() { return data; } public void setData(int newData) { data = newData; } }
Dynamic Method Dispatch in Java:
Animal myAnimal = new Dog(); // Upcasting myAnimal.sound(); // Calls Dog's overridden sound method at runtime
Polymorphism and Interfaces in Java:
interface Shape { void draw(); } class Circle implements Shape { void draw() { System.out.println("Drawing a circle"); } }
Covariant Return Types and Polymorphism in Java:
class Animal { Animal reproduce() { return new Animal(); } } class Dog extends Animal { @Override Dog reproduce() { return new Dog(); } }
Abstract Classes and Polymorphism in Java:
abstract class Shape { abstract void draw(); } class Circle extends Shape { void draw() { System.out.println("Drawing a circle"); } }
Java Polymorphism and Method Signatures:
class MathOperations { int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } }