What is the difference between polymorphism and duck typing?

后端 未结 4 1449
心在旅途
心在旅途 2020-12-12 16:10

I\'m a little confused with the two terms, here\'s what I know:

Polymorphism is the ability of object of different types to be handled by a common interface. While d

4条回答
  •  温柔的废话
    2020-12-12 16:13

    Two type Polymorphism

    1. Method overloading(Compile time Polymorphism ).
    2. Method overriding(Run Time Polymorphism ).

    Method overloading :- same function name and different data type is known as Method overloading

    Example :

      int addTwovalues(int a, int b)
      { return (a+b)}
    
      float addTwovalues(float a, float b)
      { return (a+b)}
    
      Method overriding :- same function name and same data type but different Class
         is known as       Method overriding.
    
    
      class a
     {
      virtual int addtwovalues()
       {  // to do  }
      }
     class b:a
     {
         override int addtwovalues()
       {  // to do  }
    
      }
    
    
    
      a obj=new a();
      obj.addtwovalues();
    
      b objb=new a();
      objb.addtwovalues();  //run time Polymorphism 
    

提交回复
热议问题