0

I have a main method part of the initiating program, a superclass called Person and subclass called Employee. Person has a couple of attributes: "name" and "age" and Employee has: "department" and "location."

How do I declare or create an instance of Person inside of Employee so Employee could then obtain those attributes?

In the main I'm initiating the program as so...

Person p1 = new Person()
p1.setName("Bob"); 
p1.setAge("45"); 
System.out.println(p1); //Prints Bob and 45 via my toString() method.

Employee e1 = new Employee()
e1.setDepartment("Accounting");
e1.setLocation("New York");
System.outprintln(e1); //Prints just department and location via this 
                         class's toString method, which uses @Override.

I've tried creating a constructor which references the Person class, but I get stuck on what to return exactly. I'm brand new to programming and my head is spinning. It's probably something simple that I'm not understanding. Any help is much appreciated.

Edited to add:

So in Employee I've tried:

Person p1 = new Person;
Public p1 setPerson(string name, int age){
}

Do I instead need to invoke individual methods such as

Public p1 setName(){
}
Public p1 getName(){
return name;
}
MNewbie
  • 15
  • 2
  • Employee **already has** those attributes as it is a child class of Person. Your problem is likely that your Employee `toString()` doesn't call the super's `toString()` method or the relevant getters methods from the super class within it. But how to answer without your showing the pertinent code from Employee or Person? – Hovercraft Full Of Eels Jul 08 '17 at 00:31
  • I tried public Person getPerson(){ return Person } Do I need public Employee getPerson(name, age){ } If so how do I return both values? Thanks! – MNewbie Jul 08 '17 at 00:40
  • We need to see *relevant* code, not what you've posted above which tells us nothing that we didn't already know. And you should show it in your question, not in comments. – Hovercraft Full Of Eels Jul 08 '17 at 00:42
  • But regardless, look at the duplicates as they'll show you what you need to know. – Hovercraft Full Of Eels Jul 08 '17 at 00:43
  • Again, read the duplicates used to close this question as you're missing the point. Also as an aside, what good are setter methods that set nothing? – Hovercraft Full Of Eels Jul 08 '17 at 00:51

0 Answers0