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:



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?
Java: Tips of the Day
Hashset vs Treeset:
HashSet is much faster than TreeSet (constant-time versus log-time for most operations like add, remove and contains) but offers no ordering guarantees like TreeSet.
HashSet
- the class offers constant time performance for the basic operations (add, remove, contains and size).
- it does not guarantee that the order of elements will remain constant over time
- iteration performance depends on the initial capacity and the load factor of the HashSet.
- It's quite safe to accept default load factor but you may want to specify an initial capacity that's about twice the size to which you expect the set to grow.
TreeSet
- guarantees log(n) time cost for the basic operations (add, remove and contains)
- guarantees that elements of set will be sorted (ascending, natural, or the one specified by you via its constructor) (implements SortedSet)
- doesn't offer any tuning parameters for iteration performance
- offers a few handy methods to deal with the ordered set like first(), last(), headSet(), and tailSet() etc
Important points:
- Both guarantee duplicate-free collection of elements
- It is generally faster to add elements to the HashSet and then convert the collection to a TreeSet for a duplicate-free sorted traversal.
- None of these implementations are synchronized. That is if multiple threads access a set concurrently, and at least one of the threads modifies the set, it must be synchronized externally.
- LinkedHashSet is in some sense intermediate between HashSet and TreeSet. Implemented as a hash table with a linked list running through it, however,it provides insertion-ordered iteration which is not same as sorted traversal guaranteed by TreeSet.
So a choice of usage depends entirely on your needs but I feel that even if you need an ordered collection then you should still prefer HashSet to create the Set and then convert it into TreeSet.
- e.g. SortedSet<String> s = new TreeSet<String>(hashSet);
Ref: https://bit.ly/3d3waGh
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook