Polymorphism in JAVA - BasicITtopic

Polymorphism in JAVA

Polymorphism in Java

Polymorphism in Java:-

Polymorphism in Java is an object-oriented concept which is used to create different from a single resource.

The polymorphism can be achieved by two different ways in java:-

  1. with the help of overloading that is known as compile time polymorphism.
  2. with the help of overriding, that is known as runtime polymorphism.

 

Overloading

Java support overloading by two different ways:-

  1. method overloading.
  2. constructor overloading.

Method Overloading

When more than one method design within a single class with the same name but the argument is different is known as method overloading.

Example

class Test

{

               void area(int r)

               {

                               System.out.println("area of a circle :"+(3.14*r*r));

               }

               void area(int l,int b)

               {

                               System.out.println("area of rect :"+(l*b));

               }

               public static void main(String[] args)

               {

                               Test t=new Test();

                               t.area(4);

                               t.area(4,5);

               }

}

Output

area of a circle :50.24

area of rect :20

 

Constructor Overloading:-

When more than one constructor design within a single class with a different argument that is known as constructor overloading..

Example

class Test

{

       Test()

       {

                       System.out.println("Test() with no args");

       }Test(String s)

       {

                       System.out.println("Test() with string args");

       }

       Test(int x)

       {

                       System.out.println("Test() with int args");

       }

       public static void main(String[] args)

       {

                       new Test();

                       new Test(12);
                       new Test("abcd");


       }

}
Output:-
Test() with no args
Test() with int args
Test() with string args

Runtime Polymorphism

Java support runtime polymorphism with the help of method overriding. Method overriding only applicable when the signature (prototype, return type, name, argument) of the superclass is the same as the subclass method, then it is known as method overriding.

  • Overriding supports the concept of dynamic method dispatching. Dynamic method dispatching possible by two different ways.
  1. Upcasting
  2. Downcasting

Method overriding

When the signature of the parent class method and subclass method is totally the same then it is known as method overriding or redesign.

Restriction of method overriding:-

  1. If the method is final it cannot override in java.
  2. A static method can override with static keyword only.
  3. The access specifier of the subclass method should be same or less restriction compared to the superclass method.
  4. If the return type is class name then subclass method return type should be same or its subclass compares with the parent class.

Example:-

class Test
{
public static void show()
{
System.out.println("show in Test.class"); 
}
}
class Demo extends Test
{

public static void show()
{
System.out.println("show in Demo.class");
}
public static void main(String[] args) 
{
Test.show();
Demo.show();
}
}
Output:-
show in Test.class
show in Demo.class
basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community