You are on page 1of 18

LECTURE 9

INHERITANCE

Swapnali Kurhade
Sardar Patel Institute of Technology
INHERITANCE
INHERITANCE

 Inheritance can be defined as the process


where one class acquires the properties
(methods and fields) of another.
 With the use of inheritance the information is
made manageable in a hierarchical order.
 Reusability: Inheritance supports the concept of
“reusability”, i.e. when we want to create a new
class and there is already a class that includes
some of the code that we want, we can derive
our new class from the existing class. By doing
this, we are reusing the fields and methods of
the existing class.
IMPORTANT TERMINOLOGY:

 Super Class: The class whose features are inherited


is known as super class(or a base class or a parent
class).
 Sub Class: The class that inherits the other class is
known as sub class(or a derived class, extended
class, or child class). The subclass can add its own
fields and methods in addition to the superclass
fields and methods.
T YPES OF INHERITANCE

Single Inheritance
Multilevel Inheritance
“Multiple Inheritance”
Hierarchical Inheritance
“Hybrid Inheritance”
EXTENDS KEYWORD

extends is the keyword used to inherit the


properties of a class.

class Super{ .....


..... }

class Sub extends Super{ .....


..... }
SINGLE INHERITANCE

 When a class extends another class only then


we call it a single inheritance.
 Here A is a parent class of B and B is a child class of
A.
SINGLE INHERITANCE

class herit{
int a=10; O/P:
} 10

public class JavaApplication26 extends herit{

public static void main(String[] args) {


JavaApplication26 ob1 = new JavaApplication26 ();
System.out.println(ob1.a);
}
}
MULTILEVEL INHERITANCE

In Multilevel inheritance one can inherit


from a derived class, thereby making
this derived class the base class for the
new class
MULTILEVEL INHERITANCE

class grandpa{
O/P:
int age=90;
Gradfather age=90
} Father age=60
class pa extends grandpa{
int age1=60;
}
public class JavaApplication26 extends pa{
public static void main(String[] args) {
JavaApplication26 ob1 = new JavaApplication26 ();
System.out.println ("Gradfather
age="+ob1.age+"\nFather age="+ob1.age1);
}
}
HIERARCHICAL INHERITANCE

 In such kind of inheritance one class is inherited by


many sub classes.

arts, science and commerce class are


inheriting student class
HIERARCHICAL INHERITANCE

class student{
Hey you are a student
void display()
Welcome
{System.out.println ("Hey you are a student");} Hey you are a student
} Welcome to Science
class ar ts extends student{
void display1() { display(); System.out.println ("Wel come to ar ts");}
}
class science extends student {
void display2() { display();
System.out.println ("Welcome to Science");}
}
public class JavaApplicati on26 {
public static void main(String[] args) {
ar ts ob1=new ar ts (); ob1 .display1 ();
science ob2=new science(); ob2.display2();
} }
MULTIPLE INHERITANCE

“Multiple Inheritance” refers to the concept of


one class extending (or inherits) more than
one base class.
 The problem with “multiple inheritance” is
that the derived class will have to manage the
dependency on two base classes.
HYBRID INHERITANCE

In simple terms you can say that Hybrid


inheritance is a combination of Single and
Multiple inheritance.
A hybrid inheritance can be achieved in the
java in a same way as
multiple inheritance can be!!
QUIZ TIME….

 Say that there are three classes: Computer,


AppleComputer, and IBMComputer. What are the
likely relationships between these classes?
a) Computer is the superclass, AppleComputer and
IBMComputer are subclasses of Computer.
b) IBMComputer is the superclass, AppleComputer and
Computer are subclasses of IBMComputer.
c) Computer, AppleComputer and IBMComputer are
sibling classes.
d) Computer is a superclass, AppleComputer is a
subclasses of Computer, and IBMComputer is a
sublclas of AppleComputer
QUIZ TIME….

 Does a subclass inherit both member variables and


methods?
a) No--only member variables are inherited.
b) No--only methods are inherited.
c) Yes--both are inherited.
d) Yes--but only one or the other are inherited.
LET’S CODE….

 Consider a scenario, Bank is a class that provides


functionality to get rate of interest. But, rate of
interest varies according to banks. For example, SBI,
ICICI could provide 8% and 9% rate of interest. Use
method overriding to calculate Simple Interest
(P*R*T/100).
 Hint- there is a hierarchical inheritance. Here bank is
the super class and where rate of interest is passed
to the each bank accordingly. It uses principal and
time from the bank class.

You might also like