• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Polymorphism, arguments and instanceof questions

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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!
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic