Java Program to Prove that the Default Constructor of the Super Class is Available to Sub Class by Default

«
»
This is a Java Program to Prove that the Default Constructor of the Super Class is Available to Sub Class by Default.

Whenever a child class constructor is executed it has to call immediate parent class constructor first then itself. If a child class constructor is failed to call immediate parent class constructor first then it cannot run itself. Thus it gives us a proof that Default Constructor of the Super Class is Available to Sub Class by Default.

Here is the source code of the Java Program to Prove that the Default Constructor of the Super Class is Available to Sub Class by Default. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

advertisement
  1. class Sup
  2. {
  3.     static int a, b;
  4.     Sup()
  5.     {
  6.        System.out.println("super class default constructor");
  7.     }
  8. }
  9. public class Sub extends Sup
  10. {
  11.     Sub()
  12.     {
  13.         System.out.println("sub class default constructor");
  14.     }
  15.     public static void main(String[] args)
  16.     {
  17.        new Sub();
  18.     }
  19. }

Output:

$ javac Sub.java
$ java Sub
 
super class default constructor
sub class default constructor

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