Java Program to Allocate and Initialize Super Class Members Using Constructor

«
»
This is a Java Program to Allocate and Initialize Super Class Members Using Constructor.

In a class hierarchy, constructors are called in order of derivation, from superclass to subclass.

Here is the source code of the Java Program to Allocate and Initialize Super Class Members Using Constructor. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

advertisement
  1.  class Parent
  2.  {
  3.      Parent(int a, int b)
  4.      {
  5.          System.out.println(" the super class constructor");
  6.           int z = a + b;
  7.          System.out.println("the super class method ");
  8.          System.out.println("the sum is "+z);
  9.      }
  10.  }
  11. public class Child extends Parent
  12. {
  13.     Child(int x)
  14.     {
  15.         super(12, 20);
  16.         System.out.println("the sub class constructor ");
  17.         System.out.println(x);
  18.     }
  19.     public static void main(String ... a)
  20.      {
  21.         Child obj = new Child(10);
  22.      }
  23. }

Output:

$ javac Child.java
$ java Child 
 
the super class constructor
the super class method 
the sum is 32
the sub class constructor 
10

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement

Here’s the list of Best Reference Books in Java Programming, Data Structures and Algorithms.

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn