0% found this document useful (0 votes)
64 views4 pages

Assignment 3

Inheritance in Java allows one class to acquire the properties and behaviors of another class. This allows for code reusability and runtime polymorphism through method overriding. There are different types of inheritance like single, multilevel, and hierarchical. Inheritance creates an "is-a" relationship and represents a parent-child relationship where the child acquires the characteristics of the parent class.

Uploaded by

Simoli Modi
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)
64 views4 pages

Assignment 3

Inheritance in Java allows one class to acquire the properties and behaviors of another class. This allows for code reusability and runtime polymorphism through method overriding. There are different types of inheritance like single, multilevel, and hierarchical. Inheritance creates an "is-a" relationship and represents a parent-child relationship where the child acquires the characteristics of the parent class.

Uploaded by

Simoli Modi
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/ 4

Title: inheritance in Java

December 22, 2022

1 Practical significance
Inheritance in Java is a mechanism in which one object acquires all the
properties and behaviors of a parent object. It is an important part of OOPs
(Object Oriented programming system)
The idea behind inheritance in Java is that you can create new classes
that are built upon existing classes. When you inherit from an existing class,
you can reuse methods and fields of the parent class. Moreover, you can add
new methods and fields in your current class also.

2 Competency
“Apply the basic knowledge of inheritance in solving engineering problems”.

3 Experimental Outcomes
The most important use of inheritance in Java is code re usability. The code
that is present in the parent class can be directly used by the child class.
Method overriding is also known as runtime polymorphic. Hence, we can
achieve Polymorphic in Java with the help of inheritance

ˆ For Method Overriding (so runtime polymorphism can be achieved).

ˆ For Code Reusability.

1
4 Theoretical Background:
Inheritance represents the IS-A relationship which is also known as a parent-
child relationship.
Inheritance in Java is a mechanism in which one object acquires all the
properties and behaviors of a parent object. It is an important part of OOPs
(Object Oriented programming system)
The idea behind inheritance in Java is that you can create new classes
that are built upon existing classes. When you inherit from an existing class,
you can reuse methods and fields of the parent class. Moreover, you can add
new methods and fields in your current class also.

Types of inheritance in java


On the basis of class, there can be three types of inheritance in java:
single, multilevel and hierarchical.
In java programming, multiple and hybrid inheritance is supported through
interface only. We will learn about interfaces later.

Figure 1: Types of inheritance

1 Single Level inheritance

2
2 Multiple inheritance

3 Hierarchical inheritance

4 hybrid inheritance

5 Experimental set up:


class Employee
float salary=40000;
class Programmer extends Employee
int bonus=10000;
public static void main(String args[])
Programmer p=new Programmer();
System.out.println(”Programmer salary is:”+p.salary);
System.out.println(”Bonus of Programmer is:”+p.bonus);

OUTPUT:
Programmer salary is:40000.0
Bonus of programmer is:10000

6 Precautions
To prevent inheritance, use the keyword ”final” when creating the class.
The designers of the String class realized that it was not a candidate for
inheritance and have prevented it from being extended

7 Procedure
The implementation of its parent class recreates a new class, which is the child
class. To inherit the parent class, a child class must include a keyword called
”extends.” The keyword ”extends” enables the compiler to understand that
the child class derives the functionalities and members of its parent class.

3
8 Observations
The idea behind inheritance in Java is that you can create new classes that
are built upon existing classes. When you inherit from an existing class, you
can reuse methods and fields of the parent class. Moreover, you can add new
methods and fields in your current class also.

9 Conclusions
Conclusion. It is a property by which another class acquires a class’s prop-
erties and behavior to provide the reusability of code. There are different
types of inheritance in Java, such as Single Inheritance, Multi-level Inheri-
tance, Hierarchical Inheritance, and Multiple Inheritance in Java.

Table 1: Inheritance v/s Composition


On the basis
Inheritance Composition
of
Inheritance represents the IS-A rela- The Composition is a way to de-
Definition tionship, which is also called as parent- sign or implement the ”has-a” re-
child relationship. lationship.
Whereas in composition, both
In inheritance, the child class is depen-
Dependability child class and parent class are in-
dent upon parent class.
dependent
Whereas, using composition, we
Multiple inheri-
Java doesn’t allow multiple inheritance. can easily achieve multiple inher-
tance
itance.

You might also like