Monday, 19 October 2015

Need explanation.....?

I want to know details regarding polymorphism in sense of method overriding.
 

5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Polymorphism means having multiple forms of one thing. In inheritance, polymorphism is done, by method overriding, when both super and sub class have member function with same declaration but different definition.

      If we inherit a class into the derived class and provide a definition for one of the base class's function again inside the derived class, then that function is said to be overridden, and this mechanism is called Function Overriding.

      Inheritance should be there. Function overriding cannot be done within a class. For this we require a derived class and a base class.
      Function that is redefined must have exactly the same declaration in both base and derived class, that means same name, same return type and same parameter list.

      Delete
  2. As I understood,It is mainly used in inheritance concept to override the base function in the derived class
    by using keyword virtual in the base function.

    ReplyDelete
  3. Polymorphism
    Polymorphism means having multiple forms of one thing. In inheritance, polymorphism is done, by method overriding, when both super and sub class have member function with same declaration bu different definition.

    Function Overriding

    If we inherit a class into the derived class and provide a definition for one of the base class's function again inside the derived class, then that function is said to be overridden, and this mechanism is called Function Overriding

    Requirements for Overriding

    Inheritance should be there. Function overriding cannot be done within a class. For this we require a derived class and a base class.
    Function that is redefined must have exactly the same declaration in both base and derived class, that means same name, same return type and same parameter list.
    Example of Function Overriding

    class Base
    {
    public:
    void show()
    {
    cout << "Base class";
    }
    };
    class Derived:public Base
    {
    public:
    void show()
    {
    cout << "Derived Class";
    }
    }

    ReplyDelete