Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default Inheritance - 3rd Generation?

    My super class is called "Employee.java". The name of a class that inherits from it is called "ProductionWorker.java", so the class declaration would be:

    Java Code:
    public class ProductionWorker extends Employee{
    But then I have a class called "ProductionWorkerDemo.java" that I want to inherit from both of those:

    Java Code:
    package employment;
    import java.util.Scanner;
    
    public class ProductionWorkerDemo extends Employee{
        
        public int Shift;//As defined in the "private double getPayRate" method of 
        //the ProductionWorker class, payRate = 8.00 when Shift is 1, and 
        //payRate = 10.00 when Shift is 2.  
        
        double payRate;//Remember, for this project, we want payRate to be 
        //invisible.
        
        public ProductionWorkerDemo(String name, String aHireDate, int empNum, 
                                int shift, double pay)
        {       
            super(name, aHireDate, empNum);
            Shift = shift;
            payRate = pay;        
        }
    Using the "extends Employee" enables the class to recognize the 3 super class variables in the constructor, but that does NOT enable it to recognize the "Shift" and "payRate" variables, which are both from the "ProductionWorker.java" class. Now what?:

    public class ProductionWorkerDemo extends Employee && ProductionWorker{

    public class ProductionWorkerDemo extends Employee, ProductionWorker{

    What is the correct syntax for this? Is it smart enough to know that the "Shift" and "payRate" variables were originally declared and defined in the "ProductionWorker.java" class?
    Last edited by SamJava_the_Hut; 10-12-2013 at 09:44 PM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Inheritance - 3rd Generation?

    Java does not support multiple inheritance but if your ProductionWorkerDemo class extends ProductionWorker it will inherit from Employee through ProductionWorker. However I smell something rotten in your design. Is ProductionWorkerDemo a special kind of ProductionWorker? The "Demo" on the end suggests it is some sort of simulation class that is the main driver class and therefore inheritance is not the correct way to go. Do not abuse inheritance just so one class can more easily access stuff in another class.

  3. #3
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default Re: Inheritance - 3rd Generation?

    Right now, my only goal is to learn how the mechanics of inheritance works. I'm not trying to abuse inheritance. Basically, I'm defining most of the fields and methods in Employee and ProductionWorker, and then testing them out in ProductionWorkerDemo, which is the only class with a main method in my package right now.

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Inheritance - 3rd Generation?

    Hi,

    For inheritance you can have many level as you want. For example A <- B -< C -< D <- E. This means that class E will can be casted in type A, B, C and D.

    In you ProductionWorkerDemo class if you want it to have the behavior of Employee and ProductionWorker then you need to extends the ProductionWorker instead of Employee. Because you have made Employee <- ProductionWorker <- ProductionWorkerDemo. I this case what you mean is that ProductionWorkerDemo is a specialization of ProductionWorker.

  5. #5
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default Re: Inheritance - 3rd Generation?

    Quote Originally Posted by wsaryada View Post
    For inheritance you can have many level as you want. For example A <- B -< C -< D <- E. This means that class E will can be casted in type A, B, C and D.
    This is called "Class Chaining", right?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default Re: Inheritance - 3rd Generation?

    Quote Originally Posted by SamJava_the_Hut View Post
    This is called "Class Chaining", right?
    I don't think I've ever heard that phrase.
    It's just plain old inheritance.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. payroll generation
    By kriti in forum New To Java
    Replies: 5
    Last Post: 06-02-2010, 03:21 PM
  2. Validate XML against XSD during generation
    By mandarkatdare in forum XML
    Replies: 0
    Last Post: 05-29-2010, 10:57 AM
  3. War file generation
    By rummy in forum New To Java
    Replies: 1
    Last Post: 02-08-2010, 12:57 PM
  4. Report generation
    By anilkumar_vist in forum Advanced Java
    Replies: 1
    Last Post: 12-14-2009, 12:26 PM
  5. random generation
    By carlos123 in forum New To Java
    Replies: 10
    Last Post: 01-09-2008, 03:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •