JAVA - Abstraction

前端 未结 9 2112
旧巷少年郎
旧巷少年郎 2021-01-15 04:29

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

9条回答
  •  爱一瞬间的悲伤
    2021-01-15 05:09

    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.

提交回复
热议问题