2

I have learned that interfaces and abstract classes in java help us achieve abstraction. However, I do not fully understand that theory, abstraction means hiding internal mechanism and showing relevant details only, but how exactly interface and abstract class help us in that.

Can someone please explain me with real life examples.

dcanh121
  • 4,665
  • 11
  • 37
  • 84
  • I believe you are describing `encapsulation`. – PM 77-1 Jan 20 '15 at 05:13
  • An `interface` is like a contract, it says "I guarantee to perform this behaviour", how it actually does is irrelevant... – MadProgrammer Jan 20 '15 at 05:13
  • Like @MadProgrammer said, an interface does not show how something is done but what should be done. – charliebrownie Jan 20 '15 at 05:16
  • But interface itself doesn't do anything, as in if we talk about 'serializable' then we implement serializable but we define how exactly our objects will be serialized. I do not get what an interface hides from us. –  Jan 20 '15 at 05:23

3 Answers3

5

abstraction via interface

Here Shape could be an interface. and Rectangle and Triangle are implementing classes.

It can be stated as:

Rectangle is a shape.

Similarly

Triangle is also a shape.

A Shape does have area based on its dimensions. And each shape could have different calculation for the area.

Here the abstraction is in the area() in Shape interface. But the implementation of the area() differ throughout its implementing classes.


In the same way you can take an example of Animal interface which could have Cat, Dog and many of such implementing classes.


what is abstraction

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
0

Interfaces and abstract classes has no implementation and for abstraction you only show users the Interfaces and abstract classes. So the implementation details are hidden from the users and user can only view the definitions.

Junaid
  • 2,572
  • 6
  • 41
  • 77
0

Interface and Abstract Class both provide Abstraction,though at different levels,

Interface provide 100% abstraction,while Abstract Class provides partial abstraction.

Interface cannot have a concrete function declaration,while Abstract class can have one.
(concrete functions are functions with a body)

REAL WORLD EXAMPLES

ABSTRACTION Hiding internal complexities from the end user is abstraction.

For example

TAPE RECORDER
(100% abstraction,INTERFACE Example)

1.Press PLAY to play,
2.Press STOP to stop.
End user need not to bother about how it is done,just the functionality is done via the press of the button.

TAPE RECORDER
(partial abstraction,Abstraction Example)

1.You have a DESKTOP,
2.You do usual stuff on pre loaded Windows.
3.You start researching and created your own os,or extended an UNIX OS,with your functionality.
4.Now you use the same DESKTOP,it still has abstraction,as you start the System by pressing the START button and all,and you don't know anything how this is being done internally,but once your OS boots up,you run the functionality written by you,doing so,you aren't just using an abstract thing,but you know how it's being done too.

PROGRAMMING WORLD EXAMPLES#

ABSTRACTION

For example

API's
(100% abstraction,INTERFACE Example)

1.you get functions of the API,
2.Pass the parameter and you will get the result as the return type.
3.But the user doesn't know what internal processing is being done.

Team Leader gives architecture

(100% abstraction,INTERFACE Example)
1.you get an interface,as to what all you need to make as a programmer.
2.you make a class and implement the interface.
3.you implement all the unimplemented methods and provide the workings.
As per the Team Leader,he doesn't give a damm about the functionality but,he knows what function will do what.
(futuristic approach,generally used in programming)

Programmer make an abstract class

(partial abstraction,Abstraction Example)
1.You make an abstract class for your use,
2.Then you realize that you need to provide a method for some functionality that you want the child class can use.
3.You write the functionality straight away in a concrete function.
That's how being a programmer,you know some functionality,while you don't know what others will implement to the unimplemented functionality.(non-concrete abstract functions)

bondkn
  • 420
  • 5
  • 11
  • [From Java 8 you can define static methods in interfaces in addition to default methods](http://stackoverflow.com/a/22713721/725306) – Mohammad Faisal Jan 20 '15 at 09:37