Encapsulation - Java OOPs Concepts

Wrapping data and methods within classes

 in combination with implementation hiding (through access control) is often called encapsulation in OOPs. The result is a data type with characteristics and behaviors. Encapsulation essentially has both i.e. information hiding and implementation hiding.


Information hiding is done through using access control modifiers (public, private, protected) and implementation hiding is achieved through creation of interface for a class.
Information hiding gives the designer the freedom to modify how the responsibility is fulfilled by an object. This is especially valuable at points where the design (or even the requirements) are likely to change.
Let’s take an example to make it more clear.

1 Information hiding

class InformationHiding
{
    //Restrict direct access to inward data
    private ArrayList items = new ArrayList();
    //Provide a way to access data - internal logic can safely be changed in future
    public ArrayList getItems(){
        return items;
    }
}

2 Implementation hiding

interface ImplemenatationHiding {
    Integer sumAllItems(ArrayList items);
}
class InformationHiding implements ImplemenatationHiding
{
    //Restrict direct access to inward data
    private ArrayList items = new ArrayList();
    //Provide a way to access data - internal logic can safely be changed in future
    public ArrayList getItems(){
        return items;
    }
    public Integer sumAllItems(ArrayList items) {
        //Here you may do N number of things in any sequence
        //Which you do not want your clients to know
        //You can change the sequence or even whole logic
        //without affecting the client
    }
}

Thank you for sharing Your Knowledge with others who need It.-Team LetML

1 comment :

  1. How much does a casino make? (PDF) - Oklahomacasinoguru
    How much 먹튀 커뮤니티 does a casino make? 해외 배팅 사이트 가입 (PDF) This page 바카라조작 discusses casino tips and strategy. 토토 커뮤니티 Find 슬롯 사이트 info on table games.

    ReplyDelete

Copyright © LetML. Blogger Templates Designed by OddThemes