Questions tagged [polymorphism]

In computer science, polymorphism is a programming language feature that allows values of different data types to be handled in a uniform manner.

Polymorphism is a class of programming language features that allow a programmer to provide a single interface for multiple types. There are three main kinds of polymorphism.

  • - partial order defined on the set of types such that any provable property for a supertype also holds for all its subtypes.
  • or - where separate functions can share the same identifier.
  • or - where functions and collections can be specified generically so that they can be used on / contain any type.
10189 questions
1863
votes
20 answers

When to use virtual destructors?

I have a solid understanding of most OOP theory but the one thing that confuses me a lot is virtual destructors. I thought that the destructor always gets called no matter what and for every object in the chain. When are you meant to make them…
Lodle
  • 31,277
  • 19
  • 64
  • 91
1626
votes
28 answers

Why do we need virtual functions in C++?

I'm learning C++ and I'm just getting into virtual functions. From what I've read (in the book and online), virtual functions are functions in the base class that you can override in derived classes. But earlier in the book, when learning about…
Jake Wilson
  • 88,616
  • 93
  • 252
  • 370
896
votes
19 answers

Is List a subclass of List? Why are Java generics not implicitly polymorphic?

I'm a bit confused about how Java generics handle inheritance / polymorphism. Assume the following hierarchy - Animal (Parent) Dog - Cat (Children) So suppose I have a method doSomething(List animals). By all the rules of inheritance and…
froadie
  • 79,995
  • 75
  • 166
  • 235
636
votes
29 answers

What is polymorphism, what is it for, and how is it used?

What is polymorphism, what is it for, and how is it used?
UnkwnTech
  • 88,102
  • 65
  • 184
  • 229
377
votes
21 answers

Polymorphism vs Overriding vs Overloading

In terms of Java, when someone asks: what is polymorphism? Would overloading or overriding be an acceptable answer? I think there is a bit more to it than that. IF you had a abstract base class that defined a method with no implementation, and…
Brian G
  • 53,704
  • 58
  • 125
  • 140
253
votes
4 answers

Why does an overridden function in the derived class hide other overloads of the base class?

Consider the code : #include class Base { public: virtual void gogo(int a){ printf(" Base :: gogo (int) \n"); }; virtual void gogo(int* a){ printf(" Base :: gogo (int*) \n"); }; }; class Derived :…
Aman Aggarwal
  • 3,905
  • 4
  • 26
  • 38
199
votes
18 answers

What is the main difference between Inheritance and Polymorphism?

I was presented with this question in an end of module open book exam today and found myself lost. I was reading Head first Javaand both definitions seemed to be exactly the same. I was just wondering what the MAIN difference was for my own piece of…
Darren Burgess
  • 4,200
  • 6
  • 27
  • 43
198
votes
7 answers

Re-raise exception with a different type and message, preserving existing information

I'm writing a module and want to have a unified exception hierarchy for the exceptions that it can raise (e.g. inheriting from a FooError abstract class for all the foo module's specific exceptions). This allows users of the module to catch those…
bignose
  • 30,281
  • 14
  • 77
  • 110
173
votes
2 answers

Pure virtual destructor in C++

Is it wrong to write: class A { public: virtual ~A() = 0; }; for an abstract base class? At least that compiles in MSVC... Will it crash at run time?
Ivan Krechetov
  • 18,802
  • 8
  • 49
  • 60
162
votes
8 answers

Polymorphism: Why use "List list = new ArrayList" instead of "ArrayList list = new ArrayList"?

Possible Duplicate: Why should the interface for a Java class be prefered? When should I use List list = new ArrayList(); ArrayList inherits from List, so if some features in ArrayList aren't in List, then I will have lost some…
hqt
  • 29,632
  • 51
  • 171
  • 250
156
votes
13 answers

How to call base.base.method()?

// Cannot change source code class Base { public virtual void Say() { Console.WriteLine("Called from Base."); } } // Cannot change source code class Derived : Base { public override void Say() { …
AZ.
  • 7,333
  • 6
  • 44
  • 62
156
votes
6 answers

Private virtual method in C++

What is the advantage of making a private method virtual in C++? I have noticed this in an open source C++ project: class HTMLDocument : public Document, public CachedResourceClient { private: virtual bool childAllowed(Node*); virtual…
silverburgh
  • 8,659
  • 10
  • 31
  • 24
152
votes
12 answers

In Java, how do I call a base class's method from the overriding method in a derived class?

I have two Java classes: B, which extends another class A, as follows : class A { public void myMethod() { /* ... */ } } class B extends A { public void myMethod() { /* Another code */ } } I would like to call the A.myMethod() from…
Creasixtine
  • 740
  • 3
  • 11
  • 33
147
votes
11 answers

Jump into interface implementation in Eclipse IDE

You know how in Eclipse, pressing F3 over a method will take you to its declaration? Well I have a method that is part of an interface; clicking F3 over this naturally takes me to the declaring interface. Obviously there is an object implementing…
aeq
  • 10,377
  • 15
  • 45
  • 46
146
votes
14 answers

What is the difference between dynamic and static polymorphism in Java?

Can anyone provide a simple example that explains the difference between Dynamic and Static polymorphism in Java?
Prabhakar Manthena
  • 2,223
  • 3
  • 16
  • 30
1
2 3
99 100