Reputation: 1655
Below I am Having Two classes.Parent and Child. The Child class inherits from Parent class .In Parent class constructor I am calling print() method of Parent class.
When I create Object for Child class in main() method the Parent class constructor runs and the Child class print() method is called instead of Parent class print() method.
Q1. Why this Happens.
Q2. Why the value of i is 0
public class Sample
{
public static void main(String[] args)
{
Child objChild = new Child();
objChild.print();
}
}
class Parent
{
void print()
{
System.out.println("i Value");
}
Parent()
{
print();
}
}
class Child extends Parent
{
int i = 45;
void print()
{
System.out.println("i Value = "+i);
}
}
OP
i Value = 0
i Value = 45
Upvotes: 19
Views: 14880
Reputation: 2753
Java Beginner : I got your problem.. 1.When you create the object of child class at that time print() method called only in child class not in parent class even though parent class constructor called first bcoz the object is of child class Please refer the following example.
public class Test
{
public static void main(String[] args)
{
//create the object of child class
Child child = new Child();
}
}
class Parent
{
void print()
{
System.out.println("parent>> i ValueOne=");
}
Parent()
{
System.out.println("parent>> i ValueTwo=");
print();
}
}
class Child extends Parent
{
int i = 45;
void print()
{
System.out.println("Child>>i Value = "+i);
}
}
output:
parent>> i ValueTwo=
Child>>i Value = 0
2.If you create the parent class object at that time the print() method in parent class is called. Please refer the following example.
public class Test
{
public static void main(String[] args)
{
//create the object of Parent class
Parent parent = new Parent();
}
}
class Parent
{
void print()
{
System.out.println("parent>> i ValueOne=");
}
Parent()
{
System.out.println("parent>> i ValueTwo=");
print();
}
}
class Child extends Parent
{
int i = 45;
void print()
{
System.out.println("Child>>i Value = "+i);
}
}
output:
parent>> i ValueTwo=
parent>> i ValueOne=
3.And i think you have already cleared why that value of i is 0 or 45.
Upvotes: 4
Reputation: 2705
First, I do not see the constructor definition in your Child
class.
When you do not define a constructor for a Child
class, its default constructor is:
public Child() {
super();
}
Where super();
calls the Parent
class constructor(super class constructor).
However, you defined your print()
method in Child
class, and since it has the same signature(name+parameter) as the method in Parent
class, it overrides its super class method with the same signature.
That is why your Child
class calls Parent
class constructor, while it calls its own print()
method.
-added:
The first i value is 0 because you did not initialize an int variable i
in Parent
class, and the value of an uninitialized int variable is 0 by default. After Parent
is called, it invokes now Child
's print()
method, and i
is initialized in Child
class, so now i
is the value you initialized
Upvotes: 2
Reputation: 1398
Java actually has some pretty clear rules about this. Essentially the "int i = 45" code in the subclass is an implicit part of the subclass constructor and the superclass constructor always runs first.
The order of events is:
This gets really nasty when you have a final field. You could declare "i" final in your example and get the same result!
In general, invoking non-private (or at least non-final) methods in a constructor is asking for trouble and is often the source of nasty bugs. Invoking an abstract method is a really bad idea (most of the time).
Upvotes: 10
Reputation: 7890
Because at that time of first call to print method i
is not initialized to 45 so its printing 0.
call goes like this
i
is not initialized coz child constructor is not complete yet so its printing the default value of i
which is 0 )i
gets its value which is 45 -Upvotes: 2
Reputation: 39451
The reason the child method is called is because virtual method dispatch is the norm in Java. When you call the method, it decides which method to actually call at runtime based on the actual type of the object.
As for why it prints 0, that's because i
hasn't been set to 45 yet. Each field is initialized with a default value for that type, 0
in the case of integers. When you write int i = 45
, the compiler will generate code in the constructor to set i = 45
. But it places this code after the parent constructor is called. Therefore, you're printing the variable before it is initialized with its intended value.
Upvotes: 19
Reputation: 1376
Your child first calls your parent's constructor. At this point, i isn't initialized with the 45 value. That's why it prints 0 (the default int value).
Upvotes: 1