0% found this document useful (0 votes)
6 views8 pages

EXP3

The document outlines a practical experiment on inheritance in Java, detailing its types such as single, multi-level, and hierarchical inheritance. It includes theoretical explanations, syntax examples, and code implementations for each type of inheritance. Additionally, an evaluation grid for student performance is provided for faculty assessment.

Uploaded by

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

EXP3

The document outlines a practical experiment on inheritance in Java, detailing its types such as single, multi-level, and hierarchical inheritance. It includes theoretical explanations, syntax examples, and code implementations for each type of inheritance. Additionally, an evaluation grid for student performance is provided for faculty assessment.

Uploaded by

amirphil38
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

University Institute of Engineering

Department of Electronics Engineering

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

1. Aim of the practical: To Study The Execution Of Inheritance


And Its Different types.

2. Tool Used:VS Code

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

class ChildClass extends ParentClass {


// Child class members (fields and methods)
}

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
}

class ParentClass extends GrandparentClass {


// Parent class members
}

class ChildClass extends ParentClass {


// Child class members
}

C. Hierarchical Inheritance

 Multiple subclasses inherit from a single superclass.

Syntax

class ParentClass {
// Parent class members
}

class ChildClass1 extends ParentClass {


// Child class 1 members
}

class ChildClass2 extends ParentClass {


// Child class 2 members
}
University Institute of Engineering
Department of Electronics Engineering

4. Code for Different types of Inheritance

A. Single Inheritance.

class Vehicle {

public void startEngine() {

System.out.println("Vehicle engine started!");

class Car extends Vehicle {

public void accelerate() {

System.out.println("Car is accelerating!");

public void startEngine() {

System.out.println("Car engine started with a roar!");

public class Main {

public static void main(String[] args) {

Car car = new Car();

car.startEngine();

car.accelerate();

}
University Institute of Engineering
Department of Electronics Engineering

Output

B. Multi-level Inheritance.

class Adam {

public void adam() {

System.out.println("My name is Adam");

class Philip extends Adam {

public void philip() {

System.out.println("My name is Philip");

class Peter extends Philip {

public void peter() {

System.out.println("My name is Peter");


University Institute of Engineering
Department of Electronics Engineering

class Amir extends Peter {

public void amir() {

System.out.println("My name is Amir");

class Main {

public static void main(String[] args) {

Amir obj = new Amir();

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");

class Dog extends Animal {

void bark() {

System.out.println("Dog is barking");

class Cat extends Animal {

void meow() {

System.out.println("Cat is meowing");

}
University Institute of Engineering
Department of Electronics Engineering

public class Main {

public static void main(String[] args) {

Dog dog = new Dog();

Cat cat = new Cat();

dog.eat();

dog.bark();

cat.eat();

cat.meow();

Output
University Institute of Engineering
Department of Electronics Engineering

5. Evaluation Grid (To be filled by Faculty):


Sr. No. Parameters Marks Obtained Maximum Marks
1. Student Performance 12
(Conduct of experiment)
objectives/Outcomes.
2. Viva Voce 10
3. Submission of Work 8
Sheet
(Record)
Signature of Faculty Total Marks Obtained: 30
(with Date):

You might also like