-5
public class DeliverySystem {

     public static void main(String[] args) {
        Staff me = new partTimeStaff()
    me.CalcSalary();
    me.CalcBonus();
     }
}

class partTimeStaff extends Staff{
    public void CalcBonus(){..........}
}


class Staff{
        public void CalcSalary() {….........}
}

Why is there an error in this code and how do I solve this error ?

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • Casting is taking something of one class (or primitive type) and making it another. – jhobbie Jun 19 '14 at 16:20
  • Where is casting in your code? –  Jun 19 '14 at 16:22
  • 1
    the Title and the content of the Question are not related to each other. – Frakcool Jun 19 '14 at 16:23
  • possible duplicate of [What is the difference between a compile time type vs run time type for any object in Java?](http://stackoverflow.com/questions/14963943/what-is-the-difference-between-a-compile-time-type-vs-run-time-type-for-any-obje) – ajb Jun 19 '14 at 16:23
  • @Arkadiy There's no casting there, but he needs to use it to use `CalcBonus`, i.e. `((partTimeStaff)me).CalcBonus()`. – ajb Jun 19 '14 at 16:25
  • Where indeed is the error? And what is the error? You need to tell us the line and the error message you get. –  Jun 19 '14 at 16:25

3 Answers3

2

Your calcBonus() method is unavailable to a Staff object. Because you're using polymorphism to fit a partTimeStaff object into a Staff reference, the object can only call methods which are present in Staff.

I'll try to explain it in a simple example:

Suppose I'm a car mechanic. I know everything about cars. One of my clients brings me his car (a Ford Taurus) and asks to get it serviced. I know in general how to service cars, so that's no problem. But then he asks me to fix the radio in the car, which is specific to that particular make and model. I don't know how to do that. I don't know anything bout Ford Tauruses (Tauri?). I know what all cars do, but I don't know anything about specific types of cars.

asteri
  • 11,402
  • 13
  • 60
  • 84
2

Java is a statically typed language. this means that it has to know what methods and variables are available at compile time. When you do

Staff me = new partTimeStaff()

you're telling java that 'me' is a 'Staff' object (despite the fact that in reality, it is a partTimeStaff object). Therefore, the compiler only knows that 'me' can perform Staff functions (CalcSalary), not partTimeStaff functions (CalcBonus).

In order to get this to code to work, you'll either need to do:

partTimeStaff me = new partTimeStaff()

or

Staff me = new partTimeStaff()
me.CalcSalary();
((partTimeStaff) me).CalcBonus();

the later being an explicit cast (telling the compiler that me is actually a partTimeStaff object

clearlyspam23
  • 1,624
  • 13
  • 15
  • +1 Nice explanation. But to be fair, the downcast in your code solution is actually an example of *dynamic* typing in Java, which breaks the statement that Java is simply statically typed. There is some information preserved about the *actual* type of the object, which is clear with some simple methods like `getClass()`. – asteri Jun 19 '14 at 16:32
  • Sorry guys, I'm a bit dumb here. So how do I explain casting using that code ? – Madhoshi Jun 19 '14 at 16:32
  • 1
    @Madhoshi I'm not sure I follow your question, but in this code, the cast ((partTimeStaff) me) is saying that 'me' is actually a partTimeStaff object. This allows you to call a partTimeStaff method (indicated by the function call immediately following the cast). for more information, you might want to see [this](http://stackoverflow.com/questions/5289393/casting-variables-in-java). – clearlyspam23 Jun 19 '14 at 16:49
  • @Madhoshi The cast `((partTimeStaff) me)` is saying that `me` is actually a `partTimeStaff` object, as @clearlyspam23 says, but it also _checks_ to make sure. If `me` got reassigned to some `Staff` object that isn't a `partTimeStaff`, the cast will cause the program will throw an exception. – ajb Jun 19 '14 at 17:02
0

CalcBonus is not a method of Staff. The object me, although instantiated as an object of partTimeStaff, has the type Staff and hence can only access the Staff methods. It can access the partTimeStaff method if you add a cast as below: ((partTimeStaff) me).CalcBonus();

user3648939
  • 106
  • 2
  • 10