• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

Java Polymorphism

by admin

Polymorphism, which refers to the idea of “having many forms”, occurs when there is a hierarchy of classes related to each other through inheritance.

A call to a member method will cause a different implementation to be executed, depending on the type of object invoking the method.

Dog and Cat are classes that inherit from the Animal class. Each class has its own implementation of the makeSound() method.

Example:

class Animal {
  public void makeSound() {
    System.out.println("Grr...");
  }
}
class Cat extends Animal {
  public void makeSound() {
    System.out.println("Meow");
  }
}
class Dog extends Animal {
  public void makeSound() {
    System.out.println("Woof");
  }

Filed Under: Java

Some more articles you might also be interested in …

  1. Beginners Guide to Interfaces and Lambda Expressions
  2. Java String Classes – StringBuilder and StringTokenizer
  3. Introduction to Exceptions in Java
  4. kotlin Command Examples (Kotlin application launcher)
  5. Overloading of Methods in Java
  6. Java Classes and Objects Introduction
  7. Basics of Java Operators
  8. Java – Documentation Comments
  9. Java Introduction
  10. Java – Arrays

You May Also Like

Primary Sidebar

Recent Posts

  • Vanilla OS 2 Released: A New Era for Linux Enthusiasts
  • mk Command Examples
  • mixxx Command Examples
  • mix Command Examples

© 2025 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright