Understanding Inheritance
In Java, inheritance allows a class to inherit attributes and methods from another class. Through the 'extends' keyword, subclasses inherit from superclasses, promoting code reuse and hierarchy. Subclasses can override methods, customize behaviors, and access superclass members based on their visibility. Inheritance supports single inheritance and enables the creation of complex class relationships, enhancing code organization and system extensibility within object-oriented programming paradigms.
The basic syntax of inheritance in Java involves using the extends keyword to establish a subclass and superclass relationship. Here's a concise outline:
class Superclass {
&nbs;// Fields
// Methods
}
// Subclass extending Superclass
class Subclass extends Superclass {
// Fields specific to Subclass
// Methods overriding or additional to Superclass
}
Question-1:Write a class with following specifications:
Class name: employee Data members: String name, float basic;
Member functions: void getdata(): to accept the name and basic pay
Sub Class salary inherit Class employee.
Data members : float da, hra, gross, net;
Member Functions: void calculate(): to find the followings:da=50% of basic , hra= 10% of basic , gross= basic + da + hra, pf=8.33% of (basic+da)net = gross – pf.
void display(): to display the playslip.
OUTPUT:
Enter The name:
Namita
Enter the basic:
50000
Name=Namita
Basic=50000.0
Da=25000.0
hra=5000.0
net=49000.0
Question-2:. A super class Perimeter has been defined to calculate the perimeter of a parallelogram. Define a subclass Area to compute the area of the parallelogram by using the required data members of the super class. The details are given below:
Class name: Perimeter. Data members/instance variables: a-to store the length in decimal, b- to store the breadth in decimal.
Member functions: Perimeter( … ) : parameterized constructor to assign values to data members
double Calculate( ):calculate and return the perimeter of a parallelogram as 2 * (length + breadth).
void show(): to display the data members along with the perimeter of the parallelogram.
Class name: Area.
Data members/instance variables: h -to store the height in decimal, area-to store the area of the parallelogram.
Member functions: Area( … ) : parameterized constructor to assign values to data members of both the classes. void doarea( ):compute the area as (breadth * height)
void show(): display the data members of both classes along with the area and perimeter of the parallelogram.
Solution:
OUTPUT:
Enter the height:
5
Enter the breadth:
6
Enter the length:
4
Length=4.0
Breadth=6.0
Perimeter is:20.0
height is:5.0
Area is:30.0
In Java, inheritance facilitates code reuse and hierarchy creation by allowing subclasses to inherit attributes and methods from superclasses using the 'extends' keyword. Subclasses can override superclass methods and access superclass members. Single inheritance is supported, respecting access modifiers. Constructors are not inherited, but the subclass constructor implicitly calls the superclass constructor. Ultimately, inheritance promotes modularity and maintainability in object-oriented programming.