Single inheritance in java

 Single inheritance:

  • If one base class and one derived class in a program called  “Single inheritance”.
    Syntax:
    1. class a
      {
                //Base class
      }
      class b: public a
      {
                //Derived class
      }
      Object creation:
      Derived_class_name object_name;
      object_name.function(parameter list);
    Example:
    1. import java.util.Scanner;

      class emp_data {

          int empno, sal;

          String ename;

          Scanner sc = new Scanner(System.in);

          void get_data() {

              System.out.println("Enter employee id: ");

              empno = sc.nextInt();

              System.out.println("Enter employee name: ");

              ename = sc.next();

              System.out.println("Enter employee salary: ");

              sal = sc.nextInt();

          }

      }

       

      class emp_sal extends emp_data {

          void salary_increment() {

              if (sal <= 5000) {

                  sal = sal + 1000;

                  System.out.println("Employee salary has been increased by 1000");

              } else if (sal >= 5000 && sal <= 10000) {

                  sal = sal + 2000;

                  System.out.println("Employee salary has been increased by 2000");

              } else {

                  sal = sal + 3000;

                  System.out.println("Employee salary has been increased by 3000");

              }

          }

          void display() {

              System.out.println("\nEmployee Details.......");

              System.out.println("Employee ID : " + empno);

              System.out.println("Employee name: " + ename);

              System.out.println("Employee sal: " + sal);

          }

      }

      class Single_inheritance {

          public static void main(String args[]) {

              emp_sal e = new emp_sal();

              e.get_data();

              e.salary_increment();

              e.display();

          }

      }

    Output:
    If condition:
    Else if condition:
    Else condition:
    Share: 
    Share:

    No comments:

    Post a Comment

    Recent Posts

    Service Support

    Need our help to Learn or Post New Concepts Contact me