Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Inheritence query?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritence query??" Watch "Inheritence query??" New topic
Author

Inheritence query??

Rohit chandra
Greenhorn

Joined: Jun 23, 2007
Posts: 15


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

Joined: Jun 03, 2005
Posts: 228
Could you paste your error?
[ July 18, 2007: Message edited by: Sheethal Reddy ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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());


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Rohit chandra
Greenhorn

Joined: Jun 23, 2007
Posts: 15
@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

Joined: Jun 23, 2007
Posts: 15
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

Joined: Jan 17, 2006
Posts: 1296
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inheritence query??
 
Similar Threads
Printing Method (inheritance and super method)
Question regarding Cloneable interface and Subclasses
how to make two classes in a packahe where one class extends the other
Is it me or just Eclipse... JEdit too...
Serialization thingymabob???