• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

cannot understand the point of downcasting in the following code

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how are you?
wanted to ask you why this code is using Downcasting (1st code fragment line 36-37)? i.e.
in the line below

should not the compiler dynamically 'find out' that it is dealing with a subclass of CurrentEmployee
and use the appropriate functions in that sub-class (BasePlusCommissionEmployee)?
thank you
=================

==============
BasePlusCommissionEmployee
==============

============
CommissionEmployee
=============

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You usually do it to call methods that a class has, but that its interface/super class has not. Methods getBaseSalary and setBaseSalary are called. Are these methods also in Employee ?
 
David Schwartz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christophe Verré wrote:You usually do it to call methods that a class has, but that its interface/super class has not. Methods getBaseSalary and setBaseSalary are called. Are these methods also in Employee ?



yes, i believe they are in the Employee class
 
Marshal
Posts: 79263
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that example from Deitel and Deitel? It looks familiar.

I agree that if you are not using any specific Commission methods, the cast is unnecessary.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But it's inside an instanceof, so there must be a reason, no? And if there isn't, wow.
 
David Schwartz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes it is from Deitel and Deitel book (5th edition)
 
Campbell Ritchie
Marshal
Posts: 79263
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Get a newer edition than 5th. Get 6th or newer, which describe the new features in Java 5.

I had a quick look through my 6th edition Deitel and can't see that cast. Which example number is it in your edition?
 
reply
    Bookmark Topic Watch Topic
  • New Topic