Q:

Java program to create an employee class by inheriting Person class

belongs to collection: Java Inheritance Programs

0

Java program to create an employee class by inheriting Person class

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this program, we will create a Person class and then create a new class Employee by inheriting the features of the Person class using the "extends" keyword.

Program/Source Code:

The source code to create an employee class by inheriting the Person class is given below. The given program is compiled and executed successfully.

// Java program to create an employee class 
// by inheriting Person class

class Person {
  String name;
  int age;

  Person(int age, String name) {
    this.name = name;
    this.age = age;
  }
}

class Employee extends Person {
  int emp_id;
  int emp_salary;

  Employee(int id, String name, int age, int salary) {
    super(age, name);
    emp_id = id;
    emp_salary = salary;
  }

  void printEmployeeDetails() {
    System.out.println("Employee ID     :  " + emp_id);
    System.out.println("Employee Name   :  " + name);
    System.out.println("Employee Age    :  " + age);
    System.out.println("Employee Salary :  " + emp_salary);
  }
}

public class Main {
  public static void main(String[] args) {
    Employee emp = new Employee(101, "Savas Akhtan", 32, 22340);
    emp.printEmployeeDetails();
  }
}

Output:

Employee ID     :  101
Employee Name   :  Savas Akhtan
Employee Age    :  32
Employee Salary :  22340

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java program to override a base class method into ... >>
<< Java program to call the method with the same name...