Java Polymorphism

JK JK 2026 
Created at  
7,062 0 0

Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.

Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.

For example, think of a superclass called Animal that has a method called animalSound(). Subclasses of Animals could be Pigs, Cats, Dogs, Birds - And they also have their own implementation of an animal sound (the pig oinks, and the cat meows, etc.):

class Animal {
  public void animalSound() {
    System.out.println("The animal makes a sound");
  }
}

class Pig extends Animal {
  public void animalSound() {
    System.out.println("The pig says: wee wee");
  }
}

class Dog extends Animal {
  public void animalSound() {
    System.out.println("The dog says: bow wow");
  }
}

Remember from the Inheritance chapter that we use the extends keyword to inherit from a class.

Now we can create Pig and Dog objects and call the animalSound() method on both of them:

class Animal {
  public void animalSound() {
    System.out.println("The animal makes a sound");
  }
}

class Pig extends Animal {
  public void animalSound() {
    System.out.println("The pig says: wee wee");
  }
}

class Dog extends Animal {
  public void animalSound() {
    System.out.println("The dog says: bow wow");
  }
}

class Main {
  public static void main(String[] args) {
    Animal myAnimal = new Animal();  // Create a Animal object
    Animal myPig = new Pig();  // Create a Pig object
    Animal myDog = new Dog();  // Create a Dog object
    myAnimal.animalSound();
    myPig.animalSound();
    myDog.animalSound();
  }
}

Why And When To Use "Inheritance" and "Polymorphism"?
- It is useful for code reusability: reuse attributes and methods of an existing class when you create a new class.

 

Tags Java Java Polymorphism Facebook X
Comments 0
Similar posts
  1. Java Inheritance (Subclass and Superclass)
    7,184
  2. Java Inner Classes
    7,060
  3. Java Packages
    7,181
  4. Java Classes and Objects
    7,093
  5. Java Recursion
    7,204
  6. Java Scope
    7,109
  7. Java Methods
    7,093
  8. Java While Loop/Do While Loop/For Loop/For-Each Loop/Break/Continue
    7,877
  9. Java Switch Statements
    7,416
  10. Java Short Hand If...Else (Ternary Operator)
    7,585
  11. Java If ... Else
    7,405
  12. Java Math
    7,442
  13. Java Variables
    7,476
  14. Java Comments
    8,023
  15. The Print() Method
    7,159
  16. Java Syntax
    7,449
  17. Java Getting Started
    7,414
  18. What is Java?
    7,095
  19. Machine Learning Types and Programming Languages
    7,243
  20. Java Tutorials associated with AP Computer Science A
    7,627
  21. Creating a simple Java Servlet (Web Server Page) with Apache Maven on Microsoft Windows
    7,175
  22. Java Servlet Example
    7,127
  23. Difference between Java and Javascript
    7,629
  24. Challenge: One Code Problem Per Day
    1,114
  1. How do I replace content that based on the HTML UI Template
    7,160
  2. Dataset of California Foodbanks
    7,176
  3. Java Abstract Classes and Methods
    7,745
  4. Java Arrays
    8,727
Recently updated
  1. Why You Can't Stop Watching Jack Bryan's Beatbox Gaming Shorts
    79
  2. Song So-hee: The Soulful Voice Touching Hearts Through Korean Music
    72
  3. Watch This Dazzling Dancer’s Mind-Blowing Moves
    122
  4. Michael Jackson's Billie Jean
    183
  5. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    165
  6. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    537
  7. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    271
  8. The Evolution and Production Reality of Agentic AI
    238
  9. How to Activate or Waive Your UIUC Student Health Insurance
    324
  10. Complete Guide to Building a Machine Learning Model
    355
  11. My life cuts at Las Vegas during Thanksgiving day holiday
    7,439
  12. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    382
  13. Harness vs. OpenClaw: Two Very Different "Agents"
    1,016
  14. Clean Python Environments: The Power of venv vs. Docker
    859
  15. What is Docker? Why is Docker also useful in a development environment?
    645
  16. UIUC 2026-2027 Academic Calendar
    1,653
  17. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    836
  18. Open-Source LLMs: The AI Revolution
    788
  19. Resume 2.0: Leveling Up for My First Software Gig
    2,302
  20. Not everyone will understand what this man just did
    1,943
  21. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,624
  22. Unpacking IU's Shopper
    794
  23. Jackie Chan's Police Story: The Action Masterpiece
    685
  24. The IVE Story: Identity, 'I AM' Charts, and Influence
    988
  25. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,204
  26. Open Databases for Sex Crime Occurrences in the U.S.
    738
  27. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    2,725
  28. My First Day at University of Illinois-Urvana Champaign
    1,222
  29. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,166
  30. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,248
  31. Sipping the Stars: My Starbucks Adventure
    9,708
  32. Exciting explore at Sequoia National Park
    7,689
  33. My Life Shot at Death Valley
    1,750
  34. Ip Man fights with Muay Thai Master
    965
  35. Mad Clown - Don't Die
    1,060
  36. How to get Student Enrollment and Degree Verification at UIUC
    4,993
  37. LAX Thanksgiving Rush: A Joyful Reunion
    943
  38. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,154
  39. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    1,010
  40. 10CM - To Reach You (너에게 닿기를)
    1,181
  41. Feeling weak? Transform yourself at the UIUC ARC!
    1,591
  42. BOYNEXTDOOR - If I Say I Love You
    1,205
  43. The Future of Software Engineer - AI Engineering
    945
  44. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    942
  45. Lie - Legend song by BIGBANG
    7,841
  46. Why ROLLBACK is useful when you work with Google Gemini CLI?
    869
  47. Reimbursement after Vaccination at McKinley Health Center
    1,044
  48. Gemini CLI makes a Magic! Time to speed up your app development with Google Gemini CLI!
    985
  49. Common Questions from UIUC school life in terms of CS Program
    1,150
  50. UIUC Immunization Compliance
    1,238