answersLogoWhite

0


Best Answer
Answer

the biggest advantage lies in creation of reusable code by programmers, you dont care about the specific objects used just like driving a car without knowing what plugs are in the engine. multiple forms of one object are called in the same way

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

Polymorphism is when several related objects can be referred through a common ancestor, which reduces programming complexity. One example from a video game perspective might be an Enemy class. Each enemy uses a different type of attack, but from the programming perspective, it would be more efficient to let the Enemy determine how it attacks, the damage it deals, etc.

Without polymorphic code, you would have to do something like the following:

while(!gameOver) {

foreach(SlowShip ship:SlowShips) {

ship.attackPlayer();

}

foreach(FastShip ship:FastShips) {

ship.attackPlayer();

}

// One loop for each type of enemy.

}

With polymorphic code, you can do the following:

while(!gameOver) {

foreach(EnemyShip enemy:EnemyShips) {

enemy.attackPlayer();

} // Handles any different number of enemy types.

}

This code is more efficient because it only processes enemy types that are actually present, instead of every type of enemy in the game.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Polymorphism and inheritance are often treated as being the same thing because you cannot have runtime polymorphism without inheritance. However, you can have inheritance without runtime polymorphism. You can also have compile time polymorphism without inheritance.

Inheritance relates to the way in which a new class of object can be derived from an existing class of object. The new class inherits all the public and protected members of the existing class, and thus allows us to create a more specialised version of the existing class without having to rewrite code that is common to both. The existing class is referred to as the base class. Derived classes may inherit from two or more base classes and base classes may themselves be derivatives, thus creating a hierarchy (a family-tree) of classes, with the least-derived class(es) serving as the root(s).

The only rule regarding inheritance is that the least-derived class or classes in an inheritance hierarchy must have a virtual destructor. This ensures that whenever any class of object within a hierarchy is destroyed, the most-derived destructor is always invoked first. This is the exact reverse of construction, where the least-derived base class constructor is always invoked first.

At this point it is worth noting that inheritance is not the only way to make use of existing code. Composition offers an alternative method by embedding an instance of one or more classes inside another. The containing class does not inherit the properties of the embedded members, but it can expose as much or as a little of the embedded member interfaces as required. It can also use delegation to provide more specialised interfaces. Composition does not allow runtime polymorphic behaviour, however you can still use inheritance without runtime polymorphism by employing private inheritance. Public inheritance enables runtime polymorphic behaviour.

Polymorphism comes in two forms: compile time and runtime. Other than the name, they actually have very little in common. Polymorphism literally means "multiple forms". Humans are polymorphic in that we are all human but we take many forms, such as our varying ethnicities, language, skills, etc. A Scotsman and an Afro-Caribbean are both types of human, as are painters and sailors. In programming terms, we can say that these different types of human are "specialisations" of the human base class. The human base class provides everything that is common to all humans (mammal, ape-like, etc) while the specialisations cater for traits that are uncommon.

Compile time polymorphism specifically relates to template classes and functions, where an implementation only differs by type. Rather than writing out the same implementations over and over to cater for these different types, we simply define the implementation once and let the compiler generate the specific classes and functions for us, according to the types we actually use. For instance, a std::vector<int> object is an example of compile time polymorphism in that it provides us with a dynamic array specialised to store integer sequences.

Runtime polymorphism is different in that it makes use of public inheritance. Any object that can publicly derive from a base class has an "is-a" relationship with that base class. A square object "is-a" kind of shape, thus a std::vector<shape> allows us to store all kinds of shapes within the same container. Without public inheritance this example would be impossible to achieve.

Squares and circles are specialisations of the shape base class, however quite often we do not need to know the specifics of the actual shape, we can simply talk about shapes in broad, generalised terms. Every type of shape can be drawn, thus every shape should have a draw() method, and each specialisation must provide its own implementation of that method. But how can we invoke those methods if we don't know the actual type of shape? That's where runtime polymorphism comes into play.

Inheritance allows an object to inherit all the public and protected members of its base class. However, when those members are declared virtual within the base class, we gain polymorphic behaviour (provided the base class is publicly inherited). When a function is declared virtual, the most-derived override of that function is always invoked first (just as it is when the base class destructor is declared virtual). As a result of this, we can be sure that when we invoke the virtual draw() method upon a shape of unknown type, we can be certain that a circle object will draw a circle and a square will draw a square. In other words, objects behave according to their specific type even when we cannot determine the actual type. We can, of course, determine the actual type any time we wish simply by switching on the object's runtime type, but this is an expensive operation. More importantly, if the triangle class didn't exist when we wrote our code, knowing the type is rendered useless. Runtime polymorphism overcomes these problems with relative ease and low cost.

The cost of runtime polymorphism comes in the form of a virtual table (often shortened to v-table), which consumes memory. Every class that declares or inherits a virtual method has a virtual table associated with it and the most-derived object's class provides the complete table for that object, mapping the virtual function addresses to the most-derived overrides of those functions. Thus when you implicitly invoke a virtual method, the compiler performs a simple table lookup to determine which function to invoke. If you need to invoke a specific implementation of that function in one of the object's base classes, you can use scope resolution to do so. This is often used when a specialisation can make use of a generic implementation in one of its base classes which it then augments with more specialised code, thus minimising code duplication (which may introduce inconsistencies).

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

The only advantage is that when you have a pointer or reference to a base class (as you would within the base class itself, for instance) there is absolutely no need to know the derived object's type in order to make it "do the right thing". You simply make the call against the base class and the virtual table routes the call to the derived class override.

Without polymorphic calls you'd have to use expensive runtime information to determine the actual derived type in order to downcast the object to that type and then make the appropriate call. With a well-designed base class and a virtual interface there should never be any need to downcast (if there is then your design is probably flawed). You certainly don't want to downcast from within the base class itself since that would require your base class to be blessed with knowledge of derived classes that have yet to be conceived, which would be impossible. The virtual table provides all the functionality you'll ever need both now and in the future, with absolute minimal cost.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

the obvious answer is...

Polymorphism reduces the effort required to extend an object system by enabling a number of different operations to share the same name.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Polymorphism allows you to use the same function name but with different arguments (of different type).

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the advantages and disadvantages of polymorphism and inheritance?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is a relationship in following a-polymorphism b-inheritance c-overloading d-none of these options?

Inheritance.


3 pillars of object oriented programming?

abstraction, inheritance, encapsulation, and polymorphism.


What are the benefits and application of OOP?

Encapsulation, inheritance, polymorphism and abstraction.


What are the characteristics of OOPS?

Data Encapsulation, Abstraction, Inheritance, Polymorphism


Can you have inheritance without polymorphism?

Yes. Inheritance and polymorphism are two different things. Inheritance is when the attributes and methods of a class are inherited by a deriving class that creates a more specialized type. Polymorphism is when two methods exist with the same name, differing only in argument types, or in class type. The former type, argument types, is an example of ad-hoc polymorphism that does not even require a class.


Examples for oops concepts?

polymorphism,inheritance,encapsulation,objects,classes


How is run time polymorphism accomplished?

Through inheritance and virtual functions.


What are the advantages and disadvantages of inheritance tax?

An advantage of the inheritance tax is that it raises money for the government. A disadvantage is that people that inherit property may not have the means to pay the taxes on the items.


What are the various elements of Object oriented programming?

Encapsulation, data hiding, inheritance and polymorphism.


What are the basic concept of object oriented programming language?

Inheritance Encapsulation Polymorphism Abstraction


What is oops.explain briefly the elements of oops?

if any system supports for abstraction,encapsulation,inheritance and polymorphism.


What are the features of object oriented program?

The features of object oriented programming are Abstraction, Encapsulation, Polymorphism &amp; Inheritance