I thought the result could be '43' because the type of q was 'poly 1'. However, the result was '44'. I couldn't understand that. please give me the answer.
class poly1 {
int a;
public poly1(){
a = 3;
}
public void print_a(){
System.out.print(a);
}
}
public class poly2 extends poly1{
public poly2(){
a = 4;
}
public void print_a(){
System.out.print(a);
}
public static void main(String[] args){
poly2 p = new poly2();
p.print_a();
poly1 q = new poly2();
q.print_a();
}
}
For example, the ASCII value of A is 65.
The this keyword refers to the current object in a method or constructor. The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter).
Type is the common superinterface for all types in the Java programming language. These include raw types, parameterized types, array types, type variables and primitive types.
When you invoke a class' constructor, the class' super type constructor is invoke first (until there is no super types).
When you invoke
new poly2();
The poly1
constructor is invoked first (because poly1
is a super type of poly2
), setting a
to 3
and then the poly2
constructor is invoked, setting a
to 4
which is what you see.
the type of q was 'poly 1'
What seems to confuse you is that in the following code
poly1 q = new poly2();
the variable q
is declared as type poly1
. That makes no difference in this case. What actually matters is the run time type of the object. That's determined by the new
statement. In this case, the object is of dynamic type poly2
.
When you run this program,at
poly2 p = new poly2();
a's value is 4 as it initialised by the constructor of poly2, again at
poly1 q = new poly2();
a's value is initalised by the constructor of poly2 to 4,so it will print 4 again giving a output of "44". Everytime u instatiate "p" and "q" ,the value of "a" is first initialised by the constructor of poly1 and again the calue of "a" gets initialised by constructor of class poly2 as poly2 extends poly1 class.
Try debugging the code and you will get to know the exact flow of the program.
According to the language specs, instance variables aren't even initialized until a super() call has been made.
These are the steps performed during the constructor step of class instance creation/
Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
If this constructor begins with an explicit constructor invocation (§8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.
This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.
Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.
Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With