w3resource

Java Inheritance Programming - Person Class with methods called getFirstName() and getLastName()

Java Inheritance: Exercise-7 with Solution

Write a Java program to create a class known as Person with methods called getFirstName() and getLastName(). Create a subclass called Employee that adds a new method named getEmployeeId() and overrides the getLastName() method to include the employee's job title.

Sample Solution:

Java Code:

// Person.java
// Parent class Person

public class Person {
    private String firstName;
    private String lastName;

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }
}

// Employee.java
// Child class Employee
public class Employee extends Person {
    private int employeeId;
    private String jobTitle;

    public Employee(String firstName, String lastName, int employeeId, String jobTitle) {
        super(firstName, lastName);
        this.employeeId = employeeId;
        this.jobTitle = jobTitle;
    }

    public int getEmployeeId() {
        return employeeId;
    }

    @Override
    public String getLastName() {
        return super.getLastName() + ", " + jobTitle;
    }
}
// Main.java
// Main class
public class Main {
    public static void main(String[] args) {
      Employee employee1 = new Employee("Kortney", "Rosalee", 4451, "HR Manager");
      System.out.println(employee1.getFirstName() + " " + employee1.getLastName() + " (" + employee1.getEmployeeId() + ")");
	  Employee employee2 = new Employee("Junior", "Philipa", 4452, "Software Manager");
      System.out.println(employee2.getFirstName() + " " + employee2.getLastName() + " (" + employee2.getEmployeeId() + ")");
    }
}

Sample Output:

Kortney Rosalee, HR Manager (4451)
Junior Philipa, Software Manager (4452)

Explanation:

The Person class has two private instance variables, firstName and lastName, and two public methods, getFirstName() and getLastName(), that return the values of these variables.

The Employee class is a subclass of Person, and adds two private instance variables, employeeId and jobTitle, as well as a public method called getEmployeeId(). It also overrides the getLastName() method from the Person class to include the employee's jobTitle.

In the above Main class, we create two instances of the Employee class, namely "employee1" and "employee2".

"employee1" is initialized with the values "Kortney" for the first name, "Rosalee" for the last name, 4451 for the employee ID, and "HR Manager" for the job title. Employee1's getFirstName(), getLastName(), and getEmployeeId() methods of "employee1" are called. Their return values are concatenated into a string, which is printed to the console.

Similarly, Employee2's getFirstName(), getLastName(), and getEmployeeId() methods of employee2 are called. Their return values are concatenated into a string, which is printed to the console.

Flowchart:

Flowchart: Person Class with methods called getFirstName() and getLastName().
Flowchart: Person Class with methods called getFirstName() and getLastName().
Flowchart: Person Class with methods called getFirstName() and getLastName().

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Animal Class with a method move().
Next: Create a class called Shape with methods called getPerimeter() and getArea().

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.