wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Polymorphism, arguments and instanceof  questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Polymorphism, arguments and instanceof  questions" Watch "Polymorphism, arguments and instanceof  questions" New topic
Author

Polymorphism, arguments and instanceof questions

Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Q.1 Here is the inheritance structure:

Any why the above code generate the error: java:26: non-static variable this cannot be referenced from a static context. Which one is non-static and which one is static content?
Q.2 Polymorphic arguments (same as above hierarchy)
In the Employee class:
public TaxRate findTaxRate(Employee e)
{
.....do calculation and return tax rate
}
Manager m = new Manager();
.
.
Taxrate t = findTaxRate(m); //What does this statement mean? to calculate Manager's tax rate?? So, a subclass's object can be passed to the parent class's function??
Is there any direction (upward or downward) among the hierarchy of classes for passing the object reference?
Q.3 instanceof
e.g.

So, for a downward direction, if Employee's object (parent class) wants to access to the Manager's attributes (subclass), it has to use instanceof and cast the Employee's object, e.g.
public void method(Employee e) { //Where is this method? in another class? or same class of Employee?

I don't undestand when should I use instanceof and casting object? Why should I use it as Employee e = new Manager(); or new Director(); (The subclass object can be instantiated along the hierarchy). or instanceof is used when the subclass is inherited HORIZONTALLY.

Thanks a lot
Andrew
(edited by Cindy to format code)

[This message has been edited by Cindy Glass (edited December 14, 2001).]
omar khan
Ranch Hand

Joined: Feb 01, 2001
Posts: 183
Q1:
Employee e = new Manager();
Manager m = new Employee();
in method main generates "non-static variable this cannot be referenced from a static contex" error since Employee and Manager are non static inner classes referenced in the main method which is static. In Java one cannot reference a non static field, method or inner class from a static method.
e.bonus = 1000;
in method main generates "cannot resolve symbol" error since the instantiation of Manager() failed.
[This message has been edited by OMAR KHAN (edited December 14, 2001).]
Colin Kenworthy
Ranch Hand

Joined: Aug 06, 2001
Posts: 88
"public class Manager extends Employee "
Here you state that the class Manager is a subclass of class Employee. Or, to put it another way, class Employee is the superclass of class Manager. It is perfectly legitimate to define a variable of one class and assign to it an object of that class OR a subclass of it.
e.g. Object o = new Employee(); (Object is the superclass of all classes)
e.g. Employee empl = new Manager();
As Manager is a subclass of Employee it inherits all of Employee's methods and attributes and then adds some of it's own.
Anything you can do to an Employee object you can do to a Manager object.
As far as the compiler is concerned whenever you reference variable empl you are referencing an Employee object. If you want to use the Manager's methods or attributes you need to cast the variable empl to class Manager.
e.g. empl.upBonus(); will not work - upBonus is not a method in class Employee
e.g. ((Manager)empl).upBonus(); compiler accepts this.
Any 'teach yourself..' book and the java tutorial http://java.sun.com/docs/books/tutorial/index.html
will teach you this. Don't try to run before you can walk!
Bill Krieger
Ranch Hand

Joined: Sep 27, 2001
Posts: 53
I think the main problem here is the inappropriate use of inner classes. Your Employee class, Manager class, etc., should not be inner classes. As inner classes they require an instance of a Test object to be instantiated. There is no relationship between an employee and a 'test.'
Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Thanks for all of your help.
I amended the code as follows and do some tests:
public class Test
{
public static void main (String [] args)
{
Employee e = new Manager();
e.name = "XYZ"; //ok
//e.bonus = 1000; //not ok
Employee f = new Employee();
Manager d = (Manager)f;
d.bonus = 1000;
Manager c = (Manager)e;
c.bonus = 2000;
//Manager n = new Employee(); //not ok
Manager m = new Manager();
m.name = "Andrew Parker"; //ok
Employee k = new Director();
}
}
class Employee {
String name;
String address;
String salary;
public void upSalary(){}
}
class Manager extends Employee {
int bonus;
public void upBonus(){}
}
class Director extends Manager {
int allowance;
public void upAllowance(){}
}
Regards
Andrew
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Polymorphism, arguments and instanceof questions