AI May 15, 2023 5 min read

Module 11: Polymorphism

BR

Bhargava Ram Pantina

Web Developer & AI Enthusiast

🔄 What is Polymorphism?

Polymorphism means "many forms". It allows objects to take on multiple forms depending on the context. In Java, it allows one interface to be used for a general class of actions.

There are two types:

  • Compile-time Polymorphism (Method Overloading)
  • Runtime Polymorphism (Method Overriding)

📚 Method Overloading (Compile-Time)

Same method name with different parameters in the same class.

                    class MathUtils {
                        int add(int a, int b) {
                            return a + b;
                        }
                    
                        double add(double a, double b) {
                            return a + b;
                        }
                    
                        public static void main(String[] args) {
                            MathUtils mu = new MathUtils();
                            System.out.println(mu.add(2, 3));      // 5
                            System.out.println(mu.add(2.5, 3.1));  // 5.6
                        }
                    }
                    

🚀 Method Overriding (Runtime)

A subclass provides a specific implementation of a method already defined in its superclass.

                    class Animal {
                        void makeSound() {
                            System.out.println("Animal makes sound");
                        }
                    }
                    
                    class Cat extends Animal {
                        void makeSound() {
                            System.out.println("Cat meows");
                        }
                    
                        public static void main(String[] args) {
                            Animal a = new Cat();
                            a.makeSound();  // Cat meows
                        }
                    }
                    
Artificial Intelligence Web Development Technology Trends Machine Learning UX Design

Share this article

About the Author

BR

Bhargava Ram Pantina

Bhargava Ram is a web developer and AI enthusiast with over 8 years of experience in the tech industry. He specializes in creating innovative web solutions that leverage the latest technologies to solve real-world problems.

Related Articles

AI Mar 15, 2023

Implementing ChatGPT in Your Web Application: A Step-by-Step Tutorial

Learn how to integrate OpenAI's ChatGPT into your web application to create intelligent chatbots and enhance user interactions.

Read More →
Web Development Apr 22, 2023

Building Accessible Web Forms: A Complete Guide

Learn how to create web forms that are accessible to all users, including those with disabilities. This comprehensive guide covers ARIA attributes, keyboard navigation, and more.

Read More →

Subscribe to My Newsletter

Get the latest articles and resources delivered straight to your inbox.

No spam, just valuable content. Unsubscribe anytime.