Re: Parent Class non parametrized Constructor Invoked even if am
not invoking super() constructor from Child Class
Ados wrote:
I have 3 classes student, onlinestudent which extends student class
and RunStudent class which has main method,
student class:
--------------------------------------------------------------------
package Student;
public class student {
public int Age;
public int RollNo;
public char Name;
public student() {
System.out.println("No Parameter Student Constructor");
};
public student(char a, char b) {
System.out.println("Paramter Student Constructor");
};
}
-----------------------------------------------
OnlineStudent Class:
-----------------------------------------
package Student;
public class OnlineStudent extends student{
int webid;
int netid;
public OnlineStudent() {
//super();
System.out.println("Below is the Student Class Constructor : Parent
Class");
System.out.println("Online Student Non Parameter Constructor");
}
public OnlineStudent(int webid, int netid){
System.out.println("Parameter OnlineStudent Constructor");
};
}
---------------------------
RunStudent Class
------------------------
package Student;
public class RunStudent {
public static void main(String[] args) {
OnlineStudent Raj = new OnlineStudent(4,5);
}
}
-------------------------------------
------------------------------------
My Question:
If I am creating object instance of OnlineStudent class than why non
parametrized
public student(){
System.out.println("No Parameter Student Constructor");
}
is invoked even if I am not explicitly invoking it.
If a constructor's first statement is not an explicit
call to super() or super(...arguments...), the Java compiler
automatically inserts a call to super(). You *cannot*
construct an OnlineStudent without first constructing a
Student, using one or another of Student's constructors.
Also, you cannot construct a Student until you have first
constructed an Object, using one of Object's constructors --
in this case, the Object() constructor, invoked by the
super() call Java inserts in each Student() constructor.
2nd Question:
When I change non parameterized public student() to parametrized
public student(int a, int b) than I get compilation error at
public OnlineStudent() { //
student() is not defined implicitly and so explicitly define it
//super();
System.out.println("Below is the Student Class Constructor : Parent
Class");
System.out.println("Online Student Non Parameter Constructor");
}
public OnlineStudent(int webid, int netid){ //student
() is not defined implicitly and so explicitly define it
System.out.println("Parameter OnlineStudent Constructor");
};
why do I get this error.
Sorry; I do not understand exactly what changes you have
made, nor what "this error" is.
3rd Question:
If we have defined any parametrized constructor then nonparametric
constructor let's say default constructor are needed or its ok if we
do not have them ?
Your question is unclear, but I *think* you are asking
about what constructors Java automatically creates for you.
The first thing to understand is that *every* class needs a
constructor. If you write a class with no constructors, Java
automatically creates one for you, the equivalent of
SomeClass() { super(); }
That is, Java automatically creates a constructor with no
parameters, often called the "default" or "no-arguments"
constructor.
But if you have written one or more explicit constructors
of your own, Java sees that the "every class needs a constructor"
requirement has been fulfilled. In this case, Java does not
generate any additional constructors: the class already has
constructors.
When we define any constructor then we do not get the default
constructor that is nonparametric constructor, am I right or not ?
You're right (if I've understood your question). Java
generates a no-arguments constructor if the class as written
has no constructors at all, but if the class already has at
least one constructor Java doesn't need to generate one.
--
Eric Sosman
esosman@ieee-dot-org.invalid