2

I have this conceptual query. We say Interface is used to achieve 100% abstraction i.e. hide all the implementation.

But in Interface, all the methods are declared abstract. There is no implementation.

And when the other class implements the same interface then there is no more abstraction as the implementation is defined in the class which implements it.

Please throw some light on it.

Adnan
  • 1,243
  • 2
  • 16
  • 34

6 Answers6

3

You have a missconception about the abstraction interfaces provide.

1. What is an interface?
An interface is a type ob object containing only (public) method signatures (Can vary from language to language). It provides no implementation (function body) of those methods. It can be seen as a kind of contract an implementing class has to fullfil.

2. What kind of abstraction can be achieved by using interfaces?

Created with 'Violet UML Editor' (Created with 'Violet UML Editor')

In this structure the class User only knows that the objects in the objects-array have a method with the signature void Display(). How this method is actually implemented is unnknown to User.

The abstraction happening is depicted by the red line. This is the only (pretty powerful) abstraction achieved by interfaces.

The implementation details of the various variants of void Display() are hidden from User. It can just call IDisplayable.Display() and through polymorphism the correct method gets called.

LukeG
  • 605
  • 10
  • 19
  • Ok i got your point, but where will be the implementation(method body) of void Display() happen? – Adnan Jan 22 '16 at 19:52
  • @Adnan inside of the classes, that implement the interface. The interface is just saying: "Look, every class that implements me has the following methods: ...". The classes implementing the interface then **have** to provide the implementation of these methods. – LukeG Jan 22 '16 at 19:54
  • So when the user himself is implementing the display() method then how will be he unaware of its function & thus abstraction is lost. This is the only confusion I have. Your help will be appreciated :-) – Adnan Jan 22 '16 at 19:59
  • @Adnan I added a little diagram to the answer, which will hopefully clear your confusion about the abstraction interfaces achieve. – LukeG Jan 22 '16 at 20:15
  • Thanks a ton mate. It helped me understand the concept how interface really achieves abstraction. :-) Thanks for your time. – Adnan Jan 22 '16 at 20:20
  • @Adnan No problem, that's why I'm here :) – LukeG Jan 22 '16 at 20:21
  • you should mention that Java8 interfaces **can** have method bodies – specializt Aug 01 '16 at 09:22
  • 1
    can anyone place one sample program for this statement : " It can just call IDisplayable.Display() and through polymorphism the correct method gets called. " – Mohankumar Rathinam Aug 06 '17 at 10:51
1

I would like to explain the difference in non programming language.

Assume that you have TV and a Remote. By Remote, you can operate with TV.

Remote had On and Off buttons. When you click on On button, TV will switch on. But you don't know internals of On button implementation . Same is the case with Off button, which switches off TV.

Now Remote is an interface and TV is an implementation object. Map same concepts in java programming language and you will get more clarity.

Remote is interface with On and Off methods. TV implements Remote interface.

Abstraction is hiding details and TV has achieved it. Even in absence of Remote interface, TV can have On and Off buttons and hides the details.

Interface with abstract methods is not necessary to define abstraction. But interface is pure abstract and hides the details of implementation. It exposes contract by hiding internal implementation of implementor.

Have a look at related question

Abstraction VS Information Hiding VS Encapsulation

Community
  • 1
  • 1
Ravindra babu
  • 42,401
  • 8
  • 208
  • 194
  • And the implementation of on & off buttons is in the Class that implemented that interface. Right? Is that what you're saying? – Adnan Jan 22 '16 at 20:42
  • Yes. Remote abstracts the details and TV implements those details. – Ravindra babu Jan 22 '16 at 20:45
  • But after implementation, we use implementation class rather than interface. But in your example, we are using interface(Remote) for the functionality. Can you please be more specific? or it's like: Remote is an interface. on & off are abstract methods. And TV implements the interface & defines on & off methods. but, in this case, abstraction is not achieved in the context of the user. – Adnan Jan 22 '16 at 20:51
  • 1
    Remote is interface with On and Off methods. TV implements Remote interface. Abstraction is hiding details and TV has achieved it. Even in absence of Remote interface, TV can have On and Off buttons and hides the details. Interface with abstract methods is not necessary to define abstraction. But interface will facilitate it to define contract. – Ravindra babu Jan 22 '16 at 20:57
  • I totally understand what you're saying. If TV implements Remote interface and implements On & Off buttons then the TV is aware of the On & Off buttons and hence abstraction is lost. My Question how will Remote(interface) achieve 100% abstraction by the help of TV(Implementation class). Is there any other method through which we can achieve this? And Regarding your comment where does the TV hides the details of On and Off button? – Adnan Jan 22 '16 at 21:09
  • 2
    TV knowing On and Off functions does not have any impact on abstraction. User knows On and Off button methods but does not know how are they implemented in TV. – Ravindra babu Jan 22 '16 at 21:13
  • So basically, the thing is after implementing the interface. When any other class inherits the implemented class and then executes On and Off methods, he'll not be knowing about the details of On and Off buttons(methods) as it is being defined in Super class. I hope I'm making some sense. – Adnan Jan 22 '16 at 21:27
  • If a base class implements interface and derived class of base class overrides it, derived class is changing the implementation details. Since it has access to base class method ( non static and not private methods), it knows base class implementation. Here interface is so powerful that it can simply change the implemented class from base to derived and user can't know the difference. – Ravindra babu Jan 23 '16 at 05:58
0

This statement is true in the following sense:

When we use an interface implementation as an instance of an interface, the implementation is indeed hidden, i.e. there can actually be object of any implementation.

The example:

public <T> Set<T> listToSet(List<T> list) {
    HashSet<T> result = new HashSet<>();
    for (T t: list) {
        result.add(t);
    }
    return result;
}

Here the implications are:

  1. The caller can pass any List<T> implementation, and the implementation is hidden from the method's intrinsics, it just knows that what is passed is a List<T>, thus it can be iterated over.

  2. The method returns a Set<T>, and the concrete implementation is hidden from the caller, there can be any Set<T> implementation (HashSet<T> in the example).

So, the statement about 100% abstraction is true for the cases when the declared type for a variable or a return type is interface.

hotkey
  • 111,884
  • 27
  • 298
  • 285
0

In Java interfaces can't contain implementation to ensure the abstraction (but this is not a general OOP requirement), thus when you manipulate an object through an interface type you can't rely on any implementation.

In modern Java, you can provide some default implementation to methods, which is not contradictory with abstraction because you can't use attributes, so the provided code is only "functional" in the sense it only describes generic behavior.

On the other end you have concrete classes that provide full implementation of some interface.

In between you have abstract classes, that provide partial implementation.

A good practice is to hide implementation by:

  • providing interface to the user of the concepts
  • providing factories to hide the reality of concrete classes

Of course this is an ideal.

Jean-Baptiste Yunès
  • 30,872
  • 2
  • 40
  • 66
0

I think if don't know anything about implementation just we have requirement specification then we should go for interface and inside interface every method is public and abstract whether declare or not hence interface is also considered 100% pure abstract class

0

I think below programme will help you to understand about interface and abstraction:

  interface Person{
   void dsplay();
}
class Student implements Person{
   public void dsplay() {
      System.out.println("This is display method of the Student class");
   }
}
class Lecturer implements Person{
   public void dsplay() {
      System.out.println("This is display method of the Lecturer class");
   }
}
public class AbstractionExample{
   public static void main(String args[]) {
      Person person1 = new Student();
      person1.dsplay();
      Person person2 = new Lecturer();
      person2.dsplay();
   }
}
Meet Patel
  • 200
  • 8