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.

  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
advertisement

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

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.