Results 1 to 6 of 6
Thread: Inheritance
- 02-20-2011, 03:46 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
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
- 02-20-2011, 03:48 PM #2
Member
- Join Date
- Feb 2011
- Posts
- 30
- Rep Power
- 0
1. Use code tags.
2. You cannot call "super" multiple times in constructor.
3. What is compiler output?
- 02-20-2011, 03:55 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
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
- 02-20-2011, 04:16 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 30
- Rep Power
- 0
There is no such contructor method.Java Code:Practical1.java:113: cannot find symbol symbol : constructor Employee(java.lang.String) location: class Employee super(first);
What thing you don't understand? Everything is written clearly in compiler's output.Java Code:Practical1.java:114: call to super must be first statement in constructor super(last);
- 02-20-2011, 05:40 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
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 ManagerLast edited by new_2_j; 02-20-2011 at 05:43 PM.
- 02-20-2011, 05:56 PM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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
-
JPA Inheritance
By videanuadrian in forum New To JavaReplies: 1Last Post: 01-10-2011, 06:44 AM -
Inheritance
By terahawks in forum New To JavaReplies: 1Last Post: 04-23-2010, 09:58 AM -
Inheritance
By Nerijus in forum New To JavaReplies: 5Last Post: 04-20-2010, 03:47 AM -
Inheritance
By mew in forum New To JavaReplies: 1Last Post: 12-07-2007, 06:08 PM -
Inheritance in GUI
By Marty in forum SWT / JFaceReplies: 2Last Post: 05-11-2007, 12:54 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks