Results 1 to 6 of 6

Thread: Inheritance

  1. #1
    new_2_j is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default Inheritance

    Hi all,

    The code below is for an Employee class, however i want to add a Manager class which extends Employee class but the addition of the code:

    class Manager extends Employee{
    public Manager(String first, String last, double employee){
    super(first);
    super(last);
    super(employee);
    }
    }

    prevents the program from compiling.


    Here is the full program:

    import java.util. *;

    /*---------------------------------------------------------------
    class: Employee
    Constructor: Employee(String first, String last, double employee)
    Accessor: getFirstName()
    getLastName()
    getEmployeeNumber()
    Mutator: setFirstName(String firstName)
    setLastName(String lastName)
    setEmployeeNumber(double employeeNumber)
    Methods: remove()
    printEmployeeDetails()
    getCount()
    ---------------------------------------------------------------*/
    class Employee
    {

    // Private fields
    private String firstName;
    private String lastName;
    private double employeeNumber;
    private static int count;


    // Constructor method
    public Employee(String first, String last, double employee)
    {
    this.firstName = first;
    this.lastName = last;
    this.employeeNumber = employee;
    ++count;
    }


    // Accessor for first name
    public String getFirstName()
    {
    return firstName;
    }

    // Accessor for last name
    public String getLastName()
    {
    return lastName;
    }

    // Accessor for employee number
    public double getEmployeeNumber()
    {
    return employeeNumber;
    }


    // Mutator for first name
    public void setFirstName(String firstName)
    {
    this.firstName = firstName;
    }

    public void setLastName(String lastName)
    {
    this.lastName = lastName;
    }

    public void setEmployeeNumber(double employeeNumber)
    {
    this.employeeNumber = employeeNumber;
    }


    // Method to remove employee
    public void remove()
    {
    --count; // remove employee
    }


    // Method to print employee details
    public void printEmployeeDetails()
    {
    System.out.println(this.firstName);
    System.out.println(this.lastName);
    System.out.println(this.employeeNumber);
    System.out.println(" ");
    }


    // Get number of employees
    public void getCount()
    {
    System.out.println("Count: " + count);
    }

    }// end Employee class




    //-----------------------------------------------
    class Manager extends Employee{
    public Manager(String first, String last, double employee){
    super(first);
    super(last);
    super(employee);
    }
    }


    //------------------------------------------------------
    public class Practical1{

    public static void main (String[] args)
    {
    // Test program
    Employee emp1 = new Employee("Jenny", "Jones", 123456);
    Employee emp2 = new Employee("Mark", "McGonagle", 234567);
    Employee emp3 = new Employee("Paula", "Peters", 345678);
    Employee emp4 = new Employee("Donalod", "Denny", 456789);

    emp4.getCount();
    emp1.getCount();
    emp1.remove();
    emp2.getCount();
    System.out.println("First name : " + emp1.getFirstName() );
    } // end main
    }//end practical1

  2. #2
    codesmuggler is offline Member
    Join Date
    Feb 2011
    Posts
    30
    Rep Power
    0

    Default

    1. Use code tags.
    2. You cannot call "super" multiple times in constructor.
    3. What is compiler output?

  3. #3
    new_2_j is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    Compiler output is as below:

    Practical1.java:113: cannot find symbol
    symbol : constructor Employee(java.lang.String)
    location: class Employee
    super(first);
    ^
    Practical1.java:114: call to super must be first statement in constructor
    super(last);
    ^
    Practical1.java:115: call to super must be first statement in constructor
    super(employee);
    ^
    3 errors

  4. #4
    codesmuggler is offline Member
    Join Date
    Feb 2011
    Posts
    30
    Rep Power
    0

    Default

    Java Code:
    Practical1.java:113: cannot find symbol
    symbol : constructor Employee(java.lang.String)
    location: class Employee
    super(first);
    There is no such contructor method.
    Java Code:
    Practical1.java:114: call to super must be first statement in constructor
    super(last);
    What thing you don't understand? Everything is written clearly in compiler's output.

  5. #5
    new_2_j is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    I don't understand how to initialise the three attributes of the Employee class from within the Manager class constructor.

    The following code compiles, so could you show how me, perhaps with some code, how to extend this for constructors with variable length arguments.

    Sorry but i am just learning Java and the concepts of OOP coming from C background.

    class Employee{

    private String firstName;


    // Constructor method
    public Employee(String first)
    {
    this.firstName = first;
    }


    }//end Employee


    class Manager extends MyBase{
    public Manager(String first){
    super(first);
    }
    }//end Manager
    Last edited by new_2_j; 02-20-2011 at 05:43 PM.

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    If a c function expected 3 arguements most likely you would pass that function 3 arguements. Well Employee's constructor expects 3 arguements so you must pass super 3 arguements; 2 Strings and a double inside Manager Constructor

    Java Code:
    class Manager extends MyBase{
        public Manager(String first,String last, double num){
            super(first,last,num);
        }
    }//end Manager

Similar Threads

  1. JPA Inheritance
    By videanuadrian in forum New To Java
    Replies: 1
    Last Post: 01-10-2011, 06:44 AM
  2. Inheritance
    By terahawks in forum New To Java
    Replies: 1
    Last Post: 04-23-2010, 09:58 AM
  3. Inheritance
    By Nerijus in forum New To Java
    Replies: 5
    Last Post: 04-20-2010, 03:47 AM
  4. Inheritance
    By mew in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 06:08 PM
  5. Inheritance in GUI
    By Marty in forum SWT / JFace
    Replies: 2
    Last Post: 05-11-2007, 12:54 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
  •