Experiment 3 — Multilevel Inheritance
Aim:
Implement a multilevel inheritance hierarchy Person → Student → Result to
demonstrate inherited properties and method reuse in java.
Brief theory: Multilevel inheritance refers to one class inherits from another, and
that class also passes on its features to a third class. It’s like a family chain. The first
class is the grandparent or super class. The next class is the parent, which gets
features from the grandparent. The third class is the child, which gets features from
both the parent and the grandparent. In This way, the child class can use things like
features, methods, and properties from all the classes above it.
Algorithm / Procedure:
(1) Create Person with name, age, and showPerson() method.
(2) Create Student extends Person with roll and showStudent() that calls
showPerson().
(3) Create Result extends Student with marks and showResult() printing
complete info.
(4) In main(), instantiate Result and call showResult().
(5) Add more methods: compute grade from marks, override to String().
Program/Code:
// [Link]
class Person {
protected String name;
protected int age;
public Person(String name, int age) {
[Link] = name;
[Link] = age;
}
public void showPerson() {
[Link]("Name: %s, Age: %d%n", name, age);
}
}
class Student extends Person {
protected int roll;
public Student(String name, int age, int roll) {
super(name, age);
[Link] = roll;
}
public void showStudent() {
showPerson();
[Link]("Roll: " + roll);
}
}
class Result extends Student {
private double marks;
public Result(String name, int age, int roll, double marks) {
super(name, age, roll);
[Link] = marks;
}
public void showResult() {
showStudent();
[Link]("Marks: " + marks);
}
}
public class MultilevelInheritanceDemo {
public static void main(String[] args) {
Result r = new Result("Kuldeep", 19, 108, 80.5);
[Link]();
}
}