• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How interface solve their intended purpose and how default methods doesn't contradict it?

 
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to Java and never done a full blown project, so may be my question is very silly and answer very obvious   , but I will throw it anyway:-

I read in the oracle docs that interfaces help in keeping the secrecy of code/implementation, ie they prevent your code/implementation from being exposed to the other party. Below is an excerpt from oracle docs:-



For example, imagine a futuristic society where computer-controlled robotic cars transport passengers through city streets without a human operator. Automobile manufacturers write software (Java, of course) that operates the automobile—stop, start, accelerate, turn left, and so forth. Another industrial group, electronic guidance instrument manufacturers, make computer systems that receive GPS (Global Positioning System) position data and wireless transmission of traffic conditions and use that information to drive the car.

The auto manufacturers must publish an industry-standard interface that spells out in detail what methods can be invoked to make the car move (any car, from any manufacturer). The guidance manufacturers can then write software that invokes the methods described in the interface to command the car. Neither industrial group needs to know how the other group's software is implemented. In fact, each group considers its software highly proprietary and reserves the right to modify it at any time, as long as it continues to adhere to the published interface.\



My simple question is that how, in the case where we don't create/use any interface, the code of auto manufacturers and guidance manufacturers gets exposed to each other(which creates the need of an Interface in the first place)? I mean, even when you implement an Interface, its not that the code of auto manufacturers is in a server in USA and the code of guidance manufacturers is in India, with both servers not connected by any network!!

My second question is regarding default methods. Suppose, in the above example, auto manufacturers implement an default method in the interface itself, so now does that default method code/implementation gets exposed to guidance manufacturers? If yes, then is using default methods, a trade off? Ie you gain something(adding new methods conveniently to old interface) but you also loose something(hiding your implementation)?

 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhay Bhatt wrote:My simple question is that how, in the case where we don't create/use any interface, the code of auto manufacturers and guidance manufacturers gets exposed to each other(which creates the need of an Interface in the first place)?


Car manufacturers must publish modules that contain the declarations of the methods that can be called, otherwise guidance manufacturers can't develop their software effectively. However, they are not required to publish the modules that implement these methods. They could install these modules right into the cars that they manufacture, even having them protected by the car's hardware. When they don't use interfaces to publish the method declarations, they must include their implementations with the published module, which can then be reverse engineered by the guidance manufacturers.

Suppose, in the above example, auto manufacturers implement an default method in the interface itself, so now does that default method code/implementation gets exposed to guidance manufacturers? If yes, then is using default methods, a trade off?


Yes, you could say that. However, usually default method implementations shouldn't be very complex, so I doubt anybody would care about reverse engineering them.
 
Abhay Bhatt
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Car manufacturers must publish modules that contain the declarations of the methods that can be called, otherwise guidance manufacturers can't develop their software effectively. However, they are not required to publish the modules that implement these methods. They could install these modules right into the cars that they manufacture, even having them protected by the car's hardware. When they don't use interfaces to publish the method declarations, they must include their implementations with the published module, which can then be reverse engineered by the guidance manufacturers.



Can you please tell me what do you mean by a 'published module'? My best guess would be that it might have something to do with access modifiers but then I am not sure.

Stephan van Hulst wrote:

Yes, you could say that. However, usually default method implementations shouldn't be very complex, so I doubt anybody would care about reverse engineering them.



But they can be complex, can't they? Is there a logic behind, functionality added later on, being, usually or always, relatively less complex or simpler, in nature?
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhay Bhatt wrote:Can you please tell me what do you mean by a 'published module'? My best guess would be that it might have something to do with access modifiers but then I am not sure.


Actually, I meant it quite literally. A published module would be one that's released to the public at large, likely through the website of some organization that's tasked with maintaining the standard. It would be accompanied by documentation.

In the example above, guidance manufacturers would have access to the public interface and maybe a reference implementation for testing purposes, but not to proprietary implementations that are only released to customers that buy a car.

But they can be complex, can't they? Is there a logic behind, functionality added later on, being, usually or always, relatively less complex or simpler, in nature?


Yes, default implementations can be complex. The logic behind them usually being simple is that they're based on public methods of the same interface, and that they can't use fields or depend on other implementation details.

A default implementation is usually a simple sensible one- or two-liner, so that developers that quickly want to write some concrete implementation don't have to worry about it.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, remember the purpose of default methods in interfaces. When Streams and λs were introduced in 2014, it was necessary to add a stream() method to every List in existence. How do you do that? Do you require everybody to add stream() methods to all their List implementations? Then what will happen if you declare a List as List and List doesn't have the stream() method? So what will happen if you add a stream() method to the List interface? What will happen when you recompile code where the List implementations don't implement that method? And the implementer looks at the Java7 documentation for List and there isn't a stream() method?

As you will see, that is going to cause no end of confusion; people will stop using Java® because of loss of backwards compatibility. So Oracle reused the existing default keyword to mean (an instance) method added to an interface which has an implementation. Now you can use the stream() method on every List in Java8 because it is inherited with implementation from the List interface (or more precisely from List's superinterface). If the idea behind default methods was for adding functionality to existing interface, is there any need to write a default method in a new interface at all?

I am pretty sure methods like stream() run some pretty complicated code in the background.

Moving question as too difficult for the beginning forum.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:If the idea behind default methods was for adding functionality to existing interface, is there any need to write a default method in a new interface at all?


Definitely. Default methods introduce the concept of "traits" to Java. I've written some new interfaces with default methods, because they are much easier to work with than incorporating implementations from multiple disjoint abstract classes.

For instance, take the following interface:

Should I really put the four default method implementations in a separate abstract class, even though they are very sensible defaults for almost every binary tree? What if my binary tree implementation also has to extend a different abstract class? Should I really have to reimplement these defaults all over again?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree. Yes, those methods are better as defaults.
 
You didn't tell me he was so big. Unlike this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic