polymorphism c# interview questions

When the same entity (function or object) behaves differently in different scenarios, it is known as Polymorphism in C++. The topics Covered Under Polymorphism in C++ are:

C Tutorial: polymorphism in C

To show how we might implement polymorphic behavior via inheritance we will implement a general Shape hierarchy. For now, shapes will only have the ability to compute their area. We will implement concrete Square and Circle shapes that will be “instances of” the Shape “class.” Before showing the code, I’ll first describe the general design. The Shape base will be responsible for two things: (1) storing a table of function pointers that point to the implementations of specific (concrete) functions (e.g., Circle’s Area function) and (2) invoking the correct concrete function with the appropriate concrete instance. This is done by storing a function table and pointer to the concrete type in a Shape instance. This code is shown below.

Polymorphic behavior is an incredibly useful construct in software systems. There are generally two ways by which a program can be constructed to exhibit polymorphic behavior. Namely, through composition or inheritance. Polymorphism via composition relies on (1) well-defined and narrow interfaces and (2) other objects or types containing references to things that implement those interfaces. To give a modern example, consider the following Go program.

Inheritance, on the other hand, achieves polymorphic behavior through the interface of a class. Or, put another way, composition allows reuse at the object level whereas inheritance allows for reuse at the class level. For example, if a class called Child inherits from a class called Parent then it must be true that a Child instance can be substituted in any place where an instance of type Parent is needed. This is the basic idea behind the Liskov Substitution Principle [1] and is the L in the famed SOLID principles for OO programming.

Even still, inheritance has its place for a reason. When an object truly does exemplify the is-a relationship instead of a has-a relationship, inheritance is perfectly plausible. For example, an array list is most certainly a specific type of list, and so making an array list implementation inherit from a generic list implementation (or implement a list interface) is quite acceptable.

That’s it. We have the pieces necessary to create concrete Shape types and then instantiate general Shapes from them. This allows us to pass around Circles and Squares wherever Shapes are needed, thereby abiding by Liskov’s principle. The code below shows how we might create a Circle and Square instance, “cast” them to general Shape instances, and then compute their area through the shape_Area function.

The word “polymorphism” means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. A real-life example of polymorphism is a person who at the same time can have different characteristics. Like a man at the same time is a father, a husband and an employee. So the same person exhibits different behavior in different situations. This is called polymorphism. Polymorphism is considered as one of the important features of Object-Oriented Programming. In C++, polymorphism is mainly divided into two types:

This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Tutorials

Polymorphism means “many forms”, and it occurs when we have many classes that are related to each other by inheritance.

Like we specified in the previous chapter; Inheritance lets us inherit fields and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.

For example, think of a base class called Animal that has a method called animalSound(). Derived classes of Animals could be Pigs, Cats, Dogs, Birds – And they also have their own implementation of an animal sound (the pig oinks, and the cat meows, etc.):

Remember from the Inheritance chapter that we use the : symbol to inherit from a class.

Now we can create Pig and Dog objects and call the animalSound() method on both of them:

The output will be:

What is Polymorphism in C++?

Polymorphism in C++ means, the same entity (function or object) behaves differently in different scenarios.Consider this example:

The “ +” operator in c++ can perform two specific functions at two different scenarios i.e when the “+” operator is used in numbers, it performs addition.

And the same “+” operator is used in the string, it performs concatenation.

FAQ

What is polymorphism in C?

Master C and Embedded C Programming- Learn as you go

Polymorphism is a key feature of object oriented programming that means having multiple forms. This is divided into compile time polymorphism and runtime polymorphism in C++. An example of compile time polymorphism is function overloading or operator overloading.

Is polymorphism possible in C?

Polymorphism is possible in C language. Explanation: It is possible to implement polymorphism in C language, even though it doesn’t support class. We can use structures and then declare pointers which in turn points to some function.

How many types of polymorphism are there in C?

In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. A real-life example of polymorphism is a person who at the same time can have different characteristics. Like a man at the same time is a father, a husband and an employee.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *