- Multilevel inheritance has derived from the class itself and derives the subclass further.
- It means one base class derived from the intermediate class, that intermediate class derived the derived class.
Syntax:
Example:
Output:
If condition:
import java.util.Scanner;
public class get_data //Base class
{
int pin;
Scanner sc = new
Scanner(System.in);
void get_value() {
System.out.println("\n\t
Welcome to XXX Bank ");
System.out.println("Enter your pin number: ");
pin = sc.nextInt();
}
}
class compare extends get_data //Intermediate class
{
void compare_data() {
if (pin == 1234) {
System.out.println("Your pin number is valid. ");
} else {
System.out.println("Your pin number is invalid. ");
}
}
}
class cash_withdrawal extends compare //Derived class
{
int amt;
int cash;
void get_cash() {
amt = 2000;
if (pin == 1234) {
System.out.println("Enter an amount for cash withdrawal: ");
cash = sc.nextInt();
amt = amt - cash;
System.out.println("Your current balance : " + amt);
} else {
System.out.println("Please enter correct
pin number...");
}
}
}
class Multilevel_Inheritance {
public static void main(String
args[]) {
cash_withdrawal c = new
cash_withdrawal();
c.get_value();
c.compare_data();
c.get_cash();
}
}
No comments:
Post a Comment