2

I have a very basic java theory question. Why the Employee class calls its self recursively in the following example?

class Employee { Employee emp = new Employee(); } public class Manager extends Employee { public static void main(String[] args){ Manager mgr = new Manager(); } } 
3
  • 4
    You're instantiating an Employee class whitin your Employee class, which instantiates its own Employee class, which instantiates its own Employee class, etcetera. This question means you lack fundamental understanding of object oriented software. Re-read the basics :) Commented Dec 4, 2014 at 14:17
  • Why you place your second line, the Employee create, is it because you thought you have to create it, in order to have inheritance hierarchy or it's by choice, a composition, you want each Employee to have a reference to another employee? Maybe we can help you further. Commented Dec 4, 2014 at 14:25
  • @Dennisch I think it should be very clear to you from my question that I lack fundamental understanding that's why i asked the question.I read this example in a book but i didn't understand the explanation. Thanks to Jesper for such detailed explanation. Commented Dec 5, 2014 at 14:47

3 Answers 3

4

Look at what the code is doing:

When you create a new Manager object, the Employee part of that Manager object is also going to be initialized (because Manager extends Employee).

When the Employee part is initialized, its emp member variable is going to be initialized. It will be initialized with a new Employee object. But that object also has an emp member variable, which will be initialized with a new Employee object. And that object also has an emp member variable, which will be initialized with a new Employee object. And that object also has an emp member variable, which will be initialized with a new Employee object. And that object also has an emp member variable, which will be initialized with a new Employee object. And that object also has an emp member variable, which will be initialized with a new Employee object. And that object also has an emp member variable, which will be initialized with a new Employee object. ... etc. until the stack overflows.

Sign up to request clarification or add additional context in comments.

5 Comments

Note that he doesn't create an Employee in his code. He creates a Manager only. So his question is not about the recursion but about inheritance.
But Manager extends Employee, so the Employee part of the Manager object will also be initialized, leading to the recursive loop.
Best explanation, although it should start like When you create a new Manager object, you create a new Employee object, when you create a new Employee object, its emp member variable is going to be initialized. It will be initialized with a new Employee object. But that object also has an emp member variable, which will be initialized with a new Employee object...
Thanks guys, I added some info on how the Employee part of the Manager object is going to be initialized.
@Jesper, I got it now... how it works, Thanks very much for such an excellent explanation...
0

Skip the commented object creation, every time you create an Employee which happens when you create Manager because it inherits it, it enters an internal loop.

class Employee { //Employee emp = new Employee(); } 

Comments

0
class Employee { Employee emp = new Employee(); } 

What this means is that each Employee contains an instance of another Employee. As such, when your Employee is constructed, it also has to create the Employee it contains. However, by the same logic, that Employee must also have its own child Employee that must be constructed, and so on.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.