About Parent and Child Classes Relations

We will do some simple poc on the following topics.

1. How many objects are created when child class object is created?
2. Child Class is the instance of Parent Class?
3. Parent Class is the instance of Child Class?
4. How to initialize the instance variables of Parent class through Child class?
5. How is Parent's super class related to its child class?


1. How many objects are created when child class object is created?

Lets do a poc on this, Lets a create a parent class Parentclass.java, which prints the hashcode when constructor of this class is called.
  
 package com.learning.childparent;  
 public class ParentClass {  
      public ParentClass() {  
           System.out.println("Parent Constructor : "+this.hashCode());  
      }  
 }  

Now create child class ChildClass.java which extends the ParentClass, and also print the hashcode in the constructor of the child class.

 package com.learning.childparent;  
 public class ChildClass extends ParentClass {  
      public ChildClass() {  
           System.out.println("Child Constructor : " + this.hashCode());  
      }  
 }  

Lets test the code,  create one more class called TestingClass and create an object of the child class and run the code.

 package com.learning.childparent;  
 public class TestingClass {  
      public static void main(String[] args) {  
           /*  
            * We Know already that Object is created first and later constructor is  
            * called printing the hashcodes in the constructors both Parent Class  
            * and child class  
            */  
           ChildClass childClass = new ChildClass();  
      }  
 }  


Output :
 Parent Constructor : 2022023948  
 Child Constructor : 2022023948  

Now you can see both the hashCodes are same, the answer for How many objects are created when child object is created ? Ans. 1, because if there are two objects then hashcodes would be different.



2. Child Class is the instance of Parent Class?

We take the same classes from the point one and then lets run the code.
 package com.learning.childparent;  
 public class TestingClass {  
      public static void main(String[] args) {  
           ChildClass childClass = new ChildClass();  
           System.out.println("**********************");  
           // This would be obviously true as ChildClass object is the instanceof  
           // itself  
           System.out.println(childClass instanceof ChildClass);  
           System.out.println("**********************");  
           System.out.println(childClass instanceof ParentClass);  
           System.out.println("**********************");  
           System.out.println(childClass instanceof Object);  
      }  
 }  


Output:
 Parent Constructor : 227780261  
 Child Constructor : 227780261  
 **********************  
 true  
 **********************  
 true  
 **********************  
 true  

So answer for Child Class is the instance of Parent Class?
Answer : Only one Child object is created and the created childObject is instance to itself, instance to parent class and Instance of parent's super  class (i.e, java.lang.Object Class).

3. Parent Class is the instance of Child Class?

We take the same classes from the point one and then lets run the code.

 package com.learning.childparent;  
 public class TestingClass {  
      public static void main(String[] args) {  
           ParentClass parentClass =new ParentClass();  
           System.out.println("*********************");  
           System.out.println(parentClass instanceof ParentClass);  
           System.out.println("*********************");  
           System.out.println(parentClass instanceof ChildClass);  
      }  
 }  

Output: 

Parent Constructor : 990234593
*********************
true
*********************
false 

So the answer for Parent Class is the instance of Child Class?

Answer: Parent Class object cannot be a reference to the child class,
its as simple as that child the features which are inherited from parent class, 
but parent cannot get  the features from the child.

4. How to initialize the instance variables of Parent class through Child class?

This is a very important concept in java, lets lets do step by step analysis, Lets create  a parent class ParentClass.java 

package com.learning.childparent;
public class ParentClass {
 public String instanceVariable1OfParentClass;
 public int instanceVariable2OfParentClass;
        private boolean instanceVariable3OfParentClass;
 public ParentClass() {
  
 }

}

Now Create a child class ChildClass.java which extends the ParentClass
package com.learning.childparent;
public class ChildClass extends ParentClass {

 public String instanceVariable1OfChildClass;
 public int instanceVariable2OfChildClass;

 public ChildClass() {

 }

}

We already know when we create an object of child class, it is also reference to parent class, so if that is the case then members of the parents class (which are not private) should be visible to the child class.

So lets see if its true 

package com.learning.childparent;

import java.lang.reflect.Field;

public class TestingClass {

 @SuppressWarnings("rawtypes")
 public static void main(String[] args) throws ClassNotFoundException {
  System.out.println("***********");
  // lets create Class Object from the childClass
  Class childClass = Class.forName("com.learning.childparent.ChildClass");
  // By using reflection API i am trying to get the data members of
  // the child class
  Field[] childFields = childClass.getFields();
  for (Field childfield : childFields) {
   System.out.println(childfield.getName());
  }
  System.out.println("***********");
 }
}


Output:
***********
instanceVariable1OfChildClass
instanceVariable2OfChildClass
instanceVariable1OfParentClass
instanceVariable2OfParentClass
***********


so we can see from the output that even the parents data members (which are not private) are accessible. 

Now, We know that when an object is created the instance variables will hold the default values of its types.

lets see by that by printing the values of the declared instance variables of both parent and child  constructor in its respective classes

package com.learning.childparent;

public class ParentClass {
 public String instanceVariable1OfParentClass;
 public int instanceVariable2OfParentClass;

 public ParentClass() {
  System.out.println("******************************************");
  System.out.println("Value of instanceVariable1OfParentClass : "+this.instanceVariable1OfParentClass);
  System.out.println("Value of instanceVariable2OfParentClass : "+this.instanceVariable2OfParentClass);
  System.out.println("******************************************");
 }

}


package com.learning.childparent;

public class ParentClass {
 public String instanceVariable1OfParentClass;
 public int instanceVariable2OfParentClass;

 public ParentClass() {
  System.out.println("******************************************");
  System.out.println("Value of instanceVariable1OfParentClass : "+this.instanceVariable1OfParentClass);
  System.out.println("Value of instanceVariable2OfParentClass : "+this.instanceVariable2OfParentClass);
  System.out.println("******************************************");
 }

}

lets run the code by creating the object of the child class
package com.learning.childparent;

public class TestingClass {

 public static void main(String[] args) throws ClassNotFoundException {

  ChildClass childClass = new ChildClass();

 }
}

Output:
******************************************
Value of instanceVariable1OfParentClass : null
Value of instanceVariable2OfParentClass : 0
******************************************
******************************************
Value of instanceVariable1OfChildClass : null
Value of instanceVariable2OfChildClass : 0
******************************************

Now we know when an object is created the declared instance variables hold the default values of its types.

lets create paramterized constructors in both Parent and child to intialize the instance variable and see what is the behaviour.

package com.learning.childparent;

public class ParentClass {
 public String instanceVariable1OfParentClass;
 public int instanceVariable2OfParentClass;

 public ParentClass() {
  System.out.println("******************************************");
  System.out.println("Value of instanceVariable1OfParentClass : " + this.instanceVariable1OfParentClass);
  System.out.println("Value of instanceVariable2OfParentClass : " + this.instanceVariable2OfParentClass);
  System.out.println("******************************************");
 }

 public ParentClass(String variable1, int variable2) {
  this.instanceVariable1OfParentClass = variable1;
  this.instanceVariable2OfParentClass = variable2;
  System.out.println("Value of instanceVariable1OfParentClass : " + this.instanceVariable1OfParentClass);
  System.out.println("Value of instanceVariable2OfParentClass : " + this.instanceVariable2OfParentClass);
 }

}


package com.learning.childparent;

public class ChildClass extends ParentClass {

 public String instanceVariable1OfChildClass;
 public int instanceVariable2OfChildClass;

 public ChildClass() {
  System.out.println("******************************************");
  System.out.println("Value of instanceVariable1OfChildClass : " + this.instanceVariable1OfChildClass);
  System.out.println("Value of instanceVariable2OfChildClass : " + this.instanceVariable2OfChildClass);
  System.out.println("******************************************");
 }

 // since we have got the members of Parents class to child class,
 // passing the values of the instance variables through child class constructor.
 public ChildClass(String Parentvariable1, int ParentVariable2, String variable1, int variable2) {

  this.instanceVariable1OfChildClass = variable1;
  this.instanceVariable2OfChildClass = variable2;
  System.out.println("Value of instanceVariable1OfChildClass : " + this.instanceVariable1OfChildClass);
  System.out.println("Value of instanceVariable2OfChildClass : " + this.instanceVariable2OfChildClass);

 }
}


now lets run the code by passing the values to the parameterized constructor of child class object

package com.learning.childparent;

public class TestingClass {

 public static void main(String[] args) throws ClassNotFoundException {

  ChildClass childClass = new ChildClass("this is parent variable 1", 1, "this is child variable 1", 2);

 }
}


Output:
******************************************
Value of instanceVariable1OfParentClass : null
Value of instanceVariable2OfParentClass : 0
******************************************
Value of instanceVariable1OfChildClass : this is child variable 1
Value of instanceVariable2OfChildClass : 2

We can still see parents instance variables hold the default values, because it is not an automatic process even though passing the values of ParentClass instance variable through child it will not set, We need to call the parent's paramterized constrictor ans pass the values in the child's parameterized constructor, This process is called Constructor Chaining.

lets see now how it works.

package com.learning.childparent;
public class ChildClass extends ParentClass {

 public String instanceVariable1OfChildClass;
 public int instanceVariable2OfChildClass;

 public ChildClass() {
  System.out.println("******************************************");
  System.out.println("Value of instanceVariable1OfChildClass : " + this.instanceVariable1OfChildClass);
  System.out.println("Value of instanceVariable2OfChildClass : " + this.instanceVariable2OfChildClass);
  System.out.println("******************************************");
 }

 // since we have got the members of Parents class to child class,
 // passing the values of the instance variables through child class
 // constructor.
 public ChildClass(String Parentvariable1, int ParentVariable2, String variable1, int variable2) {
  // calling the parents constructor 
  super(Parentvariable1,ParentVariable2);
  this.instanceVariable1OfChildClass = variable1;
  this.instanceVariable2OfChildClass = variable2;
  System.out.println("Value of instanceVariable1OfChildClass : " + this.instanceVariable1OfChildClass);
  System.out.println("Value of instanceVariable2OfChildClass : " + this.instanceVariable2OfChildClass);

 }
}


now in the above child class i am calling the parent parameterized constructor through super();

Lets test code.

package com.learning.childparent;

public class TestingClass {

 public static void main(String[] args) throws ClassNotFoundException {

  ChildClass childClass = new ChildClass("this is parent variable 1", 1, "this is child variable 1", 2);

 }
}

Output:
Value of instanceVariable1OfParentClass : this is parent variable 1
Value of instanceVariable2OfParentClass : 1
Value of instanceVariable1OfChildClass : this is child variable 1
Value of instanceVariable2OfChildClass : 2

This is how we initialize the data members of parent members through child class.

5. How is Parent's super class related to the Parent's child class?

Lets create a simple POC on this to find out what exactly is the relation.

lets create parent class ParentClass.java with few methods

package com.learning.childparent;
public class ParentClass {

 public void parentMethod1() {

 }

 public void parentMethod2() {

 }

 public void parentMethod3() {

 }
}

Lets create child class ChildClass.java with few methods extending the parent class.

package com.learning.childparent;
public class ChildClass extends ParentClass {

 public void childMethod1() {

 }

 public void childMethod2() {

 }

 public void childMethod3() {

 }
}

now lets see by reflection what exactly are the visible available for child class

package com.learning.childparent;
import java.lang.reflect.Method;

public class TestingClass {

 public static void main(String[] args) throws ClassNotFoundException {
  // lets do an reflection of the child class to fetch what all the
  // methods it holds
  Class childClass = Class.forName("com.learning.childparent.ChildClass");
  System.out.println("*************************");
  System.out.println("Super Class" + childClass.getSuperclass());
  // getMethods() will provide methods from the super class which are
  // available to the child class
  Method[] childClassMethods = childClass.getMethods();
  System.out.println("*************************");
  for (Method method : childClassMethods) {

   System.out.println(method.getName());
  }
  System.out.println("*************************");
 }
}

Output:


Super Classclass com.learning.childparent.ParentClass
*************************
childMethod1
childMethod2
childMethod3
parentMethod1
parentMethod2
parentMethod3
wait
wait
wait
equals
toString
hashCode
getClass
notify
notifyAll
*************************

now we can see, Super class of Child Class shows
com.learning.childparent.ParentClass
but we are also seeing the below methods which are the methods of java.lang.Object classs
wait
wait
wait
equals
toString
hashCode
getClass
notify
notifyAll

people get confused here saying Object is also direct superclass of child class, which is  not the case. because JAVA never supports multiple inheritance. but we are seeing the methods of ParentClass's super class methods because,
ParentClass's  super class is Object class so the  methods of are Object class is  visible
 to Parent's Class, so now we are extending the ParentClass to ChildClass, so what ever the ParentsClass methods are having (with object class methods) are available to Child Class.

Object<---super class--- ParentClass<------super class----ChildClass.
this kind of relationship is called multilevel inheritance.




I am an open source enthusiast. Currently having 6 years of experience and part of Ericsson Global. I am a traveler, Coding manic, Foodie, Gaming Freak and an Amazing Cook.

Share this

Pages
Previous
Next Post »