Multilevel inheritance in java

Multilevel inheritance:

  • 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:
  1. class a
    {
             //Base class
    }
    class b extends class a
    {
             //Intermediate class

    }
    class c extends class b
    {
              //Derived class
    }
    Object creation:
    Derived_class_name object_name;
    object_name.function(parameter list);
Example:
  1. 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();

        }

    }

Output:
If condition:
Else condition:
Share:

No comments:

Post a Comment

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me