Design a class student, exam and result, where result is inherited from exam and exam is inherited from student. Write possible constructors to initialize the value. Write a main function to test the constructor execution by creating objects.


Q.) Design a class student, exam and result, where result is inherited from exam and exam is inherited from student. Write possible constructors to initialize the value. Write a main function to test the constructor execution by creating objects.

Subject: Object Oriented Technology

I. Introduction

Inheritance is a fundamental concept in Object Oriented Programming (OOP) where a new class is formed using properties and functionalities of an existing class. The new class is known as the derived (or child) class and the existing class is known as the base (or parent) class. In this question, we are asked to design three classes: student, exam, and result, where result is a child class of exam, and exam is a child class of student.

II. Class Design

Student Class

The student class is the base class in this scenario. It can have attributes such as studentId and name. Here is a possible design:

public class Student {
    private String studentId;
    private String name;

    // getters and setters
}

Exam Class

The exam class is a child class of student. It can have additional attributes such as examId and subject. Here is a possible design:

public class Exam extends Student {
    private String examId;
    private String subject;

    // getters and setters
}

Result Class

The result class is a child class of exam. It can have additional attributes such as marks and grade. Here is a possible design:

public class Result extends Exam {
    private int marks;
    private String grade;

    // getters and setters
}

III. Constructors

In OOP, a constructor is a special method used to initialize objects. It is called when an object of a class is created. In Java, constructors have the same name as the class and do not return any type.

In the context of inheritance, we can use the 'super' keyword in the child class to call the constructor of its parent class. This is necessary when we want to initialize the attributes of the parent class.

Here are the constructors for our classes:

Student Class Constructor

public Student(String studentId, String name) {
    this.studentId = studentId;
    this.name = name;
}

Exam Class Constructor

public Exam(String studentId, String name, String examId, String subject) {
    super(studentId, name);
    this.examId = examId;
    this.subject = subject;
}

Result Class Constructor

public Result(String studentId, String name, String examId, String subject, int marks, String grade) {
    super(studentId, name, examId, subject);
    this.marks = marks;
    this.grade = grade;
}

IV. Main Function

In the main function, we can create an object of the result class to test the execution of the constructors. When we create an object of the result class, the constructors of the parent classes (exam and student) are also called.

Here is a possible main function:

public static void main(String[] args) {
    Result result = new Result("S01", "John Doe", "E01", "Math", 85, "A");
}

V. Program

Here is the complete program:

public class Student {
    private String studentId;
    private String name;

    public Student(String studentId, String name) {
        this.studentId = studentId;
        this.name = name;
    }

    // getters and setters
}

public class Exam extends Student {
    private String examId;
    private String subject;

    public Exam(String studentId, String name, String examId, String subject) {
        super(studentId, name);
        this.examId = examId;
        this.subject = subject;
    }

    // getters and setters
}

public class Result extends Exam {
    private int marks;
    private String grade;

    public Result(String studentId, String name, String examId, String subject, int marks, String grade) {
        super(studentId, name, examId, subject);
        this.marks = marks;
        this.grade = grade;
    }

    // getters and setters
}

public class Main {
    public static void main(String[] args) {
        Result result = new Result("S01", "John Doe", "E01", "Math", 85, "A");
    }
}

VI. Conclusion

Understanding inheritance and constructors is crucial in OOP as it allows us to create more complex and organized structures in our programs. Inheritance allows us to reuse code and create relationships between classes, while constructors allow us to initialize our objects in a controlled manner. In this question, we have designed three classes using inheritance and initialized their attributes using constructors.

Summary

In this question, we are asked to design three classes: student, exam, and result, where result is a child class of exam, and exam is a child class of student. We will create constructors for each class to initialize their attributes. Finally, we will test the constructors by creating an object of the result class in the main function.

Analogy

Imagine a family tree where the student class is the parent, the exam class is the child, and the result class is the grandchild. The parent passes down certain traits to the child, and the child passes down certain traits to the grandchild.

Quizzes
Flashcards
Viva Question and Answers

Quizzes

What is inheritance in object-oriented programming?
  • A. It is a concept where a new class is formed using properties and functionalities of an existing class.
  • B. It is a concept where a new class is formed using properties and functionalities of an existing object.
  • C. It is a concept where a new object is formed using properties and functionalities of an existing class.
  • D. It is a concept where a new object is formed using properties and functionalities of an existing object.