0% found this document useful (0 votes)
24 views2 pages

Java Superclass Constructor Example

Uploaded by

payalsaini3109
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views2 pages

Java Superclass Constructor Example

Uploaded by

payalsaini3109
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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:

You might also like