The program was working fine unless i entered System.out.println(people.getSalary());
I have no idea why the compiler is flagging this as error??
1. The object reference has an access to the getDescription() method of class Employee byt whenever i try to access getSalary() method on the same object reference it flags an error??
If object variable of Person can access getDescription() method in Employee class then why not getSalary() method.
Because getDescription() is declared in Person class, getSalary() is not. If you are using a reference of type Person, you may only call methods visible in Person class (and any class it inherits from) unless you cast the reference to a more specific type.
[ July 18, 2007: Message edited by: Garrett Rowe ]
Originally posted by Rohit chandra:
Person is an abstract class, however the object variable of Person is pointing to Employee object. If object variable of Person can access getDescription() method in Employee class then why not getSalary() method.
Because getDescription() is defined for Person, whereas getSalary() is not. The fact that Person.getDescription() is abstract doesn't matter; the compiler knows that any object of type Person has this method.
If you use a cast, as suggested above, then you're telling the compiler that the Person is really an Employee, so then it can find the getSalary() method.