This is a Java Program Which has two Classes Which Initializes a String in Its Constructor.
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.
class String_Initialise
{
String a, b;
public String_Initialise()
{
System.out.println("Base Class Constructor");
a = "String from Base Class";
}
}
public class String_Initialise1 extends String_Initialise
{
public String_Initialise1()
{
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);
}
}
Output:
$ 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.
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]Related Posts:
- Practice BCA MCQs
- Apply for Java Internship
- Apply for Computer Science Internship
- Practice Programming MCQs
- Practice Information Technology MCQs