0

i had given the following code in an interview. I want to know whether it is right or not..

 public class DataAbstraction
    {
        public static void main (String args[])
        {
            MyDetails obj = new MyDetails();
            obj.setNumebr(10);
            obj.incrementBy(20);
            int num = obj.getMumber();
            System.out.println(num);
        }
    }

class MyDetails
{
    private int n;
    public void setNumebr(int i)
    {
        n = i;
    }
    public void incrementBy(int i)
    {
        n = n + i;
    }
    public int getMumber()
    {
        return n;
    }
}

So please check it and correct me if i was wrong

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Dil Se...
  • 877
  • 4
  • 16
  • 43

2 Answers2

0

There are many forms of abstractions in software. I would say that this is an example of data abstraction (though I would usually call it encapsulation). You could, if you would like to, change the member variable n to be of type... String(!), without changing the public interface of MyDetails.

Put differently: The details in the MyDetails class are hidden from the client code. The fact that MyDetails stores an int is abstracted away and it could be changed, for instance like this:

class MyDetails
{
    private String n;                // changed internal detail
    public void setNumebr(int i)
    {
        n = "" + i;
    }
    public void incrementBy(int i)
    {
        n = "" + getMumber() + i;
    }
    public int getMumber()
    {
        return Integer.parseInt(n);
    }
}

Have a look at the Wikipedia article on data abstraction for further details:

aioobe
  • 413,195
  • 112
  • 811
  • 826
-1

Since there aren't enough details in the question its guessing time again:

1) No, its wrong. it contains various spelling errors like "getMumber" and "setNumebr".

2) Yes, if we ignore the spelling errors the methods seem to do what one would expect from their names.

2) No, it doesn't launch the rocket and it doesn't scale to multi processor machines (assuming these where the requirements).

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348