Advertisement

header ads

Java OOP Abstraction - A simple tutorial



Abstraction is simply hiding the internal process of a certain class and providing only the final output. This java abstraction tutorial provides a simple yet strong introduction to abstraction in java. Abstraction in java can be divide in to two main categories.

We will discuss about interfaces in another tutorial.

In Abstract classes,


  • Use abstract keyword
  • May contains abstract and none abstract methods
  • Abstract classes cannot be instantiated
  • To use an abstract class, it should be inherited from another class and provide implementations of all abstract methods in it.


Abstract methods

  • Contains abstract keyword
  • Does not contain a method body
  • Ends with semicolon (;)


Consider following code sample.
1
2
3
4
public abstract class MyClass { // abstract class
 
 abstract void myMethod(); // abstract method
}

Now, refer following example to get an overall idea about abstraction in java.

This my project hierarchy.

This is Car.java class

  • This is an abstract class, therefore I'm using abstract keyword.
  • It has an abstract method 'createCar' (line 12). In eclipse: press Alt+Shift+S and click on 'generate constructor using fields' to add constructor.
  • Also Car class contains getters and setters for carModel variable. In eclipse: press Alt+Shift+S and click on 'Generate getters and setters' to add those.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.example.abstraction;

public abstract class Car {
 private String carModel;

 public Car(String carModel) {

  this.carModel = carModel;
  System.out.println("Creating a car: " + getCarModel());
 }

 abstract void createCar();

 public String getCarModel() {
  return carModel;
 }

 public void setCarModel(String carModel) {
  this.carModel = carModel;
 }

}

This is Lamborghini.java class

  • Its an inherited class from Car.java class.
  • It has implemented 'createCar' method which is an abstract method in Car.java class (line 11). Also we use 'getCarModel()' to acquire the value for 'carModel' String
  • It has its default constructor (line 5)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
package com.example.abstraction;

public class Lamborghini extends Car {

 public Lamborghini(String carModel) {
  super(carModel);
  // TODO Auto-generated constructor stub
 }

 @Override
 void createCar() {
  System.out.println("Car created: " + getCarModel());
  // TODO Auto-generated method stub

 }

}

This is Main.java class

  • It has main method and instantiated Lamborghini class with Car reference and has passes the 'Lamborghini Avantador' value as 'carModel' string.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package com.example.abstraction;

public class Main {

 public static void main(String[] args) {
  Car car = new Lamborghini("Lamborghini Avantador");
  car.createCar();

 }
}

When we run the Main.java class. We can have the following output.
Creating a car: Lamborghini Avantador
Car created: Lamborghini Avantador

Now you can understand that abstraction is simply hiding the internal process of a class and showing only the functional output. Getters and setters are used to acquire and set values for fields. This is an introduction to abstraction with a simple but effective example. You can try more complex examples once you have got the idea behind this concept.

Share this tutorial among friends and meet you again with another tutorial. Like our facebook page to stay up to date our latest tutorials.

Post a Comment

0 Comments