Java Project - School Management System
Sridhar Raj P
?? On a mission to teach 1 million students | Developer & Mentor | 7,550+ Students ?? | Material UI | CoreUI | JavaScript | React JS | Redux | Python | Java | Springboot | MySQL | Self-Learner | Problem Solver
A School Management System using Java Inheritance.
?? Features:
? Single Inheritance: Person → Student, Teacher
? Method Overriding: Different implementations of displayInfo()
? Encapsulation: Private attributes with getters and setters
?? Java Code: School Management System
// Parent Class: Person
class Person {
protected String name;
protected int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Method to display person info (Overridden in child classes)
public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
// Child Class: Student (inherits from Person)
class Student extends Person {
private int studentId;
private String grade;
// Constructor
public Student(String name, int age, int studentId, String grade) {
super(name, age);
this.studentId = studentId;
this.grade = grade;
}
// Overriding displayInfo() to include student-specific details
@Override
public void displayInfo() {
super.displayInfo();
System.out.println("Student ID: " + studentId);
System.out.println("Grade: " + grade);
}
}
// Child Class: Teacher (inherits from Person)
class Teacher extends Person {
领英推荐
private String subject;
private double salary;
// Constructor
public Teacher(String name, int age, String subject, double salary) {
super(name, age);
this.subject = subject;
this.salary = salary;
}
// Overriding displayInfo() to include teacher-specific details
@Override
public void displayInfo() {
super.displayInfo();
System.out.println("Subject: " + subject);
System.out.println("Salary: $" + salary);
}
}
// Main Class
public class SchoolManagementSystem {
public static void main(String[] args) {
// Creating a Student object
System.out.println("\n--- Student Information ---");
Student student1 = new Student("Alice Johnson", 16, 101, "10th Grade");
student1.displayInfo();
// Creating a Teacher object
System.out.println("\n--- Teacher Information ---");
Teacher teacher1 = new Teacher("Mr. Smith", 40, "Mathematics", 50000);
teacher1.displayInfo();
}
}
?? Explanation:
1. Parent Class: Person
2. Child Class: Student (Inherits Person)
3. Child Class: Teacher (Inherits Person)
4. Main Class: SchoolManagementSystem
?? Key OOP Concepts Demonstrated
? Inheritance: Student and Teacher inherit from Person
? Method Overriding: displayInfo() behaves differently in child classes
? Encapsulation: Private attributes with constructors to initialize values