Friday, 25 November 2016

Program to implement the following Multi Level Inheritance

In multilevel inheritance the subclass is super class to another class. The syntax for multilevel inheritance is given below:
class   <superclass-name> {
    // Body of superclass
}

class   <subclass-name1>   extends   <superclass-name> {
 // Body of subclass
}

class   <subclass-name2>   extends   <subclass-name1> {
 // Body of subclass
}


In this program, we have created three classes from which Account is a superclass for Saving_Acc and the Saving_Acc is the superclass for Acct_Details. In main( ) method we have created an object Acct_Details and pass the values to constructor then called the display( ) method. Here we used the super keyword to call parent class constructor and method.

Note that after compiling the MultiLevelInh.java file the four .class files will be created "Acoount.class", "Saving_Acc.class", "Acct_Details.class" and "MultiLevelInh.class".


PROGRAM
import java.lang.*;

class Account {
 
 String cust_name;
 int acc_no;
 
 Account(String a, int b) {
  
  cust_name=a;
  acc_no=b;
 }
 
 void display() {
  
  System.out.println ("Customer Name: "+cust_name);
  System.out.println ("Account No: "+acc_no);
 }
}

class Saving_Acc extends Account {

 int min_bal,saving_bal;
 
 Saving_Acc(String a, int b, int c, int d) {
  
  super(a,b);
  min_bal=c;
  saving_bal=d;
 }
 
 void display() {
  
  super.display();
  System.out.println ("Minimum Balance: "+min_bal);
  System.out.println ("Saving Balance: "+saving_bal);
 }
}

class Acct_Details extends Saving_Acc {
 
 int deposits, withdrawals;
 
 Acct_Details(String a, int b, int c, int d, int e, int f) {
  
  super(a,b,c,d);
  deposits=e;
  withdrawals=f;
 }
 
 void display() {
  
  super.display();
  System.out.println ("Deposit: "+deposits);
  System.out.println ("Withdrawals: "+withdrawals);
 }
}

public class MultiLevelInh {

 public static void main(String[] args) {
  
  Acct_Details A = new Acct_Details("Pankaj",666,1000,5000,500,9000);
  A.display();
 }
}
OUTPUT
C:\>javac MultiLevelInh.java
C:\>java MultiLevelInh
Customer Name: Pankaj
Account No: 666
Minimum Balance: 1000
Saving Balance: 5000
Deposit: 500
Withdrawals: 9000

No comments:

Post a Comment