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<Integer,ArrayList<Integer>> 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!
Data Hiding is basically hiding details about internal Data Members of a class and only exclusively giving the concerned class access to it.
Thus it avoids unnecessary penetration from other classes in the application or from any other application, meaning other classes cannot access private member variables directly.
Thus in this way you abstract internal details of the concerned class.
I'm not sure if this answers your question but if you're talking about abstract classes in general, they are there to provide functionality to a child class that extends it without the child class having to know or deal with all the details of the implementation (it's hidden from the user of the child class).
Let's take a car for example:
public abstract class Vehicle {
protected int _numberOfWheels;
public Vehicle() {
this._numberOfWheels = 4;
}
}
public class Truck extends Vehicle {
public int carryingLoad;
public Truck() {
this.carryingLoad = 4000; // kg or something
}
}
So vehicle is an abstract instance of a vehicle object and already has some functionality associated with it, such as the number of wheels. It's a protected method, so if I create a new instance of truck:
// Inside main
Truck truck = new Truck();
I cannot change the number of wheels, however, if I were to write a function for truck within the class like:
// Inside the Truck class
public void addSpareTire() {
this._numberOfWheels++;
}
So that I could call it like:
// Inside main
truck.addSpareTire();
I could still interact with some variables, but only thorough the functions in the class that extends the abstract class. I can add one tire at a time by calling addSpareTire()
, but I could never interact directly with _numberOfWheels
from the main function where I am using the Truck
object, but I can from inside the Truck
class declaration. From the user of the truck object, that information is hidden.
I don't know if this is what you're asking for. Hope this helps.
You are confusing abstraction (the programming pattern) with the abstract keyword of the Java language. Despite the similitude, they are only very lightly related semantically.
Abstraction means that you hide the implementation details from the code that uses a class or method. The code using your method does not need to know that you are implementing say, a List, using arrays or dynamic pointers or an embedded database or files in a filesystem.
abstract is used to mark classes that have methods that are only declared and not implemented. It is "abstract" in the sense that they cannot be instantiated, you cannot create any instances out of these classes (but you can create them out of their concrete subclasses).
You are going to get access to the code once you implement the class/interface and you will modify it according to your need.
I guess you are little confused about the concepts which language gives you like Abstraction
here. You always have access to your own code but the things like Abstraction
OR Polymorphism
gives you the ideal ways which things must be. So in Abstraction
you are just saying I know there will be a behavior having name someThing
as abstract method but right now you dont know how it will behave, The implementer will tell us how this will be. See following code.
abstract class Game{
public abstract void play();
}
class Football extends Game{
@Override
public abstract void play(){
// write how football play
}
}
class Cricket extends Game{
@Override
public abstract void play(){
// write how Cricket play
}
}
I am here leaving a question for you.
Why you are making class level attributes public/protected/private although you have access to the code when you implement?
It is not hiding the information from you, but from the client of your abstraction. Take a look at this example.
public class Demo {
Client client = new Client(new Implementation());
}
public interface Abtraction {
void doWork();
}
public class Implementation implements Abtraction{
@Override
public void doWork() {
//doingTheWork
}
}
public class Client{
private Abtraction theAbstraction;
public Client(Abstraction theAbstraction){
this.theAbstraction = theAbstraction;
}
}
The class Client
is unaware of the implementation of Abstraction
, this means that the Demo
class can provide different implementations of Abstraction
without messing up the Client
.