0% found this document useful (0 votes)
5 views2 pages

Experiment 3 Java Programming

The document outlines an experiment on multilevel inheritance in Java, demonstrating a hierarchy of classes: Person, Student, and Result. It explains the concept of inheritance where a child class inherits properties and methods from its parent and grandparent classes. The provided code implements this hierarchy and includes methods to display information about a person, student, and their results.

Uploaded by

kuldeepbehera431
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Experiment 3 Java Programming

The document outlines an experiment on multilevel inheritance in Java, demonstrating a hierarchy of classes: Person, Student, and Result. It explains the concept of inheritance where a child class inherits properties and methods from its parent and grandparent classes. The provided code implements this hierarchy and includes methods to display information about a person, student, and their results.

Uploaded by

kuldeepbehera431
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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]();
}
}

You might also like