Example : A simple Java program to show Dynamic Polymorphism/Method Overriding.
class Animal 
{
    public void makeSound() 
    {
        System.out.println("The animal makes its own sound");
    }
}

class Dog extends Animal 
{
    @Override
    public void makeSound() 
    {
        System.out.println("Dogs sound as Woof!");
    }
}

class Cat extends Animal 
{
    @Override
    public void makeSound() 
    {
        System.out.println("Cats sound as Meow!");
    }
}

class Poly 
{
    public static void main(String[] args) 
    {
        Animal obj1= new Animal();
        Animal obj2 = new Dog();
        Animal obj3 = new Cat();

        obj1.makeSound(); 
        obj2.makeSound(); 
        obj3.makeSound(); 
    }
}

Output:

The animal makes its own sound
Dogs sound as Woof!
Cats sound as Meow!
Example : A simple Java program to show Static Polymorphism/Method Overloding.
(Method Overloading is a feature of java in which a class may have more than one method with same name, but different number and datatype of arguments.)


(I) A Method overloading concept having different number of arguments in the list - 

import java.io.*;
class Example
	{  
	   void display(char c)  // Single Argument
	   {
            System.out.println(c);
	   }
	   void display(char c, int num)   // Two Arguments 
	   {
            System.out.println(c +" "+num);
	   }
           void display(char c, int num, float val)   // Three Arguments 
	   {
            System.out.println(c +" "+num +" "+val);
	   }
	   void display(char c, int num, float val, double y)   // four Arguments 
	   {
            System.out.println(c +" "+num +" "+val+" "+y);
	   }
	}
class Overload
{
   public static void main(String args[])
   {
       Example obj = new Example();
       obj.display('X');
       obj.display('Y',800);
       obj.display('P',1400,23.5412f);
       obj.display('P',1400,23.5412f,543.0123);
   }
}

Output :
X
Y 800
P 1400 23.5412
P 1400 23.5412 543.0123

//  --------------------------------------------------

(II) A Method overloading concept having different datatypes as arguments in the list - 

import java.io.*;
class Example
	{  
	   void display(char c)  
	   {
            System.out.println(c);
	   }

	   void display(int num)    
	   {
            System.out.println(num);
	   }

           void display(double val)   
	   {
            System.out.println(val);
	   }

	   void display(String str)   
	   {
            System.out.println(str);
	   }
	}

class Overload
{
   public static void main(String args[])
   {
       Example obj = new Example();
       obj.display('X');
       obj.display(800);
       obj.display(23.12);
       obj.display("codershelpline");
   }
}

Output :
X
800
23.12
codershelpline

// ---------------------------------------------------------

(III) A Method overloading concept having different sequence of datatype as argument in the list - 

import java.io.*;
class Example
	{  
	   void display(int num,char c,double val)  
	   {
            System.out.println(c +" "+num +" "+val);
	   }

	   void display(double val,int num,char c)   
	   {
            System.out.println(c +" "+num +" "+val);
	   }

           void display(char c, int num, double val)    
	   {
            System.out.println(c +" "+num +" "+val);
	   }
	}

class Overload
{
   public static void main(String args[])
   {
       Example obj = new Example();
       obj.display(1400,'P',23.12);
       obj.display(23.12,1400,'P');
       obj.display('P',1400,23.12);
   }
}

Output :
P 1400 23.12
P 1400 23.12
P 1400 23.12

Loading

Categories: Java

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.