Advertisement
vvccs

[X]EX3_Inheritance

Nov 7th, 2023 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. import java.lang.*;
  2. import java.util.Scanner;
  3.  
  4. class College {
  5.     private String collegeName;
  6.  
  7.     public College() {
  8.         collegeName = "Default College";
  9.     }
  10.  
  11.     public College(String name) {
  12.         collegeName = name;
  13.     }
  14.  
  15.     public String getCollegeName() {
  16.         return collegeName;
  17.     }
  18.  
  19.     public void setCollegeName(String name) {
  20.         collegeName = name;
  21.     }
  22. }
  23.  
  24. class Department extends College {
  25.     private String departmentName;
  26.  
  27.     public Department() {
  28.         departmentName = "Default Department";
  29.     }
  30.  
  31.     public Department(String collegeName, String departmentName) {
  32.         super(collegeName);
  33.         this.departmentName = departmentName;
  34.     }
  35.  
  36.     public String getDepartmentName() {
  37.         return departmentName;
  38.     }
  39.  
  40.     public void setDepartmentName(String name) {
  41.         departmentName = name;
  42.     }
  43. }
  44.  
  45. class Student extends Department {
  46.     private String studentName;
  47.     private int studentID;
  48.  
  49.     public Student() {
  50.         studentName = "Default Student";
  51.         studentID = 0;
  52.     }
  53.  
  54.     public Student(String collegeName, String departmentName, String studentName, int studentID) {
  55.         super(collegeName, departmentName);
  56.         this.studentName = studentName;
  57.         this.studentID = studentID;
  58.     }
  59.  
  60.     public String getStudentName() {
  61.         return studentName;
  62.     }
  63.  
  64.     public void setStudentName(String name) {
  65.         studentName = name;
  66.     }
  67.  
  68.     public int getStudentID() {
  69.         return studentID;
  70.     }
  71.  
  72.     public void setStudentID(int id) {
  73.         studentID = id;
  74.     }
  75. }
  76. class Main {
  77.     public static void main(String[] args) {
  78.         Scanner scanner = new Scanner(System.in);
  79.  
  80.         System.out.print("Enter college name: ");
  81.         String collegeName = scanner.nextLine();
  82.  
  83.         System.out.print("Enter department name: ");
  84.         String departmentName = scanner.nextLine();
  85.  
  86.         System.out.print("Enter student name: ");
  87.         String studentName = scanner.nextLine();
  88.  
  89.         System.out.print("Enter student ID: ");
  90.         int studentID = scanner.nextInt();
  91.  
  92.         Student student = new Student(collegeName, departmentName, studentName, studentID);
  93. System.out.println("\n\n------------------------------------------------------");
  94.         System.out.println("\nCollege Name: " + student.getCollegeName());
  95.         System.out.println("Department Name: " + student.getDepartmentName());
  96.         System.out.println("Student Name: " + student.getStudentName());
  97.         System.out.println("Student ID: " + student.getStudentID());
  98.  
  99.         scanner.close();
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement