I am little confused about abstraction in java.
I have checked many pages stating that abstraction is data hiding(Hiding the implementation).
What I understa
Maybe an example help you better. Suppose you want to implement a Graph class which may be have adjacency list or adjacency matrix to represent its nodes. So in abstract you want to "addNode" "addEdge" to this graph at least:
public abstract class Graph
{
public abstract int addNode();
public abstract void addEdge(int from, int to);
}
Now you can extend two classes:
public class GraphAdjList extends Graph
{
private Map> adjListsMap;
public int addNode()
{
//list based implementation
}
public void addEdge(int from, int to)
{
//list based implementation
}
}
public class GraphAdjMatrix extends Graph
{
private int[][] adjMatrix;
public int addNode()
{
//matrix based implementation
}
public void addEdge(int from, int to)
{
//matrix based implementation
}
}
when you call either of addEdge from these two classes you don't have to worry about the data structure behind it, you just know that you get the result you needed so for example:
Graph g1,g2;
g1 = new GraphAdjList();
g2 = new GraphAdjMatrix();
g1.addEdge(1,2);
g2.addEdge(1,2);
through polymorphism you call two different functions but get the same result as client of the Graph.
Another real life example would be car brakes. As a car client, the manufacturer gives you a pedal to push without knowing what is the implementation of the brake in the back-end. It can be a drum-brake or disc-brake implementation in the back. All you need is to push the brake!