EXPERIMENT NO.
3
Date of Performance:
Date of submission:
Aim: Program on various types of inheritance and Exception handling
Theory:
Inheritance in java
Inheritance is an object-oriented programming feature that allows a class to acquire
properties and behaviors (fields and methods) of another class. It promotes code
reusability and establishes a parent-child relationship between classes.
Types of Inheritance in Java:
Type Description
●Single Inheritance One subclass inherits from one superclass.
●Multilevel Inheritance A class inherits from a class, which in turn inherits from
another class
● Hierarchical Inheritance Multiple classes inherit from the same superclass.
●Multiple Inheritance (via A class implements multiple interfaces. Java doesn't
Interface) support multiple inheritance using classes directly.
●Super Class/Parent Class: The class whose features are inherited is known as a
superclass(or a base class or a parent class).
●Sub Class/Child Class: The class that inherits the other class is known as a subclass(or a
derived class, extended class, or child class). The subclass can add its own fields and
methods
in addition to the superclass fields and methods.
●Extends Keyword: This keyword is used to inherit properties from a superclass.
Syntax:
The syntax for inheritance in Java is listed below:
class ChildClass extends ParentClass {
// Additional fields and methods
}
Exception handling in java:
An exception is an unwanted or unexpected event that disrupts the normal flow of a
program.
Java provides exception handling to gracefully handle runtime errors.
Keywords used:
● try– Block where exception-prone code is written.
● catch – Block that handles the exception.
● finally– Executes regardless of exception (optional).
● throw – Used to explicitly throw an exception.
● throws – Declares exceptions in method signature.
Program:
I. Single inheritance:
// Parent class
class Shape {
double area;
void showArea() {
[Link]("Area: " + area);
}
}
// Child class
class Rectangle extends Shape {
double length, breadth;
Rectangle(double l, double b) {
length = l;
breadth = b;
area = length * breadth; // calculate area
}
void displayRectangle() {
[Link]("Rectangle length: " + length);
[Link]("Rectangle breadth: " + breadth);
showArea(); // method from parent
}
}
// Main class
public class SingleInheritanceShapes {
public static void main(String[] args) {
Rectangle r = new Rectangle(10, 5);
[Link]();
}
}
Output:
II. Multiple inheritance:
// Base class (Grandparent)
class Person {
String name;
int age;
void setPerson(String n, int a) {
name = n;
age = a;
}
void displayPerson() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}
// Derived class (Parent)
class Student extends Person {
String school;
void setStudent(String n, int a, String s) {
setPerson(n, a);
school = s;
}
void displayStudent() {
displayPerson();
[Link]("School: " + school);
}
}
// Derived class (Child)
class CollegeStudent extends Student {
String course;
void setCollegeStudent(String n, int a, String s, String c) {
setStudent(n, a, s);
course = c;
}
void displayCollegeStudent() {
displayStudent();
[Link]("Course: " + course);
}
}
// Main class
public class MultilevelInheritanceDemo {
public static void main(String[] args) {
CollegeStudent cs = new CollegeStudent();
[Link]("Rahul", 20, "ABC School", "Computer Science");
[Link]();
}
}
Output:
Iii. Hierarchical inheritance:
// Parent class
class CollegeMember {
String name;
int age;
void setDetails(String n, int a) {
name = n;
age = a;
}
void displayDetails() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}
// Child class 1
class Student extends CollegeMember {
String course;
void setStudent(String n, int a, String c) {
setDetails(n, a);
course = c;
}
void displayStudent() {
displayDetails();
[Link]("Course: " + course);
}
}
// Child class 2
class Teacher extends CollegeMember {
String subject;
void setTeacher(String n, int a, String s) {
setDetails(n, a);
subject = s;
}
void displayTeacher() {
displayDetails();
[Link]("Subject: " + subject);
}
}
// Child class 3
class Staff extends CollegeMember {
String department;
void setStaff(String n, int a, String d) {
setDetails(n, a);
department = d;
}
void displayStaff() {
displayDetails();
[Link]("Department: " + department);
}
}
// Main class
public class HierarchicalInheritanceCollege {
public static void main(String[] args) {
Student s = new Student();
[Link]("Rahul", 20, "Computer Science");
[Link]();
[Link]()
Teacher t = new Teacher();
[Link]("Dr. Mehta", 45, "Mathematics");
[Link]();
[Link]();
Staff st = new Staff();
[Link]("Anita", 35, "Administration");
[Link]();
}
}
Output:
IV. Try-Catch Block:
public class Main {
public static void main(String[] args) {
try {
int a = 10, b = 0;
int result = a / b; // ArithmeticException
[Link]("Result: " + result);
} catch (ArithmeticException e) {
[Link]("Exception caught: " + e);
} finally {
[Link]("This block always executes");
}
}
}
Output:
V. User defined exception:
class MyException extends Exception {
MyException(String message) {
super(message);
}
}
public class Main {
public static void main(String[] args) {
try {
int age = 15;
if (age < 18) {
throw new MyException("Age must be 18 or above");
}
} catch (MyException e) {
[Link]("Custom Exception: " + [Link]());
}
}
}
Output:
CONCLUSION:
Inheritance simplifies code structure by allowing the reuse of existing classes, while
exception handling provides a structured approach to managing runtime errors. Together,
they enhance the reliability and maintainability of Java programs.
SIGN AND REMARK:
R1 R2 R3 R4 TOTAL
(3 marks) (5 marks) (4 marks) ( 3 marks) (15 marks)