This discussion is archived
1 2 Previous Next 21 Replies Latest reply: Sep 22, 2006 4:08 PM by 3004 RSS

Help-Accessing Constructors

807598 Newbie
Currently Being Moderated
Hey Guys,
I am having trouble accesing a contructor from a parent class. It is protected 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!
  • 1. Re: Help-Accessing Constructors
    3004 Newbie
    Currently Being Moderated
    I can post code, if needed.
    Yes.

    When you post code, please use[code] and [/code] tags as described in Formatting tips on the message entry page. It makes it much easier to read.
  • 2. Re: Help-Accessing Constructors
    807598 Newbie
    Currently Being Moderated
    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.
  • 3. Re: Help-Accessing Constructors
    3004 Newbie
    Currently Being Moderated
    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.
  • 4. Re: Help-Accessing Constructors
    807598 Newbie
    Currently Being Moderated
    So in the Child class Manager, I must use Super to access the Employee constructor?

    Sorry for my newbness, I am learning :-P
  • 5. Re: Help-Accessing Constructors
    3004 Newbie
    Currently Being Moderated
    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.
  • 6. Re: Help-Accessing Constructors
    3004 Newbie
    Currently Being Moderated
    Sorry for my newbness, I am learning :-P
    Nothing wrong with that. Everybody's a newbie at some point.
  • 7. Re: Help-Accessing Constructors
    807598 Newbie
    Currently Being Moderated
    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.
  • 8. Re: Help-Accessing Constructors
    3004 Newbie
    Currently Being Moderated
    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.
  • 9. Re: Help-Accessing Constructors
    807598 Newbie
    Currently Being Moderated
    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.
  • 10. Re: Help-Accessing Constructors
    3004 Newbie
    Currently Being Moderated
    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.
  • 11. Re: Help-Accessing Constructors
    3004 Newbie
    Currently Being Moderated
    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.
  • 12. Re: Help-Accessing Constructors
    807598 Newbie
    Currently Being Moderated
    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.
  • 13. Re: Help-Accessing Constructors
    3004 Newbie
    Currently Being Moderated
    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.
  • 14. Re: Help-Accessing Constructors
    3004 Newbie
    Currently Being Moderated
    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.
1 2 Previous Next