2 Replies - 881 Views - Last Post: 09 February 2013 - 04:43 AM Rate Topic: -----

#1 Trashed675  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-May 12

Implicit super constructor ** is undefined. Must explicitly invoke...

Posted 08 February 2013 - 09:21 PM

Implicit super constructor Worker() is undefined. Must explicitly invoke another constructor

Here is the code that i'm getting this message on. Yes it is for a school assignment. There are 3 classes, Worker, Producer, and Manager. I'm stuck with this error on both of the sub classes(Producer and Manager).

//Worker class


public class Worker{

	String n;
	static long uW;
	
	public static void main(String[] args)
	{
		//producer producerObject = new Producer();
	}
	
	public Worker(String name, long unitsWorked)
	{
		n = name;
		uW = unitsWorked;
	}
	
	public long work(long units)
	{
		//long unitsWorked = 0;
		long worked = 0;
		if (units>=1)
		{
			uW = (units + worked);
			return uW;
		}
		else 
			return uW;
	}
	
	
	public long getUnitsWorked()
	{
		//double unitsWorked;
		return uW;
		//System.out.println(uW);
	}
	
	public double pay(double multiplier)
	{
		double mult = multiplier;
		double paid = mult*uW;
		uW = 0;
		return paid;
	}
	
	public String toString()
	{
		String s = super.toString();
		return "name: " + n + "units worked: " + uW;
	}
}



//Producer class


public class Producer extends Worker{
	
	private int proTar;
	long uW = Worker.uW;
	String n;
	
	public static void main(String[]) args)
	{
		
	}
	
	public Producer (String name, int productionTarget)
	{
		proTar = productionTarget;
		n = name;
	}
	
	public long work(long units)///////////////////////doesn't quite make sense at the moment, need to go over directions, probably for all 3 classes
	{
		//long unitsWorked = 0;
		long worked = 0;
		if (units>=1)
		{
			uW = (long) (units * .5 * proTar);
			return uW;
		}
		else 
			return uW;
	}
	
	public int getProductionTarget()
	{
		//double productionTarget; 
		return proTar;
	}
	
	public double pay(double multiplier)
	{
		double mult = multiplier;
		double paid = mult*uW;
		Worker.uW = 0;
		return paid;
	}
	
	public String toString()
	{
		String s = super.toString();
		return super.toString() + " name: " + n + ", Units Worked: " + uW + "," +
		" Production Target: " + proTar ;
	}

}



//Manager Class


public class Manager extends Producer{
	
	
	int nS;
	int units = 0;
	boolean wl;
	long uW = Worker.uW;
	String n;
	
	
	
	public Manager(String name, int numberSubordinates, boolean wellLiked)
	{
		nS = numberSubordinates;
		n = name;
		wl = wellLiked;
	}
	
	public long work(long units)//if the parameter recieved is less then or equal to 0 return nothing
	{
		//long unitsWorked = 0;
		long worked = 0;
		if (units>=1)
		{
			uW = (long) (units * .75 * nS );
			return uW;
		}
		else 
			return uW;
	}
	
	public double pay(double multiplier)
	{
		double mult = multiplier;
		double paid = mult*Worker.uW;
		Worker.uW = 0;
		return paid;
	}

	public String toString()
	{
		String s = super.toString();
		return super.toString() + " name: " + n + ", unitsEarned: " + uW + "," +
				" numberSubordinates: " + nS + ", wellLiked: " + wl;
	}
}



The manager has to extend the producer which extends worker. I'm guessing i'm going about it in a less then perfect sort of way.

Is This A Good Question/Topic? 0
  • +

Replies To: Implicit super constructor ** is undefined. Must explicitly invoke...

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,686
  • Joined: 13-March 10

Re: Implicit super constructor ** is undefined. Must explicitly invoke...

Posted 09 February 2013 - 03:37 AM

Your base class is Worker. As far as the constructor is concerned, it seems fine - you provide the worker with name and unitsWorked.

Then, Producer extends Worker which is fine since a Producer IS A Worker. But, let's focus on the constructor now.

Imagine that the Worker class is a circle with 2 empty boxes inside of it. -- the boxes represent name and unitsWorked.
Now, imagine the Producer class is a larger cicrle around the Worker circle. The Producer circle has 1 empty box --- it represents productionTarget.

So to summarize the above scenario. You have 2 circles and 3 empty boxes. Worker circle has 2 of them and Producer circle contains Worker circle and 3rd box.

Now, you want to create Producer object. How do you do it?

Well, if you were to create Worker object, it's easy: -- it has 2 empty boxes so you do:

Worker worker = new Worker("name of worker", 100);



Let's go back to Producer circle -- it has a Worker circle + 1 empty box, so you need to create the inner circle + fill 1 box.

In order to accomplish that, you need to change your constructor:

class Producer extends Worker
{
     private int productionTarget;   
     public Producer(String name, long unitsWorked, int productionTarget){
             //WE NEED TO CREATE WORKER CIRCLE
             super(name, unitsWorked);
             //WE NEED TO FILL IN THE PRODUCER BOX
             this.productionTarget = productionTarget;
   
     }

}




Now, you can simply call the constructor:
Producer producer = new Producer("name of producer", 100, 200);


Was This Post Helpful? 1
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Implicit super constructor ** is undefined. Must explicitly invoke...

Posted 09 February 2013 - 04:43 AM

Sorry, darek9576, I got lost in your explanation.

@OP: The first thing the constructor of an extended class does is call the default constructor of the parent. In the case of Producer which extends Worker, there's an automatic call added by the compiler to the existing Producer constructors, so that what you have really looks like:
public Producer (String name, int productionTarget)
{
    super();
    proTar = productionTarget;
    n = name;
}

The super() statement is another way of calling the default parent constructor, Worker(). In fact, if you add the super() statement yourself as I've done above, the error message will change slightly and become more clear, because there is no such constructor. The compiler complains and tells you in the error message to explicitly call another constructor.

Another approach to eliminate the error is to simply create a default constructor in the parent class:
// default constructor
public Worker()
{
    
}

It doesn't even have to do anything, but there are probably some useful things you could do with it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1