Monday, 28 November 2016

Program to implement the Multiple Inheritance (Bank Interface, Customer & Account classes)

In this program, we have achieved multiple inheritance using interface. We have created an interface Bank having data members rate, no_of_years and method show( ). After that class Customer is created which has data members cust_name, cust_id and method display( ). Finally we declare the Account class having data members acc_no, acc_bal and method interest( ). In that we have extended Customerclass and implemented Bank interface as shown in program.


PROGRAM
/* Program to implement the Multiple Inheritance */

interface Bank {
 
 float rate = 12.0f;
 int no_of_years=3;
 void show();
}

class Customer {
 
 String cust_name;
 int cust_id;
 Customer(String n,int i) {
  
  cust_name = n;
  cust_id = i;
 }
 
 void display() {
  
  System.out.println("Customer Name = "+cust_name);
  System.out.println("Customer Id = "+cust_id);
 }
}

class Account  extends Customer implements Bank {
 
 int acc_no;
 float acc_bal;
 Account(String n,int b,int x,float y) {
  
  super(n,b);
  acc_no=x;
  acc_bal=y;
 }
 
 public void show() {
  
  display();
  System.out.println("Account No. = "+acc_no);
  System.out.println("Account Balance = "+acc_bal);
 }
 
 void interest() {
  
  show();
  float intr = (rate*acc_bal*no_of_years)/100;
  System.out.println("Interest = "+intr);
 }
}

public class Acct_Details {

 public static void main (String[] args) 
 {
  Account ac = new Account("Sameer",8,4052,5000);
  ac.interest();
 }
}
OUTPUT
C:\>javac Acct_Details.java
C:\>java Acct_Details
Customer Name = Sameer
Customer Id = 8
Account No. = 4052
Account Balance = 5000.0
Interest = 1800.0

2 comments: