Home / Java / Polymorphism in Java with examples

Polymorphism in Java with examples

Polymorphism is one of the core concepts of any Object Oriented Programing language. Polymorphism is Latin word and its meaning is that an object having a different form like water, ice and vapor all are a form of H2O.

There are two types of polymorphism in Java these are

  • Compile Time Polymorphism, achieved by Overloading concept
  • Run Time Polymorphism, achieved by the Overriding

Method Overriding in Java:-

Method Overriding means, when a method override older method, generally this concept use in inheritance. Suppose we have super class have a function ‘myfun’ and super class is extending by subclass and in subclass function ‘myfun’ would be overridden if the following rule will satisfy.

These rules are:-

  1. Superclass Function that should be overridden in subclass, cannot be static and final.
  2. In superclass, the function can have public, protected and default accessibility, while in subclass it can be same as in super class or widen than as declaring in the super

For Example:-

If is superclass function has protected accessibility than in subclass same function can have protected or public accessibility.

  1. The new method definition must have the same method signature, e., the method name, and the types and the number of parameters, including their order, are the same as in the overridden method.
  2. return type can be the covariant type, i.e. it should object reference only and in subclass function return type must be only subclass or return type function is a superclass

Polymorphism java

As from the above diagram we can see that Animal is a class have a function sound(). Class Cat and Class Dog both are extending the Class Animal also Both the classes are overriding sound() function.

Polymorphism Java Example:-

package one;
class Animal
{
void sound()
{
System.out.println("Basic Sound");
}
}
class Cat extends Animal
{
void sound()
{// Overriding
System.out.println("Cat Sound");
}
}
class Dog extends Animal
{
void sound()
{// Overriding
System.out.println("Dog Sound");
}
}
public class test
{
public static void main(String[] args)
{
Animal animal = new Animal();
animal.sound();
Cat cat = new Cat();
cat.sound();
Dog dog = new Dog();
dog.sound();
animal = cat;// Typecasting
animal.sound();
animal = dog;// Typecasting
animal.sound();
}
}

Output is:-

Basic Sound.

Cat Sound.

Dog Sound.

Cat Sound.

Dog Sound.

Explanation of Java Polymorphism Example:-

  1. we are creating a new object of class Animal, and calling sound() function, it will print “Basic Sound”
  2. Creating new object of class Cat, and calling sound() function, it will print “Cat Sound”
  3. Creating new object of class Dog, and calling sound() function, it will print “Dog Sound”
  4. Now we are assigning cat object to an animal, is this valid? YES, it is absolutely valid, we can always point subclass to the superclass, this is also known as Typecastinge. in compile time JVM will not through any error. JVM check at run time that animal referred cat object, so by calling sound function by animal will call Cat’s sound function because basically animal pointing to Cat and will give output “Cat Sound”
  5. Same as above and output would be “Dog Sound”

Method Overloading in Java:-

It determines at compile time. A function is overloaded if the signature of the method different.

Signature compromise as datatype + number of arguments

  • Number of parameters should be different
  • If number of argument is same than datatype should not be same

If fun is same with respect signature and the return value is different than that function will not be overloaded and will give compile time error.

Method Overloading Java Example:-

void myfun(int a, double b)         { /* … */ }                                          // (1)

int myfun(int a)                                { return a; }                                         // (2)

int myfun()                                         { return 1; }                                         // (3)

long myfun(double a, int b)         { return b; }                                         // (4)

long myfun(int x, double y)         { return x; }                                         // (5) Compile Time error

Suppose we have function void myfun(int a, double b)  { /* … */ }

int myfun(int a)                                { return a; }

// this is overloaded function as it has one parameter of type int

int myfun()                                         { return 1; }

// this is overloaded function as it has zero parameter

long myfun(double a, int b)         { return b; }

// this is overloaded function as it has two parameter and same datatype as given in first declaration but order is different.

long myfun(int x, double y)         { return x; }

// Compile time error as it is exactly same as first declaration.

 

Polymorphism in Java with examples
5 (100%) 1 vote

About

(royal52) is a software engineer. He is good at authoritative link building, Content Writing, SEO, Web Development, Wordpress, HTML, CSS, JAVASCRIPT, Game Development, Unity3d, Graphic Designing, Logo Designing, Android Development and Ios Development. Last but not least, he is passionate about development and gaming.

Check Also

amazon db 1

How to use Amazon DynamoDB for Java Enterprise Development

You will learn basics of Amazon DynamoDB used by java enterprise development experts for projects. …

Leave a Reply