Inheritance and Polymorphism
Inheritance and Polymorphism
Inheritance and Polymorphism
Inheritance the technique by virtue of which a class acquires the properties and
features (Data Members and Member Methods) of another class.
A class that gets inherited to another class in known as base class or parent class,
whereas the class that inherits a base class is known as derived or child class.
keyword ‘extends ‘ is used to used to inherit a class into another class.
Need of Inheritance
The base class simply defines the common functionality to be used by other classes. Once,
the base class is compiled and machine code is generated, you don’t need to recompile it
again at the time of accessing it into derived class. This feature is known as ‘Reuseability’.
Accessing Base Class Members:
Java does not allow to change the protection level of the base class members in the derived
class. Hence, the access specifier of the derived class in always public.
Private Member of a base class:
A class member declared private can never be used outside the class visibility even if the
class has been inherited to another class. Hence, private member of the base class are not
directly accessible to the derived class. But, if in any case such a situation arises that the
private members must be accessed then it can be done by using getters and setters.
Getter – It is a member method that is used to return the value of private member of base
class in derived class. The method getter is used in base class but it returns the value when
invoked in derived class. Getter is also helpful in accessing private member methods of base
class.
Setter – It is a member method which is used to feed the value in a private data member of
base class. You are well aware that base class is only a common code to be accessed by
various derived classes. Hence, the elements of base class are accessed through objects of
derived class. The need of using setter arises when the value to the private instance variable
of base class is to be provided through the object of derived class.
Illustration:
class ABC { class xyz extends ABC {
private int a; int x,s;
void SetA ( int a ){ void input ( int p ){
this.a = a; x = p;
} }
int GetA ( ){ int add ( ){
return a; s = x + GetA();
} return s; }
} }
TYPES OF INHERITANCE
1. Single Inheritance
if a base class is derived by a single derived class then it is called single inheritance.
METHOD OVERRIDING
Sometimes we may need to perform similar tasks in the base class as well as in the derived
class. In such situation, it will be helpful and easy to access, if the methods in both the
classes are defined with the same name. Hence the technique by virtue of which the
methods are defined with same name in the base as well as derived class is known as
‘Method Overriding’.
Invoking overridden method
to invoke a over ridden method of the base class, the keyword ‘super ‘is used.
Syntax: super. < Overridden Method of base class>
// Illustration:
import java.util.*;
class pay{
String name;double basic;
void getdata(){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the name");
name=sc.next();
System.out.println("Enter the basic");
}
void display(){
System.out.println("Name\t"+name+"\t basic pay\t"+basic);
}
}
class salary extends pay{
double hra,da,gross;
void calculate(){
da=20.0/100*basic;
hra=15.0/100*basic;
gross=hra+da+basic;
}
void display(){
System.out.println("Gross pay\t"+gross);
}
void test(){
calculate();
super.display();
display();
}
}
As soon as control encounters keyword super in the constructor of sub class, it moves to
execute super class constructor. In case super class constructor is parameterized then the
appropriate (actual) parameters need to be mentioned within braces along with super
keyword.
// Illustration:
class Employee{
String name;
Employee(String n){
name=n;
}
}
class Salary extends Employee{
String dept;double basic;
Salary(String nm,String dpt,double b){
super(nm); Invoking super class constructor Employee
dept=dpt;
basic=b;
}
}
}
PROGRAMS INVOLVING THE CONCEPT OF INHERITANCE:
A superclass Record contains names and marks of the students in two different single
dimensional arrays. Define a subclass Highest to display the names of the students
obtaining the highest mark
The details of the members of both classes are given below:
Assume that the superclass 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 superclass, main function and algorithm need NOT be written. [ICSE 2019]
A superclass Product has been defined to store the details of a product sold by a
wholesaler to a retailer. Define a subclass Sales to compute the total amount paid by the
retailer with or without fine along with service tax.
Some of the members of both classes are given below:
Assume that the superclass 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 superclass, main function and algorithm need NOT be written.
class Sales extends Product{
int day;double tax,totamt,fine;
Sales(String nm,int cod,int prc,int d){
super(nm,cod,prc);
day=d;
tax=0.0d;
totamt=0.0d;
fine=0.0d;
}
void compute(){
tax=12.4/100*prc;
if(day>30)
fine=2.5;
totamt=prc+tax+fine;
}
void show(){
super.show();
System.out.println("The Total amount payable is\t"+totamt);
}
}
A superclass 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:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx