I'm currently learning java, and im having a problem understanding what's going on when we use a constructor when inheriting from a class that already has a constructor.

The best would be to just throw in my example. I have 2 classes, "Person" and "Employee". "Employee" is inheriting from "Person", and the following is the code from each of their respective constructors:

This is the constructor for "Person" class:
===========================================

public Person(String name, String surname, int age, String nationality) {
        this.name = name;
        this.surname = surname;
        this.age = age;
        this.nationality = nationality; }

This is the constructor for "Employee" class, which is inheriting "Person"
===========================================================================

public Employee(String name, String surname, int age, String nationality, String qualification, String position, double rateOfPay, int hoursWorked){
        super(name, surname, age, nationality);     //   <-------------Not understanding 
        this.qualification = qualification;
        this.position = position;
        this.rateOfPay = rateOfPay;
        this.hoursWorked = hoursWorked;
        displayEmployeeDetails();
    }

What im not understanding is the line i marked with an arrow where there is the super(name, surname, age, nationality) line. Can anyone explain please?

"super" means this classes' immediate superclass - in this case it means "Person".
Because Employee inherits from Person, any constructor for Employee must first call a constructor from Person to ensure that all the inherited variables etc are correctly initialised. If you don't do this explicitly the compiler puts in a call to the superclass default constructor ie
super();
But if you want to use some other constructor from the superclass you must explicitly call it yourself as the first line of your constructor.
So,, in this case, when someone calls the Employee constructor with name etc, the first thing that does is to call the Person constructor with name etc so all those variables are initialised correctly.

"super" means this classes' immediate superclass - in this case it means "Person".
Because Employee inherits from Person, any constructor for Employee must first call a constructor from Person to ensure that all the inherited variables etc are correctly initialised. If you don't do this explicitly the compiler puts in a call to the superclass default constructor ie
super();
But if you want to use some other constructor from the superclass you must explicitly call it yourself as the first line of your constructor.
So,, in this case, when someone calls the Employee constructor with name etc, the first thing that does is to call the Person constructor with name etc so all those variables are initialised correctly.

Thanks, that surely clears up the whole point of using super for me. However, I'm not really understanding the reason behind declaring 'name', 'surname' and 'age' in both super() and Employee constructor. Could anyone shed some light to this? :)

Well, you're not so much declaring those fields in Employee. Instead, you're taking them in as parameters, and then delegating the work of handling those to the Person class, which knows how to deal with them.

So suppose that Person does something fancy with some of those parameters - maybe it calculates year of birth from the person's age, or it uses name and surname to do some geneology, or who knows what. By using super, you get that done for you, and then you add in the things that your Employee needs that Person doesn't.

This is a great example of what's meant by "code reuse".

Well, you're not so much declaring those fields in Employee. Instead, you're taking them in as parameters, and then delegating the work of handling those to the Person class, which knows how to deal with them.

So suppose that Person does something fancy with some of those parameters - maybe it calculates year of birth from the person's age, or it uses name and surname to do some geneology, or who knows what. By using super, you get that done for you, and then you add in the things that your Employee needs that Person doesn't.

This is a great example of what's meant by "code reuse".

Thanks for your informative reply, however there is still something I am not understanding. When we add the parameters for Employee, we mention all the fields (including those of person and employee), and we do so by writing their data type in front (such as String, double, int, etc). Why is it that when we call on super, we just mention the variable name, instead of referring to the data type too? I think I'm not fully understanding the concept behind mentioning the parameters in super again, once theyre mentioned in Employee's constructor, which nteracts with Person's constructor.

Good question. You're actually getting into some of the fun stuff here, but I'll try to keep it simple.

When you look at line 1 of either of your code sections, you're looking at a method declaration - it's a special method, but it's really a method. A method declaration tells the compiler a few things, but the part in the parentheses says "I need these variables of these types to get started, and I'm going to call them by these names".
So your constructor is actually a set of (zero or more) variable declarations. These are method variables, which means they only exist in the context of one call of one method. In this regard, they're just like declaring a variable in the course of the method - the only difference is*, the compiler uses the parameters to bring in information from outside to start up the method.

When you look at the call to the super() constructor, you're not declaring variables, you're using them. You're saying "This Employee is a Person, so make me a Person object with this name, surname, age, and nationality". Notice that the values you give to super() are the same as those in the Person constructor. In the constructor, you're demanding information: give me these values, or I can't do what I have to do. In the method call, you're supplying information: here's a name, a surname, and so forth.

That may leave you with more questions. Go ahead and ask, so I know what direction to go on in.

*not true, but the only difference you care about right now

Good question. You're actually getting into some of the fun stuff here, but I'll try to keep it simple.

When you look at line 1 of either of your code sections, you're looking at a method declaration - it's a special method, but it's really a method. A method declaration tells the compiler a few things, but the part in the parentheses says "I need these variables of these types to get started, and I'm going to call them by these names".
So your constructor is actually a set of (zero or more) variable declarations. These are method variables, which means they only exist in the context of one call of one method. In this regard, they're just like declaring a variable in the course of the method - the only difference is*, the compiler uses the parameters to bring in information from outside to start up the method.

When you look at the call to the super() constructor, you're not declaring variables, you're using them. You're saying "This Employee is a Person, so make me a Person object with this name, surname, age, and nationality". Notice that the values you give to super() are the same as those in the Person constructor. In the constructor, you're demanding information: give me these values, or I can't do what I have to do. In the method call, you're supplying information: here's a name, a surname, and so forth.

That may leave you with more questions. Go ahead and ask, so I know what direction to go on in.

*not true, but the only difference you care about right now

Thanks so much for your explanation, I think I've understood it :D As a beginner I'm still finding very trivial concepts hard to understand, but I suppose that is expected until I get the hang of it. Thanks once again for your help :)

Glad to help. Think about it, and come back with more questions. It's a pretty good way to learn.
Actually, these are not such trivial concepts, in that they go right down into the guts of the virtual machine. We're talking about what's on the stack frame at any given time, which is not beginner material. Doesn't mean you can't get it, though.

please can any one xplain it in clear way....
what is the difference between platform independent,portability,cross platform independent.....
as for as i cnsrned these all definitions are looking in a same way plz xplain the difference...plz im not understanding

Could you please delete this post and start a new thread. I'd be happy to try to answer this, but your question has nothing to do with constructors.

This question has already been answered. Start a new discussion instead.