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.
class Parent
{
Parent(int a, int b)
{
System.out.println(" the super class constructor");
int z = a + b;
System.out.println("the super class method ");
System.out.println("the sum is "+z);
}
}
public class Child extends Parent
{
Child(int x)
{
super(12, 20);
System.out.println("the sub class constructor ");
System.out.println(x);
}
public static void main(String ... a)
{
Child obj = new Child(10);
}
}
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 Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Check Java Books
- Check Programming Books
- Practice Information Technology MCQs
- Practice BCA MCQs
- Apply for Java Internship