Class 12 notes
Chapter- Inheritance
Inheritance in Java is a core OOP concept that allows a class to acquire properties and behaviors from
another class. It helps in creating a new class from an existing class, promoting code reusability and better
organization.
In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance
concept" into two categories:
subclass (child) - the class that inherits from another class
superclass (parent) - the class being inherited from
To inherit from a class, use the extends keyword.
The syntax of Inheritance in JAVA
class Subclass-name extends Superclass-name
{
//methods and fields
}
In sub-classes we can inherit members as is, replace them, hide them or supplement them with new
members.
The inherited fields can be used directly, just like any other fields.
We can declare new fields in the subclass that are not in the superclass.
The inherited methods can be used directly as they are.
We can write a new instance method in the subclass that has the same signature as the one in the
superclass, thus overriding it.
We can write a new static method in the subclass that has the same signature as the one in the
superclass, thus hiding it.
We can declare new methods in the subclass that are not in the superclass.
We can write a subclass constructor that invokes the constructor of the superclass, either implicitly or
by using the keyword super.
TYPES OF INHERITANCE
Single Inheritance- In single inheritance, a sub-class is derived from only one super class. It inherits
the properties and behavior of a single-parent class. Sometimes, it is also known as simple
inheritance.
Multilevel Inheritance- In Multilevel Inheritance, a derived class will be inheriting a base class and
as well as the derived class also acts as the base class for other classes.
Hierarchical Inheritance-In hierarchical inheritance, more than one subclass is inherited from a single
base class. i.e. more than one derived class is created from a single base class.
Multiple inheritances- one class can have more than one superclass and inherit features from all
parent classes. (Is not supported in JAVA PROGRAMMING)
In Java, we can achieve multiple inheritances only through Interfaces.
Hybrid Inheritance
It is a mix of two or more of the above types of inheritance. In Java, we can achieve hybrid
inheritance only through Interfaces if we want to involve multiple inheritance to implement Hybrid
inheritance.
Advantages of Inheritance in Java
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.
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.
Class Hierarchy: Inheritance allows for the creation of a class hierarchy, which can be used to model
real-world objects and their relationships.
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.
Limitations of Inheritance in Java
Complexity -> Deep inheritance hierarchies can make code hard to understand.
Correction -> keep hierarchy simple or use composition instead
Tight Coupling -> Changes in superclass can affect subclasses.
INTERFACE
An interface in Java is a blueprint that defines a set of methods a class must implement without
providing full implementation details. It helps achieve abstraction by focusing on what a class should do
rather than how it does it. Interfaces also support multiple inheritance in Java.
A class must implement all abstract methods of an interface.
All variables in an interface are public, static, and final by default.
Interfaces can have default, static, and private methods
To implement an interface, we use the keyword implements.
Sample Questions
1. A library issues books on rental basis at a 2% charge on the cost price of the book per day. As per the
rules of the library, a book can be retained for 7 days without any fine. If the book is returned after 7
days, a fine will also be charged for the excess days as per the chart given below:
Number of excess days Fine per day (Rs.)
1 to 5 2.00
6 to 10 3.00
Above 10 5.00
A super class Library has been defined. Define a sub class Compute to calculate the fine and
the total amount. The details of the members of both the classes are given below:
Class name : Library
Data members/instance variables:
name : to store the name of the book
author : to store the author of the book
p : to store the price of the book (in decimals)
Methods / Member functions:
Library( ... ) : parameterized constructor to assign values to the
data members
void show( ) : displays the book details
Class name Compute
Data members/instance variables:
d : number of days taken in returning the book
f : to store the fine (in decimals)
Methods / Member functions:
Compute( ... ) : parameterized constructor to assign values to the
data members of both the classes
void fine( ) : calculates the fine for the excess days as given in
the table above
void show( ) : displays the book details along with the number
of days, fine and the total amount to be paid.
Total amount is (2% of price of book * total no of days) + fine
Assume that the super class Library has been defined. Using the concepts of Inheritance, specify
the class Compute giving the details of constructor, void fine ( ) and void show ( ) functions.
The super class, main function and algorithm need NOT be written.
2. A super class Godown has been defined to store the details of the stock of a retail
store. Define a subclass Update to store the details of the items purchased with the
new rate and update the stock. Some of the members of both the classes are given
below:
Class name : Godown
Data members/instance variables:
item : to store the name of the item
qty : to store the quantity of an item in stock
rate : to store the unit price of an item
amt : to store the net value of the item in stock
Member functions/methods:
Godown( …) : parameterized constructor to assign value to
the data members
void display( ) : to display the stock details
Class name : Update
Data members/instance variables:
pur_qty : to store the purchase quantity
pur_rate : to store the unit price of the purchased item
Member functions / methods
Update(…) : parameterized constructor to assign values to
the data members of both the classes
void update( ) : to update the stock by adding the previous
quantity by the purchased quantity and replace the rate of the item if there is a
difference in the purchase rate. Also update the current stock value as: (quantity * unit price )
void display( ) : to display the stock details before and after
updating
Assume that the super class Godown has been defined. Using the concept of
inheritance, specify the class Update giving details of the constructor, void update
( ) and void display( ).
The super class, main function and algorithm need NOT be written.
3. A super class Circle has been defined to calculate the area of a circle. Define a subclass Volume
to calculate the volume of a cylinder.
The details of the members of both the classes are given below:
Class name : Circle
Data members/instance variables:
radius : to store the radius in decimals
area : to store the area of a circle
Methods / Member functions:
Circle( ... ) : parameterized constructor to assign values to the
data members
void cal_area() : calculates the area of a circle (πr2)
void display( ) : to display the area of the circle
Class name Volume
Data members/instance variables:
height : to store the height of the cylinder in decimals
volume : to store the volume of the cylinder in decimals
Methods / Member functions:
Volume( ... ) : parameterized constructor to assign values to the
data members of both the classes
double calculate( ) : to calculate and return the volume of the cylinder
using the formula (πr2h) where, r is the radius and
h is the height
void display( ) : to display the area of a circle and volume of a
cylinder
Assume that the super class Circle has been defined. Using the concept of inheritance, specify the
class Volume giving the details of the constructor(...), double calculate( ) and void display( ).
The super class, main function and algorithm need NOT be written.
4. A super class Bank has been defined to store the details of the customer in a bank. Define a subclass
Interest to calculate the compound interest.
The details of the members of both the classes are given below:
Class name : Bank
Data members/instance variables:
name : to store the name of the customer
acc_no : integer to store the account number
principal : to store the principal amount in decimals
Methods / Member functions:
Bank( ... ) : parameterized constructor to assign values to the
data members
void display( ) : to display the customer details.
Class name Interest
Data members/instance variables:
rate : to store the interest rate in decimals
time : to store the time period in decimals
Methods / Member functions:
Interest( ... ) : parameterized constructor to assign values to the
data members of both the classes
double calculate( ) : to calculate and return the compound interest
using the formula [ CI = P ( 1 + R/100 )N − P] where, P is the principal, R is the rate and N is the
time
void display( ) : to display the customer details along with the
compound interest
Assume that the super class Bank has been defined. Using the concept of inheritance,
specify the class Interest giving the details of the constructor(...), double calculate( ) and void
display( ).
The super class, main function and algorithm need NOT be written.
5. A superclass Flight has been defined to store the details of a flight. Define a subclass Passenger to
calculate the fare for a passenger.
The details of the members of both the classes are given below:
Class Name: Flight
Data members/Instance variables:
flightno: To store the flight number in String.
dep_time: To store the departure time in String
arr-time: To store the arrival time in String.
Methods/Member Functions:
Flight(…)- Parameterized constructor to assign values to the data members.
void show()- To display the flight details.
Class Name: Passenger
Data members/Instance variables:
id- To store the ID of the passenger.
name- To store the name of the passenger.
tax- To store the tax to be paid in decimal.
tot- To store the total amount to be paid in decimal.
Methods/Member Functions:
Passenger(…)- Parameterized constructor to assign values to the data members of both the
classes.
void cal( )- To calculate the tax at 5% of base fare and total amount(base fare+ tax).
void show( )- To display the flight details along with the passenger details and total amount to be
paid.
Assume that the super class Flight has been defined. Using the concept of inheritance,
specify the class Passenger giving the details of the constructor(...), void cal( ) and
void show ( ).
The super class, main function and algorithm need NOT be written.
6. A super class called EmpSal has been defined to store the details of an employee. Define a derived
class Overtime to compute the total salary of the employee, after adding the overtime using the
following criteria.
If hours are more than 40, then Rs 5000 are added to salary as an overtime.
If hours are between 30 and 40 (both inclusive), then Rs. 3000 are added to salary as an overtime
amount.
If hours are less than 30, then salary remains unchanged.
The details of the members of both the classes are given below.
Class name: EmpSal
Data members/instance variables:
empnum: to store the name of the employee.
empcode: integer to store the employee code.
salary: to store the salary pf the employee in decimals.
Methods/member functions:
EmpSal(…)- Parameterised constructor to assign values to the data members.
void show()- To display the details of the employee.
Class name: Overtime
Data members/instance variables:
hours: integer to store overtime in hours.
total: to store total salary in decimals.
Methods/member functions:
Overtime(…)- parameterised constructor to assign values to the data members of both the
classes.
void calSal( )- Calculates the total salary by adding the overtime amount to salary as per the criteria
given above.
void show ( )- to display the employee details along with the total salary(salary+overtime amount).
Assume that the super class EmpSal has been defined. Using the concept of inheritance, specify the
class Overtime giving the details of the constructor(...), void calSal( ) and void show ( ).
The super class, main function and algorithm need NOT be written.
7. A super class Bank has been defined to store the details of a customer. Define a sub-class Account
that enables transactions for the customer with the bank. The details of both the classes are given
below:
Class name: Bank
Data member/instance variable:
name: stores the name of the customer.
accno: stores the account number.
p: stores the principal amount in details.
Member functions/methods:
Bank(…) parameterized constructor to assign values to the instance variables.
void display() displays the details of the customer.
Class name: Account
Data member/instance variables:
amt: Stores the transaction amount in decimals.
Member functions/methods:
Account(…)- parameterized constructor to assign the values to the instance variables of both the
classes.
void deposit( )- accepts the amount and updates the principal as p=p+amt
void withdraw( )- accepts the amount and updates the principal as p=p-amt
If the withdrawal amount is more than the principal, then display the message “Insufficient Balance”.
If principal amount after withdrawal is less than 500, then a penalty is imposed by using the formula
p=p-(500-p)/10
void display()- displays the details of the customer.
Assume that the super class Bank has been defined. Using the concept of inheritance,
specify the class Account giving the details of the constructor(...), void deposit( ), void
withdrawal() and void display ( ).
The super class, main function and algorithm need NOT be written.
8. Class name: Product
Data member/instance variables:
name: stores the name of the product.
code: integer to store the product code.
amount: stores the total sale amount of the product in decimals.
Member functions/methods:
Product (String n, int c, double p): parameterized constructor to assign members name=n, code=c,
amount=p.
void show(): displays the details of the data members.
Class name: Sales
Data member/instance variables:
day: stores number of days taken to pay the amount
tax: to store the service tax in decimals
totamt: to store the total amount in decimals
Member functions/methods:
Sales(…) parameterized constructor to assign values to the instance variables.
void compute() Calculates the service tax @12.4% of the sale amount.
Calculates the fine @2.5% of the actual sale amount only if the amount paid by the retailer to the
wholesales exceeds 30 days.
Calculates the total amount paid by the retailer as (actual sale amount+service tax+fine)
void show() displays the data members of super class and the total amount.
Assume that the super class Product has been defined. Using the concept of inheritance, specify the
class Sales giving the details of the constructor(...), void compute( ) and void show( ).
The super class, main function and algorithm need NOT be written.
9. A super class Record contains names and marks of the students in two different single dimensional
arrays. Define a sub class Highest to display the names of the students obtaining the highest mark.
The details of the members of both the classes are given below:
Class name: Record
Data member/instance variable:
n[]: array to store names.
m[]: array to store marks
size: to store the number of students
Member functions/ methods
Record(int cap)- parameterized constructor to initialize the data member size=cap.
void readarray()- to enter elements in both the arrays
void display()- displays the array elements.
Class name: Highest
Data member/instance variable:
ind: to store the index
Member function/ methods
Highest(…)- parameterized constructor to initialize the data members of both the
classes.
void find( )- finds the index of the student obtaining the highest mark and assign it
to ’ind’.
void display( )- displays the array elements along with the names and marks of the
students who have obtained the highest mark.
Assume that the super class Record has been defined. Using the concept of inheritance, specify the
class Highest giving the details of the constructor(...), void find()and void display ( ).
The super class, main function and algorithm need NOT be written.
10. A super class Number is defined to calculate the factorial of a number. Define a sub class Series to
find the sum of the series S= 1!+2!+3!+…+n!.
The details of the members of both the classes are given below.
Class name: Number
Data member/instance variable:
n: to store an integer number.
Member functions/methods:
Number(int nn): to store an integer number.
int factorial(int a): returns factorial of a number.
void display(): displays the data members.
Class name: Series
Data member/instance variable:
sum: to store the sum of the series.
Member functions/methods:
Series(…)- parameterized constructor to initialize the data members of both
the classes.
void calsum()- calculates the sum of the given series.
void display()- displays the data members of both the classes.
Assume that the super class Number has been defined. Using the concept of inheritance, specify the
class Series giving the details of the constructor(...), void calsum()and void display ( ).
The super class, main function and algorithm need NOT be written.