Java Program to Understand that the Parameterized Constructor of the Super Class can be called from Sub Class Using super()

«
»
This is a Java Program to Understand that the Parametrized Constructor of the Super Class can be called from Sub Class Using super().

Asubclass can call a constructor defined by its superclass by use of the following form of super: super(arg-list);
Here, arg-list specifies any arguments needed by the constructor in the superclass.

Here is the source code of the Java Program to Understand that the Parametrized Constructor of the Super Class can be called from Sub Class Using super(). The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

advertisement
  1. import java.util.Scanner;
  2. class sup1
  3. {
  4.     int z;
  5.     sup1(int x, int y)
  6.     {
  7.         z = x + y;
  8.         System.out.println("Result:"+z);
  9.     }
  10. }
  11. public class Parametrized_Constructor_Demo extends sup1
  12. {
  13.     Parametrized_Constructor_Demo(int x, int y)
  14.     {
  15.         super(x , y);
  16.     }
  17.     public static void main(String[] args) 
  18.     {
  19.         Scanner s = new Scanner(System.in);
  20.         System.out.print("Enter value of a:");
  21.         int a = s.nextInt();
  22.         System.out.print("Enter value of b:");
  23.         int b = s.nextInt();
  24.         Parametrized_Constructor_Demo obj = new Parametrized_Constructor_Demo(a,b);
  25.     }
  26. }

Output:

$ javac Parametrized_Constructor_Demo.java
$ java Parametrized_Constructor_Demo
 
Enter value of a:32
Enter value of b:23
Result:55

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
advertisement
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