X

Java Abstraction Example

This example demonstrates the usage of abstraction in Java programming language

What is Abstraction

Abstraction is a process of hiding the implementation details from the user. Оnly the functionality will be provided to the user. In Java, abstraction is achieved using abstract classes and interfaces. We have a more detailed explanation on Java Interfaces, if you need more info about Interfaces please read this tutorial first.

Abstraction is one of the four major concepts behind object-oriented programming (OOP). OOP questions are very common in job interviews, so you may expect questions about abstraction on your next Java job interview.



Java Abstraction Example

To give an example of abstraction we will create one superclass called Employee and two subclasses –  Contractor and FullTimeEmployee. Both subclasses have common properties to share, like the name of the employee and the the amount of money the person will be paid per hour. There is one major difference between contractors and full-time employees – the time they work for the company. Full-time employees work constantly 8 hours per day and the working time of contractors may vary.

Java abstract class example

Lets first create the superclass Employee. Note the usage of abstract keyword in class definition. This marks the class to be abstract, which means it can not be instantiated directly. We define a method called calculateSalary() as an abstract method. This way you leave the implementation of this method to the inheritors of the Employee class.

package net.javatutorial;

public abstract class Employee {
	
	private String name;
	private int paymentPerHour;
	
	public Employee(String name, int paymentPerHour) {
		this.name = name;
		this.paymentPerHour = paymentPerHour;
	}
	
	public abstract int calculateSalary();

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPaymentPerHour() {
		return paymentPerHour;
	}

	public void setPaymentPerHour(int paymentPerHour) {
		this.paymentPerHour = paymentPerHour;
	}

}

The Contractor class inherits all properties from its parent Employee but have to provide it’s own implementation of calculateSalary() method. In this case we multiply the value of payment per hour with given working hours.

package net.javatutorial;

public class Contractor extends Employee {
	
	private int workingHours;

	public Contractor(String name, int paymentPerHour, int workingHours) {
		super(name, paymentPerHour);
		this.workingHours = workingHours;
	}

	@Override
	public int calculateSalary() {
		return getPaymentPerHour() * workingHours;
	}

}

The FullTimeEmployee also has it’s own implementation ofcalculateSalary()method. In this case we just multiply by constant 8 hours.

package net.javatutorial;

public class FullTimeEmployee extends Employee {

	public FullTimeEmployee(String name, int paymentPerHour) {
		super(name, paymentPerHour);
	}

	@Override
	public int calculateSalary() {
		return getPaymentPerHour() * 8;
	}

}

 

3.6 7 votes
Article Rating
filip:

View Comments (6)

  • if i am not wrong according to you FullTimeEmployee achieve abstraction.but i confused how this guy can achieve abstraction. because he already know what to implement calculateSalary () in FullTimeEmployee .

    • calculateSalary() is an abstract method in the parent class - Employee, so the ancestors like FullTimeEmployee have to do their own implementation of this method. Employee does not know what is the implementation in FullTimeEmployee and that's the abstraction

    • Wrong. Abstract methods do not have functionality at all. They just declare the signature of the method and the implementing class should care about the functionality. So abstraction is not inheritance, because when we talk about inheritance, this means you inherit the functionality :)

  • i don't understand why you need to use abstract class in every abstraction example.
    When both are totally different?

Related Post