3

As stated here

https://standardofnorms.wordpress.com/2012/09/02/4-pillars-of-object-oriented-programming/

and as the correct answer in many job interviews - the general correct answer for the question: "What are the 4 pillars of OOP?" is:

  1. Abstraction

  2. Encapsulation

  3. Inheritance

  4. Polymorphism

What I fail to understand is how inheritance not contained in polymorphism?

in other words, how can polymorphism be used without the use of inheritance?

The only way I know of using polymorphism is

class A{
    virtual void foo(){cout<<"A";}
    void bar(){cout<<"A";}
};
class B : public A{
    virtual foo(){cout<<"B";}
};

A* ab = new B();
ab->foo();//prints B, using polymorphism
ab->bar();//prints A, using inheritance

A* a = new A();
a->foo();//prints A
a->bar();//prints A, obviously

As I see it, polymorphism brings with it inheritance.

Please explain why it is distinct - or why can't inheritance be discarded as a key pillar of its own. We could use polymorphism or not.

sji
  • 1,877
  • 1
  • 13
  • 28
Gulzar
  • 23,452
  • 27
  • 113
  • 201

3 Answers3

2

What I fail to understand is how inheritence not contained in polymorphism?

in other words, how can polymorphism be used without the use of inheritence?

There are 3 main types of polymorphism, and only one of them requires inheritance to work.

  1. Ad-hoc polymorphism: This is more commonly known as function/method overloading, where multiple functions can share the same name but have different signatures. Whether or not the return type is part of the signature is language dependent.

  2. Parametric polymorphism: in OOP, this is more commonly known as generics, where a function/method can work with multiple concrete types, and return multiple concrete types, providing compile time safety.

  3. Subtype polymorphism: This is the one I think most people think of when they talk about polymorphism. As you know, this is when subtypes provide different implementation of their parent functions/methods.

You can read more about the different types of polymorphism from the wikipedia article here: https://en.wikipedia.org/wiki/Polymorphism_(computer_science)

jmrah
  • 5,715
  • 3
  • 30
  • 37
  • I thought the return type can differ. – Maarten Jul 12 '16 at 14:37
  • Yes, you are right. The return type can differ as long as the argument signature differs (at least in Java, not sure about other languages). I'll clarify the answer. – jmrah Jul 12 '16 at 14:42
1

As I understand the two concepts:

Inheritance

You could use inheritance without using polymorphisim. For example:

class Base {
public:
  void foo();
};

class Derived : public Base {
};

int main() {
  Derived d;
  d.foo();
}

Here we use the common functionality of the base type in all derived types, but at no point do we do anything polymophic (we never look at the derived instance though its base interface).

Polymorphism

Polymorphism as a concept includes more than the standard inheritance based method seen most often. This is actually subtyping which is just one kind of Polymorphism.

Writing a template method is technically a form of polymophism, and function overloading another. As a concept you could argue that many other things are ways to achieve polymophism.

For example:

// This function must be given an object that has a method foo().
template <typename T> bar(T& t) {
  t.foo();
}

This is polymorphic behavior without inheritance.

See: https://stackoverflow.com/a/10556406/1230538 for a really good explaination of this given by someone else.

Summary

99% you use inheritance to achieve polymorphism in most modern programming languages, but they are different concepts, and can exist/be used independently of each other.

Community
  • 1
  • 1
sji
  • 1,877
  • 1
  • 13
  • 28
-1

Polymorphism without inheritance:

class A {
    virtual void foo() { cout << "A"; }
};

class B {
    virtual void foo() { cout << "B"; }
};

A* a = new A();
A* b = new B();

a->foo(); // prints A
b->foo(); // prints B

Both instances have the same method. So they are polymorphic. However, the method does different things because the objects are different after all.

Ilya Vassilevsky
  • 981
  • 6
  • 14