8 Inherintance
8 Inherintance
1
Java Programming | CCIT
Table of Contents
Inheritance .......................................................... 3
Inheritance ......................................................... 3
Function/Method Overriding ............................ 11
Calling a base class function/method ................ 16
Constructors and Inheritance ........................... 21
Types of Inheritance ......................................... 24
Single level Inheritance ..................................... 24
Multi-level Inheritance ..................................... 24
Hierarchical Inheritance ................................... 25
Multiple Inheritance ......................................... 25
2
Java Programming | CCIT
Inheritance
Inheritance
It is a technique of deriving new classes from existing classes.
The derived class inherits all features of base class
from which it is derived.
In the derived class we only need to define additional
features or the changes that we require.
So by using this technique we can reuse our existing
code.
This is called as code reusability.
A class can be derived from existing class by using
keyword extends.
Syntax :
class DerivedClassName extends BaseClassName
{
-----//members of the derived class
------
}
Due to this the derived class will inherit all features of base class from which it is
derived.
3
Java Programming | CCIT
Class Account
Data members
• Accno
• Balance
Member functions
• Open( accno , bal )
• Deposit( amt )
• Withdraw( amt )
• ShowBalance( )
Then derive a new class SAccount from class Account with addational member
function addIntrest.
Class SAccount extends Account
Member functions
• addIntrest( irate , years )
4
Java Programming | CCIT
class Account
{
protected int accno;
protected double balance;
public void open(int an,int bl)
{
accno=an;
balance=bl;
}
public void deposit(int amt)
{
balance=balance+amt;
}
public void withdraw(int amt)
{
balance=balance–amt;
}
public void showBalance()
{
System.out.println("AccNo is " +accno);
System.out.println("Balance is "+balance);
}
}
class SAccount extends Account
{
public void addInterest(double r,int n)
{
double si=balance*r*n/100;
balance=balance+si;
}
}
5
Java Programming | CCIT
class demo
{
public static void main(String args[])
{
Account a=new Account();
a.open(4117,25000);
a.deposit(5000);
a.withdraw(2000);
a.showBalance();
SAccount b=new SAccount();
b.open(3012,10000);
b.deposit(2000);
b.showBalance();
b.addInterest(10.25,3);
b.showBalance();
}
}
AccNo is 4117
Balance is 28000
AccNo is 3012
Balance is 12000
AccNo is 3012
Balance is 14750
6
Java Programming | CCIT
class Set
{
protected int n1,n2,n3;
public void setData(int x,int y,int z)
{
n1=x;
n2=y;
n3=z;
}
public int sum()
{
int s=n1+n2+n3;
return s;
}
}
7
Java Programming | CCIT
Sum of set a is 17
Sum of set b is 55
Mean of set b is 18.33333
8
Java Programming | CCIT
class Rect
{
protected int L,B;
public void setDimension(int m,int n)
{
L=m;
B=n;
}
public void area()
{
int a=L*B;
System.out.println("Area is "+a);
}
}
9
Java Programming | CCIT
Area is 35
Area is 200
Perimeter is 60
10
Java Programming | CCIT
Function/Method Overriding
Redefining a base class function/method in derived class is called as function /
method overriding.
The overrided function must have
-same function name
-Same no of arguments
-Same type of arguments
-Same sequence of arguments
Similar to base class function
This overrided function will be call for derived class object instead of base class
inherited function
11
Java Programming | CCIT
class Box
{
protected int L,B,H;
public void setDimension(int x,int y,int z)
{
L=x;
B=y;
H=z;
}
public void volume()
{
int v=L*B*H;
System.out.println("Volume is "+v);
}
public void surfaceArea( )
{
int a=2*L*H+2*B*H+2*L*B;
System.out.println("Surface Area is "+a);
}
}
class OpenBox extends Box
{
public void surfaceArea()
{
int a=2*L*H+2*B*H+L*B;
System.out.println("Surface Area is "+a);
}
}
12
Java Programming | CCIT
class demo
{
public static void main(String args[])
{
Box a=new Box();
a.setDimension(2,2,2);
a.volume();
a.surfaceArea();
OpenBox b = new OpenBox();
b.setDimension(1,1,1);
b.volume();
b.surfaceArea();
}
}
Volume is 8
Surface Area is 24
Volume is 1
Surface Area is 5
13
Java Programming | CCIT
Class Account
Data members
Accno
Balance
Member functions
Open( accno , bal )
Deposit( amt )
Withdraw( amt )
ShowBalance( )
Then derive a new class CAccount from class Account which will charge Rs 1 for
each transaction.
Class Caccount extends Account
Member functions
Deposit( amt )
Withdraw( amt )
class Account
{
protected int accno;
protected double balance;
public void open(int an,int bl)
{
accno=an;
balance=bl;
}
public void deposit(int amt)
{
balance=balance+amt;
}
public void withdraw(int amt)
{
balance=balance–amt; 14
}
Java Programming | CCIT
b.withdraw(2000);
b.withdraw(3000);
b.showBalance();
}
}
AccNo is 4117
Balance is 28000
AccNo is 3012
Balance is 9997
XSet
Data members
N3
Member functions
setData(x , y)
sum( )
mean()
Note: sum( ) and mean( ) must return a value.
class Set
{
protected int n1,n2;
public void setData(int x,int y)
{
n1=x;
n2=y;
}
public int sum()
{
int s=n1+n2;
return s;
}
public double mean()
{
double m=sum()/2.0;
return m;
}
}
17
Java Programming | CCIT
Sum of set a is 9
Mean of set a is 4.5
Sum of set b is 7
Mean of set b is 2.333333
Design 2 classes Person and Student
Person
Data members
Name
City
Member functions
setData(n,c)
showdata()
Student
Data members
RollNo
Member functions
setData(rn ,nm, ct)
showdata()
class person
{
private String name,city;
public void setData(String nm,String ct)
{
name=nm;
city=ct;
}
public void showData()
{
System.out.println("Name is "+name);
System.out.println("City is "+city);
}
19
}
Java Programming | CCIT
Name is Amit
City is Amravati
RollNo is 4117
Name is Nitin
City is Nagpur 20
Java Programming | CCIT
NOTE: Call to base class constructor by using keyword super must be the first
statement in derived class constructor.If base class contains a constructor and it
doesn't have any default constructor then we have to compulsory call the base
class constructor by using keyword super.
21
Java Programming | CCIT
class Set
{
protected int n1,n2;
public Set(int x,int y)
{
n1=x;
n2=y;
}
public int sum()
{
int s=n1+n2;
return s;
}
22
Java Programming | CCIT
Sum of set a is 9
Mean of set a is 4.5
Sum of set b is 7
Mean of set b is 2.333333
Types of Inheritance
We can derive our class from existing classes in different ways.
Multi-level Inheritance
If a class is derived from a derived class then such type of
inheritance is called as multi-level inheritance.
24
Java Programming | CCIT
Hierarchical Inheritance
If multiple classes are derived from a single
base class then such type of inheritance is
called as Hierarchical inheritance.
Multiple Inheritance
If a class is derived from a multiple base classes
then such type of inheritance is called as multiple
inheritance.
NOTE: Java doesn't support multiple Inheritance for classes but it can be achieved
through Interfaces.
Design 2 classes Person and Employee
Person
Data members
Name
City
Member functions
Person(nm, ct)
Showdata()
Employee
Data members
Job
Salary
Member functions
Employee(nm, ct, jb, sl)
Showdata()
25
Java Programming | CCIT
class Person
{
protected String name,city;
public Person(String nm,String ct)
{
name=nm;
city=ct;
}
public void showData()
{
System.out.println("Name is "+name);
System.out.println("City is "+city);
}
}
class Employee extends Person
{
protected String job;
protected int salary;
public Employee(String nm,String ct,String jb,int sl)
{
super(nm,ct);
job=jb;
salary=sl;
}
public void showData()
{
super.showData();
System.out.println("Job is "+job);
System.out.println("Salary is "+salary);
}
}
26
Java Programming | CCIT
class demo
{
public static void main(String args[])
{
Person a=new Person("Amit Jain","Amravati");
a.showData();
Employee b;
b=new Employee("Nitin Joshi","Nagpur","Clerk",25700);
b.showData();
}
}
27
Java Programming | CCIT
Design 3 classes Person , Employee and Manager from given inheritance diagram
Person
Data members
Name
City
Member functions
Person(n, c)
Showdata()
Employee
Data members
Job
Salary
Member functions
Employee(n, c, j, s)
Showdata()
Manager
Data members
Branch
Member functions
Manager(n, c, j, s, b)
Showdata()
class Person
{
protected String name,city;
public Person(String nm,String ct)
{
name=nm;
city=ct;
}
public void showData()
{
System.out.println("Name is "+name); 28
Java Programming | CCIT
System.out.println("City is "+city);
}
}
class Employee extends Person
{
protected String job;
protected int salary;
public Employee(String nm,String ct,String jb,int sl)
{
super(nm,ct);
job=jb;
salary=sl;
}
public void showData()
{
super.showData();
System.out.println("Job is "+ job);
System.out.println("Salary is "+salary);
}
}
class Manager extends Employee
{
protected String branch;
public Manager(String nm,String ct,String jb,int sl,
String br)
{
super(nm,ct,jb,sl);
branch=br;
}
29
Java Programming | CCIT
City is Mumbai
Job is Sr.Manager
Salary is 65000 30
Branch is SBI-AMT
Java Programming | CCIT
Design 3 classes Person Employee and Worker from following inheritance Diagram
class demo
{
public static void main(String args[])
{
Person a=new Person("Amit Jain","Amravati");
a.showData();
Employee e;
e=new Employee("Nitin Joshi","Nagpur","Clerk",25700
5000);
e.showData();
e.payment();
Worker w;
w=new Worker("Raja","Raipur",500,20);
w.showData();
w.payment();
}
}
31
Java Programming | CCIT
class Person
{
protected String name,city;
public Person(String nm,String ct)
{
name=nm;
city=ct;
}
public void showData()
{
System.out.println("Name is "+name);
System.out.println("City is "+city);
}
}
class Employee extends Person
{
protected String job ;
protected int salary , advance ;
public Employee(String nm,String ct,String jb,int sl,int
adv)
{
super(nm,ct);
job=jb;
salary=sl;
advance=adv;
}
public void showData()
{
super.showData();
System.out.println("Job is "+job);
System.out.println("Salary is "+salary);
}
32
Java Programming | CCIT
33
Java Programming | CCIT
Name is Raja
City is Raipur
Wages is 500
Wdays are 20
Payment is 10000
34
Java Programming | CCIT
35