Polymorphism program

Polymorphism theory         

Polymorphism Error Program



 Compile time polymorphism program

                              METHOD OVERLOADING 

1:-                      No of Arg    ( Method overloading )
class Demo
 {
     void show(int a)
     {
         
         System.out.println("Method 1  " +a);
     }
     
     void show(int a,int b)
     {
         System.out.println("method 2  "  +a+" "+ b);
     }
    public static void main(String arg[]){
       
    Demo  obj= new Demo();
    obj.show(34);
    obj.show(22,33);
 }
 }
 OutPUT

 C:\java>javac Testing.java

C:\java>java Demo
Method 1  34
method 2  22 33

C:\java>



 2:-        seq of  arg    ( Method overloading )
class Demo
 {
     void show(int a,String b)
     {
         
         System.out.println("Method 1  " +a+"  "+ b);
     }
     
     void show(String b,int a)
     {
         System.out.println("method 2  "  +a+" "+ b);
     }
    public static void main(String arg[]){
       
    Demo  obj= new Demo();
    obj.show(34,"java");
    obj.show("java",34);
 }
 }
 Output
 
 C:\java>javac Testing.java

C:\java>java Demo
Method 1  34  java
method 2  34 java

C:\java>

3:-Type of Arg       (Method overloading )
class Demo
 {
     void show(String a)
     {
         
         System.out.println("Method 1  " +a);
     }
     
     void show(int a)
     {
         System.out.println("method 2  "  +a);
     }
    public static void main(String arg[]){
       
    Demo  obj= new Demo();
    obj.show("java");
    obj.show(34);
 }
 }
 //Output
 
C:\java>javac Testing.java

C:\java>java Demo
Method 1  java
method 2  34

C:\java>


4:- 

1.Can we achieve method overloading by changing the return type of method only? 

Ans:-Ans:- in java, method overloading is not possible by changing the return type of the method only because of ambiguity. 


class Demo
 {
     int  show(String a)
     {
         
         System.out.println("Method 1  " +a);
     }
     
     float show(String a)
     {
         System.out.println("method 2  "  +a);
     }
    public static void main(String arg[]){
       
    Demo  obj= new Demo();
    obj.show("java");
    obj.show("java");
 }
 }
 
C:\java>javac Testing.java
Testing.java:9: error: method show(String) is already defined in class Demo
         float show(String a)
               ^
1 error

C:\java>

5:- Can we overload java main () method ? 

 

Ans:- yes ,we can have any number of main method overloading.This is because JVM always calls main() method which receives string arrays as arguments only. 

MainMethodOverload1

class Test {

public static void main(String[] args)
{
System.out.println("method 1");
}
public static void main(int a)
{
System.out.println("method 2");
}
}
 
C:\java>javac Testing.java

C:\java>java Test
method 1

C:\java>

or            
 MainMethodOverload2
class Demo
{  
// Overloaded main() method 1  
//invoked when an int value is passed  
public static void main(Integer args)  
{  
System.out.println("Overloaded main() method invoked that parses an integer argument");  
}  
// Overloaded main() method 2  
//invoked when a char is passed  
public static void main(char args)  
{  
System.out.println("Overloaded main() method invoked that parses a char argument");  
}  
//Original main() method  
public static void main(String[] args)  
{  
System.out.println("Original main() method invoked");  
}  
}  
C:\java>javac Testing.java

C:\java>java Demo
Original main() method invoked

C:\java>




or 

MainMethodOverload3

 class Demo
{  
//original main() method  
public static void main(String args[])  
{  
Demo mmo= new Demo ();  
//calling overloaded main() method from the original main() method  
mmo.main();  
}  
//overloaded main() method  
public static void main()  
{  
System.out.println("Overloaded main() method invoked");  
}  
}  


C:\java>javac Testing.java

C:\java>java Demo
Overloaded main() method invoked

C:\java>

or 
MainMethodOverload4
 class Demo
{  
//original main() method  
public static void main(String args[])  
{  
Demo mmo= new Demo ();  
//calling overloaded main() method from the original main() method  
mmo.main();  
mmo.main(23);
mmo.main("sonu");


}  
//overloaded main() method  
public static void main( )  
{  
System.out.println("Overloaded main() method invoked");  
}  
public static void main(Integer p)
{
    System.out.println("Overloaded main() method invoked2" +" "+ p);  
}
public static void main(String args)
{
    System.out.println("Overloaded main() method invoked3"+ args );  
}

}  


C:\java>javac Testing.java

C:\java>java Demo
Overloaded main() method invoked
Overloaded main() method invoked2 23
Overloaded main() method invoked3sonu

C:\java>





or 
MainMethodOverload5
 class Demo  
{  
//Overloaded main() method 1  
public static void main(boolean args)  
{  
if (args)  
{  
System.out.println("First overloaded main() method executed");  
System.out.println(args);  
}  
}  
//Overloaded main method 2  
public static void main(String str)  
{  
System.out.println("Second overloaded main() method executed");  
System.out.println(str);  
}  
//Overloaded main method 3  
public static void main(int args)  
{  
System.out.println("Third overloaded main() method executed");  
System.out.println(args);  
}  
//Original main() method  
public static void main(String[] args)  
{  
System.out.println("Original main() method executed");  
System.out.println("Hello");  
//invoking overloaded main() method 1  
Demo.main(true);  
//invoking overloaded main() method 2  
Demo.main("mango");  
//invoking overloaded main() method 3  
Demo.main(112);  
}  
}  


C:\java>javac Testing.java

C:\java>java Demo
Original main() method executed
Hello
First overloaded main() method executed
true
Second overloaded main() method executed
mango
Third overloaded main() method executed
112

C:\java>

or 
Case 1
class Demo {

public static void main(String[] argos)
{
System.out.println("1");
Demo t= new Demo();
t.main(20);
}
public static void main(int a)
{
System.out.println(a);
}
}

C:\java>javac Testing.java

C:\java>java Demo
1
20

C:\java>


or
class Demo {

void show(int a)
{
System.out.println("int method"+a);
}

void show(String a)
{
System.out.println(" String method"+a);
}
public static void main(String[] args)
{
Demo t= new Demo();
t.show(30);
t.show("sonu");
t.show("java"); //automatic promotion
}
}
C:\java>javac Testing.java

C:\java>java Demo
int method30
 String methodsonu
 String methodjava

C:\java>




Case 2
class Testing
{
void show (char a)
{
System.out.println("object method");

}
void show (String a)
{
System.out.println("String method");
}
public static void main(String[] args)
{
Testing t= new Testing();

t.show('a') ;
t.show("Deepak");

}

}

//
C:\java>javac Testing.java

C:\java>java Testing
object method
String method

C:\java>

Case 3 
class Demo
{
void show (StringBuffer a)
{
System.out.println("String buffer method"+a);
}
void show (String a)
{
System.out.println("String method"+a);
}
public static void main(String[] args)
{
Demo t= new Demo();
t.show("Deepak");
t.show(new StringBuffer("Deepak2"));
t.show("null");

}

}

C:\java>javac Testing.java

C:\java>java Demo
String methodDeepak
String buffer methodDeepak2
String methodDeepak

C:\java>

Case 4
class Demo
{
void show(int a,float b)
{
System.out.println("int float method");
}
void show(float a, int b)
{
System.out.println("Float int method");
}
public static void main(String[] args)
{
Demo t= new Demo();
t.show(10,50.01f);
t.show(50.01f,40);
t.show(10,20);
}
}

C:\java>javac Testing.java

C:\java>java Demo
int float method
Float int method

C:\java>


or 
class Demo
{
void show(int a,float b)
{
System.out.println("int float method");
}
void show(float a,int b)
{
System.out.println("Float int method");
}
public static void main(String[] args)
{
Demo t= new Demo();
t.show(10,50.01f);  
t.show(50.01f,40);
t.show(10,20.0f);
}
}

C:\java>javac Testing.java

C:\java>java Demo
int float method
Float int method
int float method

C:\java>



Case 5 
class Demo{
void show(int a)
{
System.out.prinln("int method");
}
void show(int a)
{
System.out.println("intger method");
}
public static void main(String args){
Demo t= new Demo();
t.show(45);
t.show(45,45,24);
t.show();

} } ya program 'Run nahi hua hai


class Demo
{
   
static void meth()
{
    System.out.println("Good morning bro");
}
static void meth(int a)
{
System.out.println("Good morning "+ a+"bro")    ;// parameter
}
public static void main(String args[])
{
   
    meth(45); //argument
    meth();
   
   
   
}
}
 C:\java>javac Testing.java

C:\java>java Demo
Good morning 45bro
Good morning bro

C:\java>

 Method overriding  


class Demo
{
void show( String a,int b)
{
System.out.println("method 1"+"  "+a+"  "+b);
}
}
class Demo2
{
void show( String a,int b)
{
System.out.println("method 2"+"  "+a);
}
public static void main(String args[])
{
Demo s1= new Demo();
Demo2 s2= new Demo2();
s1.show("sonu");
s2.show(3);
}
}

C:\java>javac Testing.java

C:\java>java Demo2
method 1sonu
method 23

C:\java>

Type of arrgument
class Demo
{
void show( String a)
{
System.out.println("method 1"+"  "+a);
}
}
class Demo2
{
void show( String a)
{
System.out.println("method 2"+"  "+a);
}
public static void main(String args[])
{
Demo s1= new Demo();
Demo2 s2= new Demo2();
s1.show("sonu");
s2.show("so");
}
}

C:\java>javac Testing.java

C:\java>java Demo2
method 1  sonu
method 2  so

C:\java>


Case 1 

 

1.Do overring method must have same return type (or subtype)? 

From java 5.0 onwords it is possible to have different return type for a overriding method in child class , but child’s return types should type of parents return type. This phenomena is known as covariant return type .


Case 2 

Q2 :- overriding and Access- modifiers  

Ans:- the access modifier for an overriding method can allow more ,but not less, access than the overridden method.For example, a protected  instance method in the super-class can be made public .but not private ,in the subclass. Doing so , will generate compile-time error. 





class Demo
{
object show()
{
System.out.println( "method 1");
}
}
class Demo2 extends Demo
{  
public String show()
{  
System.out.println("method 2") ;
}
public static void main (String []args)
{
Demo2 t= new Demo2();

t.show();

Demo obj = new Demo();
Obj.show(); this program not Run
}
}























































                     















Method overloading 

No of arguments

1.
Program No:- 1
Program No:- 1





Squance of arguments
program 1


program 2



Type of arguments

Program :- 1


Program :-2



Method overriding 



large   fornt size 
Program No:- 1

  public class Test {
     void show(){ 
         System.out.println("method 1");
     }
}
class xyz extends Test{
            
    void show(){ 
        System.out.println("method 2");
    }
    public static void main(String[] args) {
       Test obj =new  Test();
        obj.show();
    }
}


Output 

method 1



Program No:-  2


public class Test {

     String  show(){ 

         

         System.out.println("method 1");

         return null;

     }

}

class xyz extends  Test{

            

    String show(){ 

        System.out.println("method 2");

        return null;

    }

    public static void main(String[] args) {

      Test obj =new  Test();

        obj.show();

    }

}


output

method 1


Program No:-  3


      public class  Test {

     int  show(){ 

         

         System.out.println("method 1");

         return 0;

     }

}

class xyz extends ex9{

            

    int show(){ 

        System.out.println("method 2");

        return 0;

    }

    public static void main(String[] args) {

       Test obj =new  Test();

        obj.show();

    }

}

output

method 1

 Program No:-  4


public class  Test {

     Object show(){ 

         

         System.out.println("method 1");

         return null;

     }

}

class xyz extends  Test{

            

    String show(){ 

        System.out.println("method 2");

        return null;

    }

    public static void main(String[] args) {

        Test obj =new  Test();

        obj.show();

    }

}

output 

method 1 


Program No:-  5

public class Test {

     Object show(){ 

         

         System.out.println("method 1"); 

      return null;

     }

}

class xyz extends Test{            

    StringBuffer show(){ 

        System.out.println("method 2");

        return null;

    }

    public static void main(String[] args) {

        Test obj =new Test();

        obj.show();

    }

}

output 

method 1 

 Program No:-  6

public class Test {

     Object show(){ 

         

         System.out.println("method 1"); 

      return null;

     }

}

class xyz extends Test{            

    StringBuilder show(){ 

        System.out.println("method 2");

        return null;

    }

    public static void main(String[] args) {

        Test obj =new Test();

        obj.show();

        

        xyz obj2 =new xyz();

        obj2.show();

 }

}

 output 

method 1 

method 2


Program No:-  7

public class Test {

     Object show(){ 

         

         System.out.println("method 1"); 

      return null;

     }

}

class xyz extends Test{            

      Number show(){ 

        System.out.println("method 2");

        return null;

    }

    public static void main(String[] args) {

        Test obj =new Test();

        obj.show();

        

        xyz obj2 =new xyz();

        obj2.show();

        

        }

}

 output 

method 1 

method 2

Program No:-  8

public class Test {

     Object show(){ 

         

         System.out.println("method 1"); 

      return null;

     }

}

class xyz extends Test{            

      Character show(){ 

        System.out.println("method 2");

        return null;

    }

    public static void main(String[] args) {

        Test obj =new Test();

        obj.show();

        

        xyz obj2 =new xyz();

        obj2.show();

        

        }

}

output 

method 1 

method 2 

Program No:- 9

public class Test {

     Object show(){ 

         

         System.out.println("method 1"); 

      return null;

     }

}

class xyz extends Test{            

      Boolean show(){ 

        System.out.println("method 2");

        return null;

    }

    public static void main(String[] args) {

        Test obj =new Test();

        obj.show();

        

        xyz obj2 =new xyz();

        obj2.show();

                                           }

                                               } 

        

 output 

method 1 

method 2 

Program No:- 10

            public class Test {

         Number show(){ 

         

         System.out.println("method 1"); 

      return 0;

     }

}

class xyz extends Test{            

      Byte show(){ 

        System.out.println("method 2");

        return 0;

    }

    public static void main(String[] args) {

        Test obj =new Test();

        obj.show();

        

        xyz obj2 =new xyz();

        obj2.show();

        

        }

}

 output 

method 1 

method 2  


Program No:-  11

public class Test {

         Number show(){ 

         

         System.out.println("method 1"); 

      return 0;

     }

}

class xyz extends Test{            

      Short show(){ 

        System.out.println("method 2");

        return 0;

    }

    public static void main(String[] args) {

        Test obj =new Test();

        obj.show();

        

        xyz obj2 =new xyz();

        obj2.show();

        

        }

}

 output 

method 1 

method 2  


Program No:-  12

public class Test {

       Number     show(){ 

         

         System.out.println("method 1"); 

      return 0;

     }

}

class xyz extends Test{            

    Integer  show(){ 

        System.out.println("method 2");

        return 0;

    }

    public static void main(String[] args) {

        Test obj =new Test();

        obj.show();

        

       xyz obj2 =new xyz();

        obj2.show();

        

        }

}

  output 

method 1 

method 2 

Program No:-  13

public class Test {

       Number     show(){ 

         

         System.out.println("method 1"); 

      return 0;

     }

}

class xyz extends Test{            

    Long  show(){ 

        System.out.println("method 2");

        return 0;

    }

    public static void main(String[] args) {

        Test obj =new Test();

        obj.show();

        

        xyz obj2 =new xyz();

        obj2.show();

        

        }

}


Program No:-  13

public class Test {

       Number     show()

 System.out.println("method 1"); 

      return 0;

     }

}

class xyz extends Test{            

    Long  show(){ 

        System.out.println("method 2");

        return null;

        

    }

    public static void main(String[] args) {

        Test obj =new Test();

        obj.show();

        

        xyz obj2 =new xyz();

        obj2.show();

        

        }

 output 

method 1 

method 2

Program No:-  13

public class Test {

       Number     show(){ 

         System.out.println("method 1"); 

      return 0;

     }

}

class xyz extends Test{            

    

      Float show(){ 

        System.out.println("method 2");

        return null;

      }

    public static void main(String[] args) {

        Test obj =new Test();

        obj.show();

        xyz obj2 =new xyz();

        obj2.show();

       }

}

output 

method 1 

method 2

Program No:-  14

public class Test {

       Number     show(){ 

         System.out.println("method 1"); 

      return 0;

     }

}

class xyz extends Test{            

    Double show(){ 

        System.out.println("method 2");

        return null;

      }

    public static void main(String[] args) {

        Test obj =new Test();

        obj.show();

        xyz obj2 =new xyz();

        obj2.show();

       }

}

output 

method 1 

method 2



Program No:-  15

Overriding and Access-Modifierspublic class Test {

      Object     show(){ 

         System.out.println("method 1"); 

      return null;

     }

}class xyz extends Test{            

   public  String  show(){ 

        System.out.println("method 2");

        return null;

      }

    public static void main(String[] args) {

        Test obj =new Test();

        obj.show();

        xyz obj2 =new xyz();

        obj2.show();

       }

}

output 

method 1 

method 2

Program No:-  16


public class Test {
      Object     show(){ 
         System.out.println("method 1"); 
      return null;
     }
}class xyz extends Test{            
    protected  String  show(){ 
        System.out.println("method 2");
        return null;
      }
   public static void main(String[] args) {
        Test obj =new Test();
        obj.show();
        xyz obj2 =new xyz();
        obj2.show();
       }
}

output 

method 1 

method 2

Program No:-  17

public class Test {

     public void     show(){ 

         System.out.println("method 1"); 

     

     }

}class xyz extends Test{            

    public void show(){ 

        System.out.println("method 2");

        

      }

   public static void main(String[] args) {

        Test obj =new Test();

        obj.show();

        xyz obj2 =new xyz();

        obj2.show();

       }

}

output 

method 1 

method 2

Program No:-  17


public class Test {

     protected void     show(){ 

         System.out.println("method 1"); 

     

     }

}class xyz extends Test{            

    protected void show(){ 

        System.out.println("method 2");

        

      }

   public static void main(String[] args) {

        Test obj =new Test();

        obj.show();

        xyz obj2 =new xyz();

        obj2.show();

       }

}

Program No:-  17

public class Test {

     protected void     show(){ 

         System.out.println("method 1"); 

     

     }

}class xyz extends Test{            

    public void show(){ 

        System.out.println("method 2");

        

      }

   public static void main(String[] args) {

        Test obj =new Test();

        obj.show();

        xyz obj2 =new xyz();

        obj2.show();

       }

}

        overriding & Exception Handling

Program No:-  18


public class Test {
    void     show(){ 
         System.out.println("method 1"); 
     
     }
}class xyz extends Test{            
   void show() throws ArithmeticException
   { 
        System.out.println("method 2");
        
      }
   public static void main(String[] args) {
        Test obj =new Test();
        obj.show();
        xyz obj2 =new xyz();
        obj2.show();
       }
}

output 

method 1 

method 2

Program No:-  19


Program No:-  20

public class Test {

    void     show()  throws RuntimeException{ 

         System.out.println("method 1"); 

     

     }

}class xyz extends Test{            

   

    

    

    

    void show() throws RuntimeException

   { 

        System.out.println("method 2");

        

      }

   public static void main(String[] args) {

        Test obj =new Test();

        obj.show();

        xyz obj2 =new xyz();

        obj2.show();

       }

}

output 

method 1 

method 2


Program No:-  21

public class Test {

    void     show()  throws RuntimeException{ 

         System.out.println("method 1"); 

     

     }

}class xyz extends Test{            

 void show() throws ArithmeticException

   { 

        System.out.println("method 2");

        

      }

   public static void main(String[] args) {

        Test obj =new Test();

        obj.show();

        xyz obj2 =new xyz();

        obj2.show();

       }

}

Program No:-  22


public class Test {

    void     show()  throws RuntimeException{ 

         System.out.println("method 1"); 

     

     }

}class xyz extends Test{

        void show() 

   { 

        System.out.println("method 2");

        

      }

   public static void main(String[] args) {

        Test obj =new Test();

        obj.show();

        xyz obj2 =new xyz();

        obj2.show();

       }

}

Program No:-  22



 Case 4    :- overriding and Abstract Method

Program No:-  23



Program No:-  24

abstract class Test {
    void     show()  throws RuntimeException{ 
         System.out.println("method 1"); 
     
     }
}class xyz extends Test{
    void show() 
                
   { 
        System.out.println("method 2");
        
      }
   public static void main(String[] args) {
//        Test obj =new Test();
//        obj.show();
        xyz obj2 =new xyz();
        obj2.show();
       }
}

output  

method 2


Program No:-  25

Program No:-  25

abstract class Test {
     abstract void display();
    void     show()  throws RuntimeException{ 
         System.out.println("method 1"); }
}class xyz extends Test{
    void display(){
    }
    void show() 
                
   { 
        System.out.println("method 2");
        
      }
   public static void main(String[] args) {
//        Test obj =new Test();
//        obj.show();
        xyz obj2 =new xyz();
        obj2.show();
       }
}

output  

method 2


Program No:-  26

Program No:-  27

Program No:-  28
interface i1
{
    void display1();
}
 abstract class Test {
     abstract void display();
    void     show() 
    { System.out.println("method 1"); }
 }class xyz implements i1{
     public  void display1(){
         
     }
    void display(){
    }
    void show() { 
        System.out.println("method 2");
        
      }
   public static void main(String[] args) {
       xyz obj2 =new xyz();
        obj2.show();
       }
}

output  

method 2


Program No:-  29

abstract class Test {

    void     show() 

    { System.out.println("method 1");

    }

 }class xyz extends Test{

    

    void show() { 

        super.show();

        System.out.println("method 2");

        

      }

   public static void main(String[] args) {

       xyz obj2 =new xyz();

        obj2.show();

       }


output 

method 1 

method 2

Program No:-  30


Program No:-  31


Program No:-  32


synchronized and stricfp

Program No:-  33


class Test {

     synchronized  void     show() 

    { System.out.println("method 1");

    }

 }class xyz extends Test{

 void show() { 

        

        System.out.println("method 2");

        

      }

   public static void main(String[] args) {

       Test obj2 =new Test();

obj2.show();

       }

}

output 

method 1


Program No:-  33

class Test {

     


  strictfp void     show() 

    { System.out.println("method 1");

    }

 }class xyz extends Test{

     

     

    void show() { 

        

        System.out.println("method 2");

        

      }

   public static void main(String[] args) {

       Test obj2 =new Test();

obj2.show();

       }

}

output  

method 1










 





Program No:-  1

  


Program No:-  2

Program No:-  3


Program No:-  4


Program No:-  5


Program No:-  5



Overriding and Access-Modifiers

Program No:-  6



Program No:-  6








 





Comments