1
public abstract class abs {
    abstract void dance();
}

This abstract class was extended by Outer class.

public class Outer extends abs {

    public static void main(String[] args)
    {
        Outer o = new Outer();
        o.dance();

    }

    @Override
    void dance() {

        System.out.println("dancing from abstract class");
    }

I didn't specify any implementation details of dance() method in abstract class but defined the dance() method in Outer class i.e implementation details are revealed in derived class (Outer). We gonna run this program from Outer class. So, where exactly we are hiding implementation details from the user? Where is abstraction achieved?

khelwood
  • 55,782
  • 14
  • 81
  • 108
Abhiram V
  • 69
  • 1
  • 12
  • In some other class: `public whatever(abs d);` can receive your an instance of Outer, and make it dance, without even knowing there is such a thing as your Outer class. – Mat Jun 13 '18 at 12:03
  • https://stackoverflow.com/a/11966068/5602214 – Prashant Jun 13 '18 at 12:04
  • Hiding implementation details = using of **supertype** which can be **interface**, **abstract class** or **class**. Example: `abs a = new Outer();` i.e. concrete class `Outer` is hidden in `abs` supertype. Why would you do that? You will find the answer in [What does it mean to program to an interface?](https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface). – matoni Jun 13 '18 at 12:11

2 Answers2

0

It is hidden in the sense that you will, probably, work with the abstraction in your application and wire the concrete classes when needed according to the context where you are in.

The key idea is that it is important that an object can do something, how it does it is not necessairly important always. A direct consequence on working with the abstraction is that you can easily change the implementation under the hood without any side-effects whatsoever.

For instance consider a sort operation of a language. When you call sort with on collection. in general, all you want is to sort the collection, about how it is done is not always important.

NiVeR
  • 9,644
  • 4
  • 30
  • 35
0

In your specific case here, nothing is hidden, because you are using Outer as the type of the variable here:

Outer o = new Outer();

To hide the implementation of Outer behind abs, you can write a method that returns abc, that actually returns an Outer:

public static getAbc() { return new Outer(); }

Now in your main method:

// you should probably put your main method in another class, together with getAbc
// this way the code can better demonstrate how to hide implementation.
public static void main(String[] args)
{
    abc o = getAbc();
    o.dance();
}

Now your main method does not know anything about Outer It only knows about abc.

You can study examples in the JDK as well. For example, the Stream interface has a method called of that returns a Stream:

Stream<String> stream = Stream.of("Hello", "foo", "bar");

What it actually returns is a ReferencePipeline.Head after calling a bunch of methods in Arrays class and StreamSupport class. See the hiding here? You, as the user of the interface, don't need to know what of actually returns. You just need to know that it returns something that can perform all the operations specified by the Stream interface.

One benefit of this is that if one day StreamSupport's code are changed to return something else, your code will still work and compile because it is guaranteed that whatever it returns, will implement the Stream interface so has all its capabilities.

Sweeper
  • 213,210
  • 22
  • 193
  • 313