Help-Accessing Constructors
Hey Guys,
I am having trouble accesing a contructor from a parent class. It isprotected with two String parameters(abstract class, w/o default constructor). The Child class calls a seperate method from the Parent, trying to access the String parameters (protected as well).
I am creating a UseChild class which creates a object and tests it.
The Error i am getting is the Child class cannot access the constructor from the Parent.
I can post code, if needed.
Thanks for your help!
> I can post code, if needed.Yes.When you post code, please use[code] and [/code] tags as described in [url= http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.
jverda at 2007-7-14 23:46:52 >

package EmployeePackage;
public abstract class Employee
{
//PARENT CLASS
protected String name="";
protected String department="";
protected Employee(String name, String department)
{
//super(name, department);
this.name=name;
this.department=department;
}
public abstract String displayData();
}
public class Manager extends EmployeePackage.Employee implements EmployeePackage.WeeklyPay
{
//CHILD CLASS
public double calculatePay()
{
return MANAGER_PAY;
}
public String displayData()
{
return "Name is: "+ name + "\nDepartment is: " + department + "\nPay is: " +calculatePay();
}
}
I havent setup the calculatePay() method properly because I have been stuck on this problem.
Employe doesn't have a no-arg constructor public Employee() {...}
but Manager does and its c'tor calls Employee's (nonexistent) one:public Manager() {
super();
}
Constructor rules:
1) Every class has at least one ctor.
1.1) If you do not define an explicit constructor for your class, the compiler provides a implicit constructor that takes no args and simply calls super().
1.2) If you do define one or more explicit constructors, regardless of whether they take args, then the compiler no longer provides the implicit no-arg ctor. In this case, you must explicitly define a public MyClass() {...}
if you want one.
1.3) Constructors are not inherited.
2) The first statement in the body of any ctor is either a call to a superclass ctor super(...)
or a call to another ctor of this class this(...)
2.1) If you do not explicitly put a call to super(...) or this(...) as the first statement in a ctor that you define, then the compiler implicitly inserts a call to super's no-arg ctor super()
as the first call. The implicitly called ctor is always super's no-arg ctor, regardless of whether the currently running ctor takes args.
2.2) There is always exactly one call to either super(...) or this(...) in each constructor, and it is always the first call. You can't put in more than one, and if you put one in, the compiler's implicitly provided one is removed.
jverda at 2007-7-14 23:46:52 >

So in the Child class Manager, I must use Super to access the Employee constructor?Sorry for my newbness, I am learning :-P
> So in the Child class Manager, I must use
> Super to access the Employee constructor?
You don't have to explicitly call a super(...) c'tor, but if you don't, then it will implicitly call super(), the no-arg c'tor. If you go that route, then the parent class has to have such a c'tor.
Now, whether you add that c'tor to the parent class, or put an expicit call to super(...) into the child class, depends on the semantics of your parent class. Does it make sense to have a no-arg c'tor? Can you give the fields meaningful, valid default values? Or does that class require name and department to be set in order to use it?
Also, I see you're initializing name and dept to "". If it makes sense for an object to exist with those fields set to empty strings, then that's fine, but if not, then don't bother setting them.
jverda at 2007-7-14 23:46:52 >

> Sorry for my newbness, I am learning :-PNothing wrong with that. Everybody's a newbie at some point.
jverda at 2007-7-14 23:46:52 >

Well Employee isnt allowed to have a no-arg constructor.The name and the department will be changed, on the UseChild class with command line args.
> Well Employee isnt allowed to have a no-arg
> constructor.
Then Manage has to call super(name, department), so probably Manager should have a (name, department) c'tor also.
> The name and the department will be changed, on the
> UseChild class with command line args.
That doesn't matter. If Emlployee doesn't have a no-arg c'tor, then there's no resaon to set those initial values. It's cluttersome and misleading. Get rid of those assignments.
jverda at 2007-7-14 23:46:52 >

> Then Manage has to call super(name, department), so
> probably Manager should have a (name, department)
> c'tor also.
I was given the docs and told to create the project. According to the docs, manager was not given a constructor but the useChild class was(no parameter). That is where i get confused.
I don't know what you mean by these classes "being given a c'tor" or not.
All I can tell you is that if Employee cannot have a no-arg c'tor, and requires name and dept. (which is a better design), then Manager should be the same way.
I have no idea what useChild is or how it fits in here.
jverda at 2007-7-14 23:46:52 >

If UseChild is a driver class for testing, then whether it has a no-arg c'tor or not has nothing to do with whether Emp and Mgr do. Emp's and Mgr's c'tors are related because Mgr is a child class of Emp, and hence must always call one of Emp's c'tors as part of its own construction process. This is not the case for UseChild.
By the way, UseChild is a rather bad class name. Class names should be nouns, such as ManagerTester.
jverda at 2007-7-14 23:46:52 >

The UseChild is just creating a object and priting name, department ect.
I looked at the Doc's (guildlines) and Manager did not have a constructor in the documentation. If that makes any sense, sorry for me being confusing and thanks for all your help i really appreciate it.
UseChild, is called UseManager. Sorry, I was just trying to simplify it.
> I looked at the Doc's (guildlines) and Manager did
> not have a constructor in the documentation. If that
> makes any sense, sorry for me being confusing and
> thanks for all your help i really appreciate it.
Hmmm... No that doesn't make much sense.
Did somebody else create Manager? Or are these docs that your instructor provided that you're supposed to use as guidelines for how to create manager?
It's not required for Manager to have a c'tor that takes name and dept., but if it doesn't, then its no-arg c'tor has to pass dummy or default values so Employee' c'tor:
public class Manager extends Employee {
public Manager() {
super(null, null);
}
}
And then you need to set those fields later. You can do that, but it's generally bad form. It would be better to have Manager's c'tor also take name and dept. But I don't know what your instructor is requiring, or what he's trying to teach. You might want to ask him for clarification.
>
> UseChild, is called UseManager. Sorry, I was just
> trying to simplify it.
jverda at 2007-7-14 23:46:52 >

> UseChild, is called UseManager. Sorry, I was just
> trying to simplify it.
UseChild, UseManager, either way, the class name is a verb phrase. Usually method names are verbs or verb phrases and class names are nouns or noun phrases, such as ManagerTester or ManagerUser or something.
jverda at 2007-7-14 23:46:52 >

code]Manager one= new Manager();one.name=args[0];[/code]I am trying to test this, and send name to the Employee constructor. How would I go about doing this? Since it has protected access...and per the guidelines, it must be called UseManager
Either public access has to be given to Employee's name and dept., (by making them public or by adding public setter methods) or else Manager's c'tor has to set them.
Really, the best way to do this is for Manager's ctor to require name and dept. It looks to me like that's how it's intended to be.
jverda at 2007-7-21 11:10:39 >

> ...and per the guidelines, it must be called> UseManagerOkay. Just be aware, then, that those guidelines are stupid.
jverda at 2007-7-21 11:10:39 >

I am so confused is because the instructor told us to build a program based on the documentation (.html). The Manager.html has 2 methods: calculatePay(), and displayData() with no mention of a constructor method. I understand that for Employee to get the name and department, a constructor is needed! Bah, I am confused.
Well, I don't have the docs or the assignment, and I can't read your instructor's mind from this distance.You'll have to either make a judgement call based on what you undestand of the assignment and what I told you, or ask the insructor for clarification.Good luck.
jverda at 2007-7-21 11:10:39 >

You're welcome.If you have any more Java questions, please post them, but if you're confused about your assignment, go to the source. :-)
jverda at 2007-7-21 11:10:39 >
