Polymorphism in Java


Polymorphism (many shapes) allows methods to do different things based on the object it is acting upon, enhancing flexibility in code.

Source Code

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

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

class Dog extends Animal {
    public void sound() {
        System.out.println("The dog barks");
    }
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments