Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encapsulation and Abstraction differences with help of an example in java

Tags:

java

I know my question will be nominated to be closed immediately upon publishing, but let me tell you that I had been searching the site for a clear cut difference in terms of an example, in Java. Please don't be in a hurry to close the question.

I've written the following:

public class Bank {
public String name="XXX Bank";
private int noOfCust;

public int getNoOfCust() {
 return getNoOfCusts() + 2;
}
public void setNoOfCust(int noOfCust) {
if(noOfCust > 0)
{
this.noOfCust = noOfCust;
}
}

private int getNoOfCusts()
{
noOfCust = 100;
return noOfCust;
}
}

Here, 'noOfCust is the data being hidden'. I am not exposing the implementation part by introducing private getNoOfCusts(). So I believe this is encapsulation, because I have bundled all the details in a class and have hidden the data too.

Question 1: If this strong encapsulation ? If not, how could I improve it to be strongly encapsulated?

Question 2: Abstraction means complete hiding of the implementation. So what I've done above is abstraction?

I've posted this question out of frustration because I had given the above explanation to an interviewer, who told me, this is not the correct meaning.

P.S: Please answer in terms of above example. I don't want real time examples or assumptions.

like image 995
Anuj Balan Avatar asked Aug 29 '12 06:08

Anuj Balan


2 Answers

Choosing necessary properties and hiding unwanted details is Abstraction. For e.g. Consider Person. Person has eyes, eyeColor, skinColor properties. But if he goes to Bank, then Bank does his Abstraction and does not consider these Person's properties but it selects name, address, phone_number. This is Abstraction. You misunderstood it.

Encapsulation simply means binding object state(properties) and behavior(methods) together. If you are creating class, you are doing encapsulation.

From wiki : In object-oriented programming languages such as C++, Object Pascal, or Java, the concept of abstraction has itself become a declarative statement - using the keywords virtual (in C++) or abstract (in Java). After such a declaration, it is the responsibility of the programmer to implement a class to instantiate the object of the declaration.

like image 145
Nandkumar Tekale Avatar answered Sep 24 '22 02:09

Nandkumar Tekale


  • The basic idea behind encapsulation is to hide the internal representation of an object from view outside of the class's definition. This is generally done to protect the internals of a class and provide a systematic way to access them.

  • Also, the idea behind having getter/setter combo is to provide a controlled way of accessing the properties of that class. So, for each private property of a class, you would would have public setter() and getter() method.

Taking these two into account, you can very well see why there's a problem with your code in terms of encapsulation. A skeleton class having this concept implemented might be as follows:

public class Bank{
    private int noOfCustomers;
    private String name;


    public String getName(){
        return this.name;
    }

    public void setName(String name){
        this.name = name;
    }

    public int getNoOfCustomers(){
        return this.noOfCustomers;
    }

    public void setNoOfCustomers(int noOfCustomers){
        this.noOfCustomers = noOfCustomers;
    }

    /**
    * This method is to illustrate that it is perfectly valid to have
    * multiple types of setter methods
    */
    public void setNoOfCustomers(String noOfCustomers){
        try{
            this.noOfCustomers = Integer.parseInt(noOfCustomers);
        }catch(Exception exe){
            //Handle exceptions
        }
    }
}

Regarding abstraction: You can think of it as an idea of presenting something in a simplified way, which is either easily comprehensible in terms of usage or more pertinent to the situation. Taking your example, you can think of Bank as an abstraction that can both represent a Federal Bank or a State Member Bank or anything for that matter. You would basically derive specific classes for each of those representations, taking Bank as the parent.

I think you can get a better picture of abstraction if you study what File represents in Java. File in itself is an abstract representation of file and directory pathnames. However, if you study the source codes, you would find that it contains several properties that are private to its design.

Encapsulation, as such can be thought as how well you protect your properties from misuse and abstraction, as such can be thought of how much simplification are you providing to the external user.

like image 45
Sujay Avatar answered Sep 24 '22 02:09

Sujay