The Artima Developer Community
Sponsored Link

Java Answers Forum
Polymorphism & inheritance

6 replies on 1 page. Most recent reply: Dec 4, 2002 4:42 PM by Michael Will

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Silvio

Posts: 3
Nickname: sillyshark
Registered: May, 2002

Polymorphism & inheritance Posted: May 23, 2002 8:41 PM
Reply to this message Reply
Advertisement
I was wondering if somebody could help me out. I am a rookie in regards to Java programming. My problem is the following: I am trying to write a superclass with 2 subclasses. Worker is the superclass and HourlyWorker and SalaryWorker are the subclasses. Basically the program is to compute the weekly pay for any worker either salary or hourly. Can somebody sugguest how I should write this program. My initial attempts have given me grief. Here is a sample of what I have so far:
public class HourlyWorker extends Worker
{  public HourlyWorker(double rate)
   { hrate = rate;
   }
   
   /**
      Method to calculate the weekly pay for an hourly employee.
   */
         
   public static int computePay(int hours)
   {  double Weekly_Pay = 0;
      if (hours > 40)
      {  double Time = hours - 40;
         Weekly_Pay = (40 * getRate) + (Time * 1.5 * getRate);
      }   
      else
      {  Weekly_Pay = hours * getRate;
      }
         
      return Weekly_Pay;
          
   }
   private double hrate;
   private double getRate;
   
   
   
}  
 
 
 
public class SalariedWorker extends Worker
{  public SalariedWorker(double rate)
   {  salaryrate = rate;
   }
   
   public static int computePay(int hours)
   {  double Weekly_Pay = 0;
      Weekly_Pay = salaryrate * 40;
      return Weekly_Pay;
   }
   private double salaryrate;
   
} 
 
  
public class Worker
{  public Worker()
   {  name = "";
      rate = 0;
      empltype = "";
   }
   
   /**
      Constructs a Worker with a given name and salary 
      rate.
      @param aName is the worker name
      @param srate is the given rate
      @param type is the type of employee
   */
   
   public Worker(String wName,double wrate, String wtype)
   {  name = wName;
      rate = wrate;
      empltype = wtype;
      
   }
         
   private String name;
   private double rate;
   private String empltype;
}


I would really appreciate any help anybody could give me.

Thank you in advance


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Polymorphism & inheritance Posted: May 24, 2002 5:16 PM
Reply to this message Reply
Silvio,
The problem you describe is typically a problem solved by the Visitor Pattern. You may find an example here (or there) : http://www.javacoder.net/patterns.jsp
It's the very last one @ the very bottom !

Please give more details on what you want an explaination on ...

Rgds,

Thomas SMETS,
SCJP2 - Brussels

Silvio

Posts: 3
Nickname: sillyshark
Registered: May, 2002

Re: Polymorphism & inheritance Posted: May 24, 2002 8:26 PM
Reply to this message Reply
Thank you for responding to my request. I do not understand how to write a subclass so that information is used from its superclass. If you try to compile the code that I provided I keep getting errors that do not make sense to me. I think I am missing something.

Silvio

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Polymorphism & inheritance Posted: May 25, 2002 8:01 AM
Reply to this message Reply
Silvio,
I got the code on my PC !
The first thing I would like to ask you is :
Do you know the difference between a Class, an Object & an instance ?
The reason is the following :
Compiling you code you got the following error :
<snip>
--------------------Configuration: test - 1.2.2 <Default>--------------------
Command : "C:\jdk\1.2.2\bin\javac.exe" -g -deprecation -classpath "C:\cygwin\data\classes\test\;C:\jdk\libs\junit\junitperf.jar;C:\jdk\libs\junit \junit.jar;C:\jdk\1.2.2\jre\lib\rt.jar;C:\jdk\1.2.2\jre\lib\i18n.jar;C:\jdk\1.2. 2\lib\dt.jar;C:\jdk\1.2.2\lib\tools.jar;C:\jdk\1.2.2\jre\lib\ext\iiimp.jar" -d "C:\cygwin\data\classes\test" @src_test.txt
Directory : C:\cygwin\data\test
C:\cygwin\data\test\design_pattern\help\SalariedWorker.java :16: Can't make a static reference to nonstatic variable salaryrate in class test.design_patterns.help.SalariedWorker.
Weekly_Pay = salaryrate * 40;
^
1 error

Process completed.
</snip>
This means the code mixes static variables & "member" variables...
Hope this helps for you to find the answer with the hints.

Rgds,

Thomas SMETS,
SCJP2 - Brussels

Silvio

Posts: 3
Nickname: sillyshark
Registered: May, 2002

Re: Polymorphism & inheritance Posted: Jun 2, 2002 8:27 PM
Reply to this message Reply
Thanks again Thomas SMETS.
I have reviewed my program using the hints you have provided me and I cannot get passed the same error you had when you compiled the code "Can't make static reference to nonstatic variable". In your response previously you mentioned "member" variables. What are "member" variables? Do you know of any Web page(s) or link(s) that I could go to and see an example or examples of a program that uses inheritance and polymorphism? I really appreciate your help.

Thanks again

Silvio

Thang Nguyen

Posts: 18
Nickname: thang
Registered: Apr, 2002

Re: Polymorphism & inheritance Posted: Jun 3, 2002 9:31 AM
Reply to this message Reply
I guess your problem is the "static" key word in the method headers you used in the class.

All the variables in the member class should be nonstatic variables, meaning they are object dependent.
And static variables are object independent. So if you use nonstatic variable in HourlyWorker class, which is a member of WORKER class, the variables should be non-static, otherwise it keeps generating the same error message.

Michael Will

Posts: 3
Nickname: zmichael
Registered: Dec, 2002

Re: Polymorphism & inheritance Posted: Dec 4, 2002 4:42 PM
Reply to this message Reply
> I guess your problem is the "static" key word in the
> method headers you used in the class.

> All the variables in the member class should be nonstatic
> variables, meaning they are object dependent.

> And static variables are object independent. So if you
> use nonstatic variable in HourlyWorker class, which is a
> member of WORKER class, the variables should be
> non-static, otherwise it keeps generating the same error
> message.

Exactly. He has a static method in Worker, which is ok, but it cannot access nonstatic variables (getrate) then. That is the bug. So either there is only one "getrate" value for all instances, then make it static too, or there are different ones for different instances of workers, then make the method nonstatic.

Michael.

Flat View: This topic has 6 replies on 1 page
Topic: try/catch help Previous Topic   Next Topic Topic: help with a statement

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use