Exp – 7
Aim: Write a program in Java in which a subclass constructor invokes the constructor of the super class and
instantiate the values
Program:
// Superclass
class Person {
String name;
int age;
// Superclass constructor
Person(String name, int age) {
[Link] = name;
[Link] = age;
[Link]("Superclass constructor called");
}
void displayInfo() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}
// Subclass
class Student extends Person {
String course;
// Subclass constructor
Student(String name, int age, String course) {
super(name, age); // Calling superclass constructor
[Link] = course;
[Link]("Subclass constructor called");
}
void showDetails() {
displayInfo();
[Link]("Course: " + course);
}
}
public class SuperConstructorDemo {
public static void main(String[] args) {
Student s1 = new Student("Alice", 20, "Computer Science");
[Link]();
}
}
Output: