Issue Of Not Using Abstraction In Java And Its Solution

See the issue when you don’t use abstraction concept in java and its solution with a program example and explanation.

First, we will create a program in which abstraction is not applied and discuss the problem because of it. The, we will see the solution of the problem with same program example by applying abstraction.

Program: Writing on a board with a marker of different colors.

Program without abstraction concept

In below program, we have a class- BlueMarker. And the marker is to be used in another Class “Board” to write using its write() method that accept Marker object.

The initial code without using abstraction can be written as below. Have a quick overview of the program and the understand the issue after that.

public class Sample{

	public static void main(String[] args) {

		BlueMarker blueMarker = new BlueMarker();
		Board board = new Board();
		board.write(blueMarker);
	}
}

class Board {
	public void write(BlueMarker b) {
		b.color();
	}

}

class BlueMarker  {

	public void color() {

		System.out.println("blue..");
	}
}

Output:
blue…

Issue on not applying abstraction

Now, lets see what are issues in above example. Look at the Board class write method. It accepts BlueMarker object and write with blue color.

class Board {
	public void write(BlueMarker b) {
		b.color();
	}

}

What if we want to write with different color marker on the board e.g. with red color or something?

Surely, we need to change the board class.

Would you really like to change the board every time when you use marker of different colors? You can think of efforts, cost and time changing the board.

So, this is not a flexible solution.

SOLUTION

We can solve the problem using abstraction concept. In brief, abstraction is a concept which tells that you should provide only necessary information to user and hide the rest.

Just think, what if the Board does not know about the Blue, green or red marker or say it does not know about their implementation. In other word, what if the different colors of marker are abstracted from the board and the board does not know anything about them.

Let’s abstract all colors types classes using an interface in java program.

Create an interface Marker and the BlueMarker class will implement it.

interface Marker {

	public abstract void color();
}

class BlueMarker implements Marker {

	public void color() {

		System.out.println("blue..");
	}
}

Now change the class Board write method and pass the interface reference into it.

class Board {
	public void write(Marker c) {
		c.color();
	}
}

Let us test the program now. We can create a Marker interface reference and create  the object of the class BlueMarker and assign to marker reference . Pass it into the Board class write method.

public class Sample {

	public static void main(String[] args) {
		
		//Create object of blue marker
		Marker blueMarker = new BlueMarker();		
		Board board = new Board();
		board.write(blueMarker);
	}	
}

TEST

Now, lets us check if the changing of Board class is required or not when we introduce a new color marker.

Create another marker of red color that implements RedMarker.

class RedMarker implements Marker{

	public void color() {

		System.out.println("red..");
	}
}

Let’s write code in main program with blue marker and red marker. Notice that we don’t need to change the Board class.

You can try creating multiple colors maker class and use in main program. You will see that we don’t need to change the Board class.

public class Sample {

	public static void main(String[] args) {		
		
		Marker blueMarker = new BlueMarker();		
		Board board = new Board();
		board.write(blueMarker);
		
		Marker redMarker = new RedMarker();				
		board.write(redMarker);
	}	
}

This is how we used abstraction in the program.

NOTE:

i)

All markers who implements Marker interface are marker type Hence, Board class public void write(Marker c) method will accept object of them.

This way all concrete implementations are abstracted form Board class. Board class does not anything about their implementation.

ii)

We can also use an abstract class instead of an interface, but we have used interface as we have only abstract method i.e. write method that is unimplemented.

If you need implemented and unimplemented method both, then you can use an abstract class or else interface is fine.

Complete Program using Abstraction Concept

Here is the complete program using three Markers of different colors with abstraction applied.

interface Marker {

	public abstract void color();
}

class BlueMarker implements Marker {

	public void color() {

		System.out.println("blue..");
	}
}


class RedMarker implements Marker{

	public void color() {

		System.out.println("red..");
	}
}

class GreenMarker implements Marker {

	public void color() {

		System.out.println("green..");
	}
}


class Board {
	public void write(Marker c) {
		c.color();
	}
}

public class Sample {

	public static void main(String[] args) {		
		
		Marker blueMarker = new BlueMarker();		
		Board board = new Board();
		board.write(blueMarker);
		
		Marker redMarker = new RedMarker();				
		board.write(redMarker);
		
		Marker greenMarker = new GreenMarker();
		board.write(greenMarker); 

	}	
	
}

Output:

blue..
red..
green..

Related Posts