What is Polymorphism and How Does it Work in Object-Oriented Programming?

  • Thread starter Punkyc7
  • Start date
In summary, polymorphism allows for accessing classes that implement the same interface without knowing the specific class. It is not the same as inheritance but is closely related. An example of polymorphism is being able to use the method MakeSound() on different animals without knowing the specific animal class.
  • #1
Punkyc7
420
0
I am confused to what a polymorphism is. I thought that it was when a class implements an abstract class but after reading things on the web I am not sure if that is right. It seems like everyplace defines it slightly differently. So my question is am I thinking about it right or is there another definition that is the agreed upon definition?
 
Physics news on Phys.org
  • #2
In its basis, polymorphism means that you can access classes that implement the same interface through that interface, without knowing which class it is. Or, put simply, where the outside of the classes looks the same - and you can use that without having to know exactly which class it is.

For example, consider often-used example of a zoo. "Before" OO, we would write things like
Code:
Animal frisk = new Cat();

// ...

if (frisk is Cat)
{
  frisk.Meow();
}
else if (frisk is Dog)
{
  frisk.Bark();
}
... // and so on

Polymorphism allows us to write
Code:
Animal frisk = new Cat();

// ...

frisk.MakeSound();

where MakeSound() is in the public interface (for example, it is defined in the abstract class Animal) and each animal knows which sound it makes, so the correct method will get called automatically.

Note that polymorphism is not the same as inheritance - although they are closely related. For example, I wouldn't say that my Ford Fiesta and your Mercedes E90 have a common base class necessarily, yet we would both know how to drive each others car because the "public interface" (position of the accelerator and brakes, etc.) are the same.
 
Last edited:

1. What is polymorphism?

Polymorphism is a concept in biology and computer science that refers to the ability of a single entity to take on multiple forms or characteristics. In biology, this can refer to the different physical forms that an organism can have during its lifetime. In computer science, it refers to the ability of a single function or object to behave in different ways depending on the input or context.

2. How does polymorphism work?

In biology, polymorphism is often caused by genetic variations, where different alleles of a gene code for different physical traits. In computer science, polymorphism is achieved through inheritance, where objects can inherit properties and behaviors from parent objects, but also have their own unique properties and behaviors.

3. What are the benefits of polymorphism?

In biology, polymorphism can increase the survival and adaptability of a species by allowing individuals to have different physical characteristics that may be better suited for different environments. In computer science, polymorphism allows for more efficient and flexible programming, as a single function or object can be used in multiple contexts.

4. Are there different types of polymorphism?

Yes, there are two main types of polymorphism in computer science: static polymorphism and dynamic polymorphism. Static polymorphism, also known as compile-time polymorphism, is achieved through function overloading and operator overloading. Dynamic polymorphism, also known as run-time polymorphism, is achieved through method overriding and virtual functions.

5. How is polymorphism used in real-world applications?

Polymorphism is used in many programming languages and is an important concept in object-oriented programming. It allows for more efficient and flexible code, as well as better organization and reusability of code. It is commonly used in applications such as video games, mobile apps, and web development.

Similar threads

  • Programming and Computer Science
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
930
  • Engineering and Comp Sci Homework Help
Replies
4
Views
720
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
10
Views
974
  • Programming and Computer Science
Replies
32
Views
5K
  • Programming and Computer Science
2
Replies
41
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Introductory Physics Homework Help
Replies
10
Views
314
Back
Top