0% found this document useful (0 votes)
12 views35 pages

8 Inherintance

The document discusses inheritance in Java programming. Inheritance allows deriving new classes from existing classes so that the derived classes can reuse fields and methods of the base class. The key topics covered include function overriding, calling base class methods, constructors and inheritance, and different types of inheritance like single, multi-level, hierarchical and multiple inheritance.

Uploaded by

Chetan laddha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views35 pages

8 Inherintance

The document discusses inheritance in Java programming. Inheritance allows deriving new classes from existing classes so that the derived classes can reuse fields and methods of the base class. The key topics covered include function overriding, calling base class methods, constructors and inheritance, and different types of inheritance like single, multi-level, hierarchical and multiple inheritance.

Uploaded by

Chetan laddha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Java Programming | CCIT

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

Design a class Set


 Data members
 N1
 N2
 N3
 Member functions
 setData( x , y , z )
 sum( )
Then derive a new class XSet from class Set containing additional member
function mean
Class Xset extends Set
 Member function
 mean( )

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

class XSet extends Set


{
public double mean()
{
double m=sum()/3.0;
return m;
}
}
class demo
{
public static void main(String args[])
{
Set a=new Set();
a.setData(4,6,7);
System.out.println("Sum of set a is "+a.sum());
XSet b=new XSet();
b.setData(10,20,25);
System.out.println("Sum of set b is "+b.sum());
System.out.println("Mean of set b is "+b.mean());
}
}

Sum of set a is 17
Sum of set b is 55
Mean of set b is 18.33333

8
Java Programming | CCIT

Design a class Rect


 Data Members
 Length
 Breadth
 Member Functions
 setDimension( x , y )
 area( )
Then derive a new class XRect from class Rect having additional method
perimeter()
class Xrect extends Rect
 Member Functions
 perimeter ( )

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

class Xrect extends Rect


{
public void perimeter()
{
int p=2*(L+B);
System.out.println("Perimeter is "+p);
}
}
class demo
{
public static void main(String args[])
{
Rect a=new Rect();
a.setDimension(5,7);
a.area();
Xrect b=new Xrect();
b.setDimension(10,20);
b.area();
b.perimeter();
}
}

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

Design class Box


 Data members
 L
 B
 H
 Member functions
 Setdimension( x , y , z )
 Volume( ) // v = L*B*H
 surfaceArea( ) // a = 2*L*H + 2*B*H + 2*L*B

Then derive a new class OpenBox from class Box.

Class OpenBox extends Box


 Member functions
 SurfaceArea( ) // a = 2*L*H + 2*B*H + L*B

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

public void showBalance()


{
System.out.println("AccNo is "+accno);
System.out.println("Balance is "+balance);
}
}
class CAccount extends Account
{
public void deposit(int amt)
{
balance=balance+amt;
balance=balance-1;
}
public void withdraw(int amt)
{
balance = balance–amt;
balance = balance–1;
}
}
class demo
{
public static void main(String args[])
{
Account a=new Account();
a.open(4117,25000);
a.deposit(5000);
a.withdraw(2000);
a.showBalance();
CAccount b=new CAccount();
b.open(3012,10000);
b.deposit(5000);
15
Java Programming | CCIT

b.withdraw(2000);
b.withdraw(3000);
b.showBalance();
}
}

AccNo is 4117
Balance is 28000
AccNo is 3012
Balance is 9997

Calling a base class function/method


 If derived class member function wants to call a base class overrided function
then this can be achieved by using keyword super
Syntax :
 super.functionName(args);

Design 2 classes Set and XSet


 Set
 Data members
 N1
 N2
 Member functions
 setData(x , y)
 sum( )
 mean()
16
Java Programming | CCIT

 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

class XSet extends Set


{
protected int n3;
public void setData(int x,int y,int z)
{
setData(x,y);
n3=z;
}
public int sum()
{
int s=super.sum()+n3;
return s;
}
public double mean()
{
double m=sum()/3.0;
return m;
}
}
class demo
{
public static void main(String args[])
{
Set a = new Set();
a.setData(4,5 );
System.out.println("Sum of set a is "+a.sum());
System.out.println("Mean of set a is "+a.mean());
XSet b=new XSet();
b.setData(1,2,4);
System.out.println("Sum of set b is "+b.sum());
System.out.println("Mean of set b is "+ b.mean());
}
} 18
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

class student extends person


{
private int rollno;
public void setData(int rn,string nm,String ct)
{
setData(nm,ct);
rollno=rn;
}
public void showData()
{
System.out.println("RollNo is "+rollno);
super.showData();
}
}
class demo
{
public static void main(String args[])
{
Person a =new Person();
a.setData("Amit","Amravati");
a.showData();
student b=new student();
b.setData(4117,"Nitin","Nagpur");
b.showData();
}
}

Name is Amit
City is Amravati
RollNo is 4117
Name is Nitin
City is Nagpur 20
Java Programming | CCIT

Constructors and Inheritance


 Whenever an object of derived class is created :
1. The constructor of derived class is called.
2. From that constructor the base class constructor is
automatically called and executed.
3. After the base class constructor is executed program
control return to derived class constructor and then
derived class constructor code is executed.
 If base class contains a constructor with argument then
we have to explicitly call it by using keyword super.
Syntax :
super(args..);

Constructors and Inheritance


 A base class constructor can be explicitly call from derived class constructor by
using keyword super:
Syntax :
classname(datatype arg1,...)
{
super(args..);
statements
---------
}

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

Design 2 classes Set and XSet


 Set
 Data members
 N1
 N2
 Member functions
 set(x, y)
 Sum()
 Mean()
 XSet
 Data members
 N3
 Member functions
 XSet(x, y, z)
 Sum()
 Mean()

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

public double mean()


{
double m=sum()/2.0;
return m;
}
}
class XSet extends Set
{
protected int n3;
public XSet(int x,int y,int z)
{
super(x,y);
n3=z;
}
public int sum()
{
int s=super.sum()+n3;
return s;
}
public double mean()
{
double m=sum()/3.0;
return m;
}
}
class demo
{
public static void main(String args[])
{
Set a=new Set(4,5);
System.out.println("Sum of set a is "+a.sum());
System.out.println("Mean of set a is "+a.mean()); 23
Java Programming | CCIT

XSet b=new XSet(1,2,4);


System.out.println("Sum of set b is "+b.sum());
System.out.println("Mean of set b is "+b.mean()) ;
}
}

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.

Single level Inheritance


 If a class is derived from a simple base class then such type
of inheritance is called as single level inheritance.

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();
}
}

Name is Amit Jain


City is Amravati
Name is Nitin Joshi
City is Nagpur
Job is Clerk
Salary is 25700

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

public void showData()


{
super.showData();
System.out.println("Branch is "+branch);
}
}
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();
Manager c;
c=new Manager("M.Rao","Mumbai","Sr.Manager",65000,"SBI-
AMT");
c.showData();
}
}
Name is Amit Jain
City is Amravati

Name is Nitin Joshi


City is Nagpur
Job is Clerk
Salary is 25700
Name is M.Rao

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

public void payment()


{
int p=salary–advance;
System.out.println("Payment is "+p);
}
}
class Worker extends Person
{
protected int wages,wdays;
public Worker(String nm,String ct,int wg,int wd)
{
super(nm,ct);
wages=wg;
wdays=wd;
}
public void showData()
{
super.showData();
System.out.println("Wages is "+wages);
System.out.println("Wdays is "+wdays);
}
public void payment()
{
int p=wages*wdays;
System.out.println("Payment is "+p);
}
}

33
Java Programming | CCIT

Name is Amit Jain


City is Amravati

Name is Nitin Joshi


City is Nagpur
Job is Clerk
Salary is 25700
Advance is 5000
Payment is 20700

Name is Raja
City is Raipur
Wages is 500
Wdays are 20
Payment is 10000

34
Java Programming | CCIT

35

You might also like