ok, what i have is a parent class, eployee, with 5 child classes, Doctor Surgeon Nurse Janitor and Receptionist. on my form i have a buttongroup for when the user selects one of those 5 jobs. when the user selects one i want to create an instance of that object, but i only want to have one variable such as that when the user selects a job that variable is then used to create an instance of 5 possible jobs, so far that works with using the variable Object job; declared globally, but when i got to use a method in the specified class it says that java.lang.object does not have the corresponding method. Im asking for how to create a variable that can store different object and still use that object's methods.

Recommended Answers

All 13 Replies

in your child classes did you extend object?

hmm..
can you show me part of your code? say one of the classes and how your initializing the object.

java class.....(what ever it is) extedns Employee

public Doctor (String n)
{
name = n;
}

public boolean getER()
{
return true;
}

//thats child class
//this is parent class
public Employee(String first)
{
emp = first;
}

public boolean getER()
{
return false;
}

//this is code
Object job; (declared globally)

if (radioDoctor.isSelected())
{
job = new Doctor (name);
}
some other stuff, this is how object gets declared

this is what i want to do

if (radioER.isSelected())
job.getER();


//this is where i get the error

Your child class should extend object... unless employee does? try doing that... the odd thing is that technically everything is an object right?

every class inherently extends object... so thats y im confused

Try testing it without using the employee class. does it still give you that error?

problem is the parent class is part of the project requierements...

Yes, instead of Object job; put Employee job;

This will allow you to create any Employee or child-class. This is called polymorphism and is a fundamental concept of Object Oriented programming. Google 'polymorphism in java' for more info.

Also, in Employee's constructor you set the variable emp to the String paramater, but Doctor gets a name varaiable set. Is this correct?

Also do all of the sub classes have the same methods? because if you define it as a doctor and dont have something like isJanitor() then it will give you an error.

All the subclasses can use the super's class methods. If you write:

Employee e = new Nurse();

The e will be able to use all the Employee's methods and the Nurse's methods but not the Janitors.
Try to put those isSomething() methods in the superclass or have one method in superclass that returns something that tells you the proffession of the created object

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.