0% found this document useful (0 votes)
20 views5 pages

Experiment 05 PS EDITED

Uploaded by

miteshpurohit980
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)
20 views5 pages

Experiment 05 PS EDITED

Uploaded by

miteshpurohit980
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
You are on page 1/ 5

Experiment 05: Inheritance and Method Overriding

Learning Objectives: To understand Inheritances in Java and Method Overriding.

Theory:
Inheritance is an important pillar of OOP(Object-Oriented Programming). It is the
mechanism in Java by which one class is allowed to inherit the features(fields and methods)
of another class. In Java, Inheritance means creating new classes based on existing ones. A
class that inherits from another class can reuse the methods and fields of that class. In
addition, you can add new fields and methods to your current class as well.

Why Do We Need Java Inheritance?

Code Reusability: The code written in the Superclass is common to all subclasses. Child
classes can directly use the parent class code.
Method Overriding: Method Overriding is achievable only through Inheritance. It is one of
the ways by which Java achieves Run Time Polymorphism.
Abstraction: The concept of abstract where we do not have to provide all details is achieved
through inheritance. Abstraction only shows the functionality to the user.

How to Use Inheritance in Java?

The extends keyword is used for inheritance in Java. Using the extends keyword indicates
you are derived from an existing class. In other words, “extends” refers to increased
functionality.

Syntax :

class derived-class extends base-class


{
//methods and fields
}

Java Inheritance Types:

Below are the different types of inheritance which are supported by Java.

1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Hybrid Inheritance
5. Multiple Inheritance (through Interface)
Advantages Of Inheritance in Java:
1. Code Reusability: Inheritance allows for code reuse and reduces the amount of
code that needs to be written. The subclass can reuse the properties and methods
of the superclass, reducing duplication of code.
2. Abstraction: Inheritance allows for the creation of abstract classes that define a
common interface for a group of related classes. This promotes abstraction and
encapsulation, making the code easier to maintain and extend.
3. Class Hierarchy: Inheritance allows for the creation of a class hierarchy, which
can be used to model real-world objects and their relationships.
4. Polymorphism: Inheritance allows for polymorphism, which is the ability of an
object to take on multiple forms. Subclasses can override the methods of the
superclass, which allows them to change their behavior in different ways.

Disadvantages of Inheritance in Java:


1. Complexity: Inheritance can make the code more complex and harder to
understand. This is especially true if the inheritance hierarchy is deep or if
multiple inheritances is used.
2. Tight Coupling: Inheritance creates a tight coupling between the superclass and
subclass, making it difficult to make changes to the superclass without affecting
the subclass.

Problem Statement 1: Write a java program to implement single level inheritance.


Program:

Output:
Problem Statement 2: Write a java program to implement multilevel inheritance.
Program:

Output:

Problem Statement 3: Write a java program to implement method overriding.


Program:
Output:

Learning Outcomes: Students will able to


LO1.1 Understand the usage of Inheritance.
LO1.2 Understands ways to implement different types of inheritances.
LO1.3 Understands about Method Overriding.

Course Outcome:
CO1: Understand about Inheritances and Method Overriding in Java.

Conclusion:We learn the concept of Inheritance.

Viva Questions:

What are the different types Inheritances in Java?

Which Inheritance is not supported in Java and how to overcome that?

What is Method Overriding in java?

For Faculty Use only:


Correction Formative Timely completion Attendance / Total
Parameters Assessment of Practical [ 40%] Learning
[40%] Attitude [20%]
Marks
Obtained

You might also like