» » Polymorphism


  • Defining multiple methods with same name
  • Static polymorphism and dynamic polymorphism

Static polymorphism:

  • Its a process of defining multiple methods with the same name and with different parameter types or list or order is called static polymorphism
  • Compile time itself we can say which method is going to get executed based on parameters types.
  • So its also called compile time polymorphism.
  • This will be achieved by method overloading.
  • Method overloading means defining multiple methods with same name and different parameters.
  • If two methods have same method name , But different parameter types those methods are considered as overloaded methods.
  • Will see example program on method overloading.

 Example program on method overloading:


  1. package instanceofjava;
  2.  
  3. public class OverloadingDemo {
  4.  
  5. void add(){
  6.     System.out.println("No-arg method");
  7. }
  8.  
  9. void add(int i){
  10.     System.out.println("int-arg method");
  11. }    
  12.  
  13. void add(String str){
  14.     System.out.println("String-arg method");
  15. }
  16.  
  17. void add(float d){
  18.     System.out.println("float-arg method");
  19. }
  20.  
  21. void add(int a, int b){
  22.     System.out.println("2 int-arg method");
  23. }
  24.  
  25. public static void main(String[] args){
  26.  
  27.     OverloadingDemo obj= new OverloadingDemo();
  28.  
  29.     obj.add();
  30.     obj.add(10);
  31.     obj.add(1.2f);
  32.     obj.add("java");
  33.     obj.add(1, 2);    
  34.  
  35. }
  36. }

Output


  1. No-arg method
  2. int-arg method
  3. float-arg method
  4. String-arg method
  5. 2 int-arg method

Method overloading with reference types:

  • We can also overload method with reference types, check below program.

  1. package instanceofjava;
  2. public class Test {
  3.  
  4. void show(){
  5.  
  6.         System.out.println("test class show() method");
  7. }
  8.  
  9. }

  1. package instanceofjava;
  2.  
  3. public class Example {
  4.  
  5. void method(String s){
  6.  
  7.        System.out.println("String-arg");
  8.  
  9. }
  10.  
  11. void method(Test obj){
  12.  
  13.     System.out.println("method(Test obj)-arg");    
  14.    obj.show();
  15.  
  16. }
  17.  
  18.  public static void main(String[] args){
  19.  
  20.     Example obj= new Example();
  21.     obj.method("polymorphism definition in oops");
  22.     obj.method(new Test());
  23.  
  24.     }
  25. }

Output:


  1. String-arg
  2. method(Test obj)-arg
  3. test class show() method

Dynamic polymorphism:

  • Redefining the super class method in sub class is called "method overriding" .
  • Method overriding is a language feature that allows a sub class to provide a specific implementation of a method that is already provided by one of its super class.
  • The implementation in the sub class overrides the implementation in the super class.
  • This is also called dynamic polymorphism / runtime polymorphism.

  1. package instanceofjava;
  2.  
  3. public class A{
  4.  
  5. void show(String s){
  6.  
  7.   System.out.println("super class method");
  8.  
  9. }
  10.  
  11. }

  1. package instanceofjava;
  2.  
  3. public class B extends A{
  4.  
  5. void show(String s){
  6.  
  7.   System.out.println("sub class method");
  8.  
  9. }
  10.  public static void main(String[] args){
  11. B obj =new B();
  12. obj.show();
  13. }
  14. }

Output:

  1. sub class method

We can not override static methods:

  • Actually static variables and static methods are class level. they are not instance level.
  • So if you try to override a static method will throw compile error
  1. package instanceofjava;
  2. public class Super {
  3.     int a,b;
  4.     static void add(){
  5.         System.out.println("super class add() method");
  6.     }
  7. }


  1. package instanceofjava;
  2. public class Sub extends Super {
  3.  
  4.  void add(){ // ERROR:This instance method cannot override the static method
  5.         System.out.println("this is sub class add() method");
  6. }
  7.  
  8.     public static void main(String [] args){

  9.         Sub obj= new Sub();
  10.  
  11.     } 
  12. }



  • Static methods are class level so we need to access by using class name.

  1. package instanceofjava;
  2. public class Sub extends Super {
  3.  
  4.  static void add(){// works fine
  5.         System.out.println("this is sub class add() method");
  6. }
  7.  
  8. public static void main(String [] args){

  9.    Super.add();
  10.    Sub.add();
  11.  
  12.  
  13.     } 
  14. }

Output:


  1. super class add() method
  2. sub class add() method

Method Overriding - Exceptions:

  •  When we overriding a method from super class to sub class. we need to follow some rules
  • Whenever sub class method throws checked exception then super class method must be declared throws with same exception or super class of that checked exception.
  1. package instanceofjava;
  2. public class Super {
  3.     int a,b;
  4.     void add(){
  5.         System.out.println("super class add() method");
  6.     }
  7. }


  1. package instanceofjava;
  2. public class Sub extends Super {
  3.  
  4.  void add() throws IOException{ // ERROR:     - Exception IOException is not compatible with
  5.                                                       //throws clause in  Super.add()
  6.         System.out.println("this is sub class add() method");
  7. }
  8.  
  9.     public static void main(String [] args){

  10.         Sub obj= new Sub();
  11.  
  12.     } 
  13. }



  1. package instanceofjava;
  2. public class Super {
  3.     int a,b;
  4.     void add()throws IOException{
  5.         System.out.println("super class add() method");
  6.     }
  7. }


  1. package instanceofjava;
  2. public class Sub extends Super {
  3.  
  4.  void add() throws IOException{// works fine
  5.         System.out.println("this is sub class add() method");
  6. }
  7.  
  8.     public static void main(String [] args){

  9.         Sub obj= new Sub();
  10.  
  11.     } 
  12. }

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu