EXP3
EXP3
Experiment: 3
Student Name: Amir Philip Adam UID: 22BEC10019
Branch: Electronics and Communication Section/Group: 22BEC-2/A
Semester: 5th Date of Performance: 2 4 /07/24
Subject Name: Java Programming
Subject Code: 22ECP-304
3. Theory:
Inheritance in Java is a mechanism that allows one class to inherit the properties and methods of another
class. This promotes code reuse and establishes a natural hierarchical relationship between classes. The
class that inherits from another class is called a subclass (or derived class), and the class being inherited
from is called a superclass (or base class).
A. Single Inheritance in Java
Single inheritance is a fundamental concept in object-oriented programming (OOP) where a class inherits
properties and behaviors from a single parent class. This mechanism promotes code reusability and
hierarchical class structures.
Key Points
Parent Class (Super Class): The class from which properties and methods are inherited.
Child Class (Sub Class): The class that inherits from the parent class.
Inheritance Keyword: extends is used to establish the inheritance relationship.
Syntax
class ParentClass {
// Parent class members (fields and methods)
}
University Institute of Engineering
Department of Electronics Engineering
B. Multi-level Inheritance
Involves a chain of classes where one class inherits from another, which in turn inherits from
another class.
Syntax
class GrandparentClass {
// Grandparent class members
}
C. Hierarchical Inheritance
Syntax
class ParentClass {
// Parent class members
}
A. Single Inheritance.
class Vehicle {
System.out.println("Car is accelerating!");
car.startEngine();
car.accelerate();
}
University Institute of Engineering
Department of Electronics Engineering
Output
B. Multi-level Inheritance.
class Adam {
class Main {
obj.adam();
obj.philip();
obj.peter();
obj.amir();
Output
University Institute of Engineering
Department of Electronics Engineering
C. Hierarchical Inheritance.
class Animal {
void eat() {
System.out.println("Animal is eating");
void bark() {
System.out.println("Dog is barking");
void meow() {
System.out.println("Cat is meowing");
}
University Institute of Engineering
Department of Electronics Engineering
dog.eat();
dog.bark();
cat.eat();
cat.meow();
Output
University Institute of Engineering
Department of Electronics Engineering