```java
class College {
private String collegeName;
public College() {
this.collegeName = "Unknown";
}
public College(String collegeName) {
this.collegeName = collegeName;
}
public String getCollegeName() {
return collegeName;
}
public void setCollegeName(String collegeName) {
this.collegeName = collegeName;
}
public void displayInfo() {
System.out.println("College Name: " + collegeName);
}
}
class Student extends College {
private String studentName;
public Student(String collegeName, String studentName) {
super(collegeName);
this.studentName = studentName;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
@Override
public void displayInfo() {
super.displayInfo();
System.out.println("Student Name: " + studentName);
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student("ABC College", "John Doe");
student.displayInfo();
student.setCollegeName("XYZ College");
System.out.println("Updated College Name: " + student.getCollegeName());
}
}
```
In this code, the `College` class has a private field `collegeName` with getter and setter methods. The `Student` class extends the `College` class and has an additional private field `studentName` with corresponding getter and setter methods. The `displayInfo` method is overridden to display both college and student information. The `main` method creates a `Student` object, calls `displayInfo` to show the initial information, and updates the college name using the setter method.
write java code by inheritance with collage class type and add all necessary things like getter and setter method,override,calling,
1 answer