JAVA - Abstraction

主宰稳场 提交于 2019-12-01 12:30:30

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!

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).

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.

In Object oriented programming Abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words user will have the information on what the object does instead of how it does it.

check the following link for more details: http://www.visionsdeveloper.com/tutorial/java/java-abstraction.jsp

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?

I think you are confused about two things

  1. Abstract concept in OOPs and its implementation in Java
  2. abstract class in java

  3. Abstract : There is OOPs concept called Abstract. Here, user can capture necessary features of an object. This abstract concept implemented as class and object in java. So, coming to you question, data hiding, when you capture important features of an object in the form of class those features are accessible only to their own class's object. Here, you are hiding all features of a class from outside the class/world. So, it is called as data hiding

  4. abstract class : This is the feature of the Java implementation. If you don't know complete implementation then you can go for abstract class of java.

Hope this helps you to understand a little

Thanks

Data hiding is when you cannot understand how to work API internally (for example, ArrayList) without reading the documentation. API creators don't provide any access to the array that underlies there.

You hide API implementation from users that will be used it. They shouldn't worry about how it works internally, they are only interested in the functionality which is provided to them.

There is a good article about that.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!