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 on 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 tyme 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

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 to 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;
	}

}

 

Share on FacebookTweet about this on TwitterShare on LinkedInShare on Google+Digg thisPin on PinterestShare on RedditShare on StumbleUponPrint this pageEmail this to someone

Leave a Reply

Be the First to Comment!

avatar
wpDiscuz