I have an assignment where I have been asked to ask the user for input. What I am trying to figure out is if there is anyway to call and use methods of another class whose constructor requires parameters without passing them from the main class that will be calling said methods. i.e.

public abstract class Employee 
{
   private String firstName;
   private String lastName;
   private String socialSecurityNumber;

   // three-argument constructor
   public Employee( String first, String last, String ssn )
   {
      firstName = first;
      lastName = last;
      socialSecurityNumber = ssn;
   } // end three-argument Employee constructor

   // set first name
   public void setFirstName( String first )
   {
      firstName = first;
   } // end method setFirstName

   // return first name
   public String getFirstName()
   {
      return firstName;
   } // end method getFirstName

that is a snippet of the code. In the class where the employee will be prompted to enter their firstName, is there a way to place the prompt inside the above setFirstName( String first) and then call that method without having to first pass the required arguments to the abstract employee class. New to java so my understanding is to call a method of another class you must first instaniate(sp?) an object of that class i.e. Employee sales = new Employee(). Obviously though when I do that it doesn't work because paramters are required due to the constructor. Any help would be great.

Recommended Answers

All 5 Replies

either you must instantiate it, or you must make the class static, but that wouldn't help you much in this case.

passing the variable to the class using the setter is the best approach, why would you want to change that?

** Edit, sorry just looked this over fast on my job. I've just noticed your class is abstract, which means you can not instantiate this. you'll have to create a subclass for it.

But why would you want to do that? It takes away the point of object programming. You would just be setting a name to a variable, instead of the name of an employee, which doesn't really make much sense.

edit: Say you create 2 employees

Employee jim = new Employee(...blah...);
Employee mark = new Employee(...blah...);
jim.setFirstName("Jim"); //will set the name of employee object(an actual thing)
//whereas if you use static methods or some other approach
Employee.setFirstName("pointless");//it doesn't make sense

edit2: just seen stultuske's edit, I too didn't notice, but anyway, my point still stands

There are subclasses in place. This is for school so I did not want to post to much. The subclasses refer to different type of employees in regards to pay, i.e. HourlyEmployee, SalariedEmployee, etc. These subclasses have overloaded constructors. So when creating an object of that subclass..

SalariedEmployee dave = new SalariedEmployee( *just pass bogus arguments here?* )
dave.setFirstName("Dave")

Where I am stuck is prompting the user for this information and then passing what they enter to the object that is created.

you can make that method static and call with help of class i.e
static void setFirstName(String Name)
{
//code
}
// call this by suing class name
Employee.setFirstName(jakir);

There are subclasses in place. This is for school so I did not want to post to much. The subclasses refer to different type of employees in regards to pay, i.e. HourlyEmployee, SalariedEmployee, etc. These subclasses have overloaded constructors. So when creating an object of that subclass..

SalariedEmployee dave = new SalariedEmployee( *just pass bogus arguments here?* )
dave.setFirstName("Dave")

Where I am stuck is prompting the user for this information and then passing what they enter to the object that is created.

You simply ask them for input, and then pass that input into the setFirstName();

Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String input = sc.next();
dave.setFirstName(input);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.