1

I am trying to better my understanding of polymorphism.

Say I have a base class called baseClass with one method called foo() and I have three derived classes called derived1 , derived2 and derived3 which all override the and implement their own versions of the foo() method.

If I have an array of baseClass objects can it contain objects that are derived from that base class like so:

baseClassArray: [ derivedObj1, derivedObj2, derivedObj3];

Also if I pull an object out of this array and called the foo() method would it call the method defined in baseClass or the method defined in the respective derived class?

Or does this depend on the programming language this is implemented in?

  • 1
    If it is an object-oriented language, then the method called will be determined by the class of the object and not the base class. – BobDalgleish Feb 19 '14 at 19:40
  • 1
    Any reason you weren't able to try this for yourself? You'd learn a lot more. – JeffO Feb 19 '14 at 20:40
  • @JeffO I was mid lecture at this point haha, I will implement in lter today though, good idea :) – The_Neo Feb 20 '14 at 16:51
  • Keep us posted on what you find and/or modify the question. – JeffO Feb 21 '14 at 16:34
3
  • You're describing polymorphism. You're using the base class as the interface. So your example is like:

((BaseClass)baseClassArray[0]).foo()

or

((BaseClass)derivedObj1).foo()

The foo() method is defined by base class, but the implementation is in the subclass, at least in your example. In OO, if a subclass overrides a method, then that becomes the implementation that gets called.

  • Inheritance is a little different. Let's say you have a derivedObj4 which does not override foo(). Then you can call:

((SubClass)derivedObj4).foo()

which would first look in derivedObj4, see that foo() is not there, and then look to BaseClass for the implementation.

  • polymorphism can rely on inheritance, which makes it a little confusing.

  • The idea behind polymorphism is that the base class defines the methods, and the subclasses define the implementation. (Although they may still inherit the implementation.) In other words, the base class masks the subclass, but the subclass does the work.

  • The idea behind inheritance is that the subclass is the interface, but inherits the implementation from the base class, so it doesn't need to implement it. The subclass masks the base class, but the base class does the work.

1

The actual method that is called will be determined at run time depending what the type of the object is. If you have an array of objects of type baseClass (which can include inherited types) the method that is called will be the foo() method of whichever type the object is.

So the objects that are of type baseClass will have the base class implementation of foo() called. If your objects are one of the derived types and the derived objects override the foo() method this will be called. If the derived objects do not override the foo() method then the base class method foo() will be called.

  • The actual method that is called will be determined at run time depending what the type of the reference is. – radarbob Feb 20 '14 at 1:54
  • That's kind of what I was trying to get at. – Rob Aston Feb 20 '14 at 10:24
1

This should work, but is language specific. what you are referring to is a "upcast", taking the derived objects and upcasting them back to the base class. Depending on your language you will have to perform the cast ex:

derived1 obj1 = new derived1();

baseClass base[10];

base[0] = (baseClass)obj1;

when you call the foo() method the baseClass foo() will be used unless you perform and additional "downcast" ex:

derived1 sameobj1 = (derived1)base[0];

after performing the downcast the foo() method for derived1 will be called. some language can use the "typeOf" comparison operator instead of upcasting and downcasting ex:

if( obj1 typeOf baseClass)

base[0] = obj1; // no casting necessary

then when you pull them out of the array base just call the foo() and the foo() for each derived class will be called.

foreach( obj in base)

obj.foo()

this will call the foo() method for each derived class.

1

Inheritance

// this is pseudo code - isn

// base class references to derived-type objects.
BaseClass thing1 = new Derived1();
BaseClass thing2 = new Derived2();
BaseClass thing3 = new Derived3();

// assume the array elements must be of the same type
baseClassArray : [thing1, thing2, thing3];

for (int i-0; i< baseClassArray.Length; i++) {
    baseClassArray[i].Foo(); // base.Foo() is called.
}

for (int i=0; i< baseClassArray.Length; i++) {
    if (baseClassArray[i] is Derived1) {
        Derived1 thingy1 = (Derived1)baseClassArray[i];
        thingy1.Foo();   // the over-ride Foo() called
    }else if ....
}

Polymorphism

// this is pseudo code - ish

public abstract class AbstractPainter {
   public abstract void Style();
}

public class Dali : AbstractPainter {
    public override Style() {
        console.writeline("floppy clocks lying about");
}

public class Warhol : AbstractPainter {
    public override Style() {
        console.writeline("High contrast soup cans");
    }
}

// cannot instantiate abstract classes
// ie.  new AbstractPainter() is not allowed.

AbstractPainter Salvador = new Dali();
AbstractPainter Andy = new Warhol();

AbstractPainterArray = [Salvador, Andy];

for (int i=0; i< AbstractPainterArray.Length; i++) {
    AbstractPainterArray[i].Style();
}

output:
    "floppy clocks lying about"
    "High contrast soup cans"

Take Away?

Inheritance: Same-type references + derived objects, same method call.

polymorphism: Same-type references + derived objects, different method call.

0

The virtual method called always depends on type of concrete instance in memory and never on how this instance is accessed or type of variable the instance is accessed from. The type of variable just tells compiler what classes and it's subclasses can be saved and what virtual methods can be called. But what method is actually called is only known from memory at runtime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.