Trouble with Abstract Class/Method

Unable to overide abstract method

Page 1 of 1

3 Replies - 4037 Views - Last Post: 07 September 2010 - 03:32 PM Rate Topic: -----

#1 Murderality  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 40
  • Joined: 14-October 09

Trouble with Abstract Class/Method

Posted 07 September 2010 - 10:02 AM

Hi there, I'm trying to override the abstract calcPay method in the Employee class, with another calcPay method found in the ProductionWorker class. However, when I compile I get the following error:

java:1: cannot find symbol
symbol : constructor Employee()
location: class Employee
public class ProductionWorker extends Employee

Here's my code for each class.

public abstract class Employee
{

	//Class fields
	private String employeeName;
	private int employeeId;
	
//Two-arg constructor
public Employee(String name, int number)
{
	this.employeeName = name;
	this.employeeId = number;
}

public void setEmployeeName(String str)
{
	this.employeeName = str;
}

public void setEmployeeId(int num)
{
	this.employeeId = num;
}

public String getEmployeeName()
{
	return employeeName;
}

public int getEmployeeId()
{
	return employeeId;
}

public abstract void calcPay();


public String toString()
{
	String str = "Employee ID: 9999 Name: xxxxxxxxxxxxxxxxxxxxxxxxxx";
	return str;
}

}




Subclass

public class ProductionWorker extends Employee
{

	//calcPay overides the abstract calcPay found in the Employee class.
	public void calcPay()
	{
	}
	
}


Any hints would be more than appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with Abstract Class/Method

#2 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Trouble with Abstract Class/Method

Posted 07 September 2010 - 10:09 AM

There is not an implicit constructor Employee() in the abstract class so you will need either create this constructor or override the super class constructor:
public class ProductionWorker extends Employee
 {

 	public ProductionWorker(String name, int number) {
		super(name, number);
		
	}

	//calcPay overides the abstract calcPay found in the Employee class.
 	public void calcPay()
 	{
 	}
 	
 }


This post has been edited by guidojavier: 07 September 2010 - 10:10 AM

Was This Post Helpful? 1
  • +
  • -

#3 Murderality  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 40
  • Joined: 14-October 09

Re: Trouble with Abstract Class/Method

Posted 07 September 2010 - 03:08 PM

View Postguidojavier, on 07 September 2010 - 09:09 AM, said:

There is not an implicit constructor Employee() in the abstract class so you will need either create this constructor or override the super class constructor:
public class ProductionWorker extends Employee
 {

 	public ProductionWorker(String name, int number) {
		super(name, number);
		
	}

	//calcPay overides the abstract calcPay found in the Employee class.
 	public void calcPay()
 	{
 	}
 	
 }



It works fine now, thanks for the help. I didn't know that constructors are not inherited.
Was This Post Helpful? 0
  • +
  • -

#4 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Trouble with Abstract Class/Method

Posted 07 September 2010 - 03:32 PM

If you do not define the any constructors Java will generate a default no-argument constructor for you. But if you add some other constructor Java will not generate this default constructor. So you have to write it out yourself. If you do not define a constructor in your subclass then when you create an object of it , Java just invoke the corresponding default constructor and at the same time invoke the default constructor of its super class and, as you have defined other constructor rather than the default one then you get the error.

This post has been edited by guidojavier: 07 September 2010 - 03:34 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1