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