Constructor is used to initialise an object. Here we made two classes as Super and Sub class using Inheritance. We initialise two different strings in the constructors of these two classes and with the help of object of Sub class we print these strings.
Here is the source code of the Java Program Which has two Classes Which Initializes a String in Its Constructor. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
String a, b;
System.out.println("Base Class Constructor");
a = "String from Base Class";
public class String_Initialise1 extends String_Initialise
System.out.println("Derived Class Constructor");
b = "String from Derived Class";
public static void main(String... arg)
String_Initialise1 obj = new String_Initialise1();
System.out.println("the strings initialised in the constructors of Base and Derived classes are :");
System.out.println(obj.a +" and "+obj.b);
$ javac String_Initialise1.java $ java String_Initialise1 Base Class Constructor Derived Class Constructor the strings initialised in the constructors of Base and Derived classes are : String from Base Class and String from Derived Class
Sanfoundry Global Education & Learning Series – 1000 Java Programs.