Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritence query??

 
Rohit chandra
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The program was working fine unless i entered System.out.println(people[0].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??
 
Saathvik Reddy
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you paste your error?
[ July 18, 2007: Message edited by: Sheethal Reddy ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no getSalary() in Person class. In order to call getSalary() you'll have to downcast to an Employee.

System.out.println(((Employee)people[0]).getSalary());
 
Rohit chandra
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@above
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.
 
Rohit chandra
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Error is:

javac PersonTest.java
PersoTest.java:19:cannot find symbol
symbol: method getSalary()
location: class Person
System.out.println(people[0].getSalary());
^
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rohit chandra:
@above
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic