You are on page 1of 6

Experiment No.

:8
Aim: To Write Java Program on inheritance

Input Specification: name of type string ,account no of type int,penalty,minimum balance ,


opening balance, interest rate of type float
Output Specification:. name of type string,account balance of type float and check book facility of
type boolean.

Objectives:-Student/we will learn to use the inheritance in the java program.

.
Theory:

Inheritance in Java

It is the mechanism in java by which one class is allowed to inherit the features(fields and
methods) of another class.
• Super Class: The class whose features are inherited is known as superclass(or a base class
or a parent class).
• Sub Class: The class that inherits the other class is known as a subclass(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.
• 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.

How to use inheritance in Java


The keyword used for inheritance is extends.

Syntax :
class derived-class extends base-class
{
//methods and fields
}
Types of Inheritance in Java
Below are the different types of inheritance which are supported by Java.
1. Single Inheritance: In single inheritance, subclasses inherit the features of one superclass.

2. Multilevel Inheritance: In Multilevel Inheritance, a derived class will be inheriting a base


class and as well as the derived class also act as the base class to other class..
3. Hierarchical Inheritance: In Hierarchical Inheritance, one class serves as a superclass (base
class) for more than one subclass..
4. Multiple Inheritance (Through Interfaces): In Multiple inheritances, one class can have
more than one superclass and inherit features from all parent classes. Please note that Java
does not support multiple inheritances with classes. In java, we can achieve multiple inheritances
only through Interfaces.
5. Hybrid Inheritance(Through Interfaces): It is a mix of two or more of the above types of
inheritance. Since java doesn’t support multiple inheritances with classes, hybrid inheritance is
also not possible with classes. In java, we can achieve hybrid inheritance only through Interfaces.

logic of the program.


In this program of inheritance, class bank is defined. Current account and saving account
are extended from bank class. In current account class, read(),deposit(),display() and minimum()
methods are defined. In saving account class, read(),deposit(),display(),withdraw() and
interestAdd()methods are defined.in main method class account, objects of current account and
saving account.

Algorithm:

Step 1: Start

Step 2: Create a class named bank

Step 3: Extend current account from bank.

Step 4: define read(),deposit(),display() and minimum() methods.

Step 5: Extend saving account from bank

Step 6: define read(),deposit(),display(),withdraw() and interestAdd()

Step 7: Create main method class named account.

Step 8: Create objects of current account and saving account.

Step 9: call the methods read(),deposit(),withdraw(),display() for objects of current class and

saving account.

Step 10:Stop
Program(Code):

// program no.8 :proogram on inheritance

import java.util.*;

class bank
{
protected String name;
protected int acc_no;
protected String type;
protected boolean cheque;
protected float penalty;
protected float min_balance;
protected float balance;
protected float interest;
}
class current_acc extends bank
{
public void read()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Name,account no.,penalty,minimum balance and opening

balance of the current account holder:");


name=sc.next();
acc_no=sc.nextInt();
penalty=sc.nextInt();
min_balance=sc.nextInt();
balance=sc.nextInt();
type="Current";
cheque=true;
interest=0;
}

public void deposit(float x)


{
balance+=x;
}

public void display()


{
System.out.println("Account Details:\nName:"+name+"\nAccount no:"+acc_no

+"\nBalance:"+balance+"\nCheque book facility:"+cheque);


}
public void withdraw(float x)
{
balance-=x;
}

public void minimum()


{
if(balance<min_balance)
balance-=penalty;
System.out.println("Panalty imposed");
}
}

class savings_acc extends bank


{
public void read()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Name,account no.,interest rate and opening balance of the

savings account holder:");


name=sc.next();
acc_no=sc.nextInt();
penalty=0;
min_balance=0;
interest=sc.nextFloat();
balance=sc.nextInt();
type="Savings";
cheque=false;

public void deposit(float x)


{
balance+=x;
}

public void display()


{
System.out.println("Account Details:\nName:"+name+"\nAccount no:"+acc_no

+"\nBalance:"+balance+"\nCheque book facility:"+cheque);


}
public void withdraw(float x)
{
balance-=x;
}
public void interestAdd()
{

balance=balance+balance*interest;
System.out.println("Interest Added");
}
}

class account
{
public static void main(String[] args)throws Exception
{
int n,i;
current_acc c =new current_acc();
c.read();
c.deposit(10000);
c.withdraw(2000);
c.display();
savings_acc s =new savings_acc();
s.read();
s.deposit(10000);
s.withdraw(2000);
s.display();

}
}

Output:

/*
D:\programs>javac account.java

D:\programs>java account
Enter Name,account no.,penalty,minimum balance and opening balance of the curren
t account holder:
sachin
151
2000
1000
10000
Account Details:
Name:sachin
Account no:151
Balance:18000.0
Cheque book facility:true
Enter Name,account no.,interest rate and opening balance of the savings account
holder:
samar
2001
9
60000
Account Details:
Name:samar
Account no:2001
Balance:68000.0
Cheque book facility:false

D:\programs>

Outcome: with this we/student will able to use property of resability through inheritance t in the
java program.

You might also like