• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cannot understand the point of downcasting in the following code

 
David Schwartz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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
=============

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • 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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
yes it is from Deitel and Deitel book (5th edition)
 
Campbell Ritchie
Sheriff
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic