Difference between inheritance and polymorphism?

This can be a confusing topic because one can argue that sharing method attributes between a super or abstract class with a derived class could be described by either Polymorphism or Inheritance. One significant difference could be illustrated as to when or under what circumstances you would you use one or the other.

An ideal instance of using Inheritance would be when you want to create an entirely new class, but wish to borrow a group of existing attributes or methods resident in an existing Abstract or super Class, instead of re-inventing the wheel. If you had an Abstract Class Carnivore and wanted to create a subclass Cat, you could instantly inherit all the methods that were common in Carnivore that applied to Cat without writing new code.

Polymorphism could be best applied when you had an existing SubClass that you wanted to modify or add a feature that could borrow a method that existed in a higher class. Take the Cat subclass, which inherited attributes from Carnivore, and you wanted to add the method Stalk, which would generically describe how a carnivore approaches its prey. However its implementation in Cat would detail the stealth it uses that are unique to a Cat.

Inheritance is the object oriented feature by which the functionality and features of one class are made available to another class without having to copy paste the code.

Polymorphism is the object oriented feature by which multiple methods of a class can coexist with minor differences. Ex: method overloading.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: