8

I can see that the term "upcast" is related to OOP, but I can't find the exact definition by searching the Internet.

Could anyone explain what does the term mean and in what situation this technique useful?

can.
  • 2,098
  • 8
  • 29
  • 42
  • I'm not sure, but I assume it's where you cast back to a parent class. For an example, go and read any article out there on polymorphism. – slugonamission Sep 18 '12 at 07:16
  • I can't say I actually know what the definition is off the top of my head, but I'd conjecture that @slugonamission is correct. – Wug Sep 18 '12 at 07:19

1 Answers1

10

From the description of the tag you posted:

Upcasting permits an object of a subclass type to be treated as an object of any superclass type.

Basically, it's where you cast a subclass instance to one of its superclasses, to show an example in pseudocode

class Base {
    function say_hi() { printf("Hello From Base Class\n"); }
}

class Person extends Base {
    function say_hi() { printf("Hello!"); }    // Overridden. Java expects an @Override annotation
}

class Dog extends Base {
    function say_hi() { printf("Woof!"); }    // Again, overridden
}

Base B = new Base();
Base P = new Person();   // Implicit upcast
Dog dog = new Dog();
Base D = (Base)Dog();    // Explicit upcast

B.say_hi(); // Hello from base class
P.say_hi(); // Hello!
D.say_hi(); // Woof!

There are a variety of times when this is useful. In general, it defines an interface of sorts, so you can subclass something, yet still use it in its original context. Say you have a game, you'd have an enemy object. This has some common functionality, like its current position, speed, health, and other things. Despite this, some enemies might move differently, might play a different die animation, and of course, would be drawn differently. The issue is, since they have the same interface, you don't want to have to have special code to handle every different type of enemy.

It would make sense to make a base "Enemy" class with these fields and empty methods, but then extend it to have SmallEnemy, EvilEnemy, BossEnemy etc with their different models and animations, filling in the blank methods. These "blank" methods can also be referred to as abstract or pure methods.

slugonamission
  • 9,562
  • 1
  • 34
  • 41
  • One question, since you have casted D to Base, how can you access Dog's `say_hi()`? I feel it hard to understand. Could you give me a concrete example instead of pseudocode one? – can. Sep 18 '12 at 07:44
  • 1
    I agree with you slugonamission but you should probably add a hint in the pseudo code that dog and person extend base. – Dan Sep 18 '12 at 08:26
  • @Dan - oops, thanks. That's what you get for coding in the morning. – slugonamission Sep 18 '12 at 09:01
  • @can. - sorry, I missed your question. In this case, you're calling the `say_hi` method on the current object. Because the D object inherits from Base, you can cast it back to Base, but it still retains the same methods, thus you can get away with doing this. – slugonamission Sep 18 '12 at 16:21
  • Also, this is implemented using a jump table. Imagine that your class has a table of methods with it. In the case of Dog, the table has the same format as Base, but I've changed an entry in it, hence when I cast to Base, I can still use the existing method table, even though the actual contents of one cell are different. – slugonamission Sep 18 '12 at 16:22