• Post Reply Bookmark Topic Watch Topic
  • New Topic

what even is the point of implements/interfaces?  RSS feed

 
Joshua Harris
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I just had to make an interface which looks like this



and two classes which implement the interface which look like this





I know these classes are extremely simple but from my understanding an interface is something you can use to create methods/variables which will be shared by multiple classes? But then when you write the classes you need to define the methods anyway, what's the point of creating an interface when you just need to write the methods in the actual class anyway?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point is not to make writing classes easier, but to make using them easier.

If you have a program that should determine the surface area of all the solids in a room, it would be annoying to first call getSurfaceArea() on all the spheres, and then getSurfaceArea() on all the cubes. Instead, interfaces allow us to write code once:


 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another useful aspect of the separating the Interface/Implementation is that you can substitute one implementation of an interface for another.

Take for instance database drivers.
The whole java.sql package is made up of interfaces for Connection, Statement, ResultSet etc etc
Each database vendor provides their own implementation of these objects to talk to their database.
But because we write to the standard interface, which database you use becomes a deployment/configuration issue.

Another example from the API is the Collections framework.
If you write to the API of using a List, that will handle ArrayList, LinkedList, MyAwesomeCustomList etc etc because all of those implement the List interface.


It also allows you to extend a system easily.
Taking your shapes example, I could write a Pyramid class which as long as it implemented the GeometricSolid implementation could be used by any existing code that understood Geometric solids.
 
Victor Bogo
Greenhorn
Posts: 25
AngularJS Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of interface as an object "kind". Sometimes you need one group of classes that share features to compulsorily implement certain methods like this:







It is a rule: Animal MUST breathe just like classes that implement Animal MUST implement method breathe!

 
Joshua Harris
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:The point is not to make writing classes easier, but to make using them easier.

If you have a program that should determine the surface area of all the solids in a room, it would be annoying to first call getSurfaceArea() on all the spheres, and then getSurfaceArea() on all the cubes. Instead, interfaces allow us to write code once:




I see. I guess that's a better way of looking at it. Thanks.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan's explanation is actually more significant. By using interfaces, you decouple your code from specific implementations. If you write your code in such a way that it only uses what it needs (interfaces) and nothing more (classes) then your code can easily interact with other code from third parties.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!