{"id":19708,"date":"2019-05-17T02:59:44","date_gmt":"2019-05-17T02:59:44","guid":{"rendered":"https:\/\/www.adactin.com\/?p=19708"},"modified":"2019-05-17T02:59:44","modified_gmt":"2019-05-17T02:59:44","slug":"java-api-backward-compatibility","status":"publish","type":"post","link":"https:\/\/www.onlinedigitalcompany.com\/adactin\/java-api-backward-compatibility\/","title":{"rendered":"Java API: Backward Compatibility"},"content":{"rendered":"<div class=\"half-width\">\n<h1>What is a backward compatibility?<\/h1>\n<p>&#8211;\u00a0Deepthi Halbhavi-<\/p>\n<p>Backward or downward compatibility in Java API is a property of an API that allows older API usages to function without breaking their existing implementation when an API is modified.<\/p>\n<h1>An API Interface<\/h1>\n<p>Let\u2019s look at the simple API example below.<\/p>\n<h2>Example:<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20622 size-full\" src=\"https:\/\/www.adactin.com\/wp-content\/uploads\/2019\/05\/1-1.png\" alt=\"\" width=\"750\" height=\"88\" \/><\/p>\n<p>The API consists of only one interface <em>Foo <\/em>with one method <em>bar()<\/em>, so the user that implements this API will need to implement the interface <em>Foo<\/em>.<\/p>\n<p>The following class <em>FooImpl <\/em>implements <em>Foo <\/em>and implements <em>bar() <\/em>method.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20623 size-full\" src=\"https:\/\/www.adactin.com\/wp-content\/uploads\/2019\/05\/2-1.png\" alt=\"\" width=\"756\" height=\"167\" \/><\/p>\n<p>Simple implementation for a simple API. But the things get trickier when a simple API is modified to accommodate new requirements.<\/p>\n<p>Let\u2019s check that out.<\/p>\n<h1>Modifying an interface<\/h1>\n<p>Now, let\u2019s modify the <em>Foo <\/em>interface and add a new method foo<em>Bar() <\/em>and see what are its implications on the implementation which is <em>FooImpl<\/em>!<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20624 size-full\" src=\"https:\/\/www.adactin.com\/wp-content\/uploads\/2019\/05\/3-1.png\" alt=\"\" width=\"752\" height=\"116\" \/><\/p>\n<p>A new method is added in the <em>Foo <\/em>interface and if the implementation is using the same API then it will fail in two ways.<\/p>\n<ol>\n<li>The existing implementation will not<\/li>\n<li>The existing implementation will not be able to use the new API and may not be able to use new features. Hence the implementation is stuck with the older version because of no backward<\/li>\n<\/ol>\n<p>Approaches to overcome the above issues<\/p>\n<h1>Implement new API<\/h1>\n<p>Just implement the new API.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"..\/wp-content\/uploads\/2019\/05\/4.png\" alt=\"\" width=\"751\" height=\"210\" \/><\/p>\n<p>As we saw above, the API is modified so, <em>FooImpl <\/em>will just implement the newly added method\u00a0<em>fooBar()<\/em>.<\/p>\n<h2>Pros:<\/h2>\n<ul>\n<li>Keep up-to-date with the API<\/li>\n<\/ul>\n<h2>Cons:<\/h2>\n<ul>\n<li>Enforces implementation to be added for new the API which may not be required at the time of upgrade<\/li>\n<li>Not all the API users would want to implement the new API<\/li>\n<li>It is not backward compatible<\/li>\n<\/ul>\n<h1>Interface versioning<\/h1>\n<p>In this approach, any new modifications to an interface are put in a new interface. Which means create a new interface that extends the existing one and suffix it with, say <em>_V1<\/em><\/p>\n<p>Let\u2019s consider the previous example and apply this approach.<\/p>\n<p>This is the original interface:<em><img loading=\"lazy\" decoding=\"async\" src=\"..\/wp-content\/uploads\/2019\/05\/5.png\" alt=\"\" width=\"1028\" height=\"127\" \/><\/em><\/p>\n<p>A\u00a0versioned interface extends the previous one<img loading=\"lazy\" decoding=\"async\" src=\"..\/wp-content\/uploads\/2019\/05\/6.png\" alt=\"\" width=\"757\" height=\"94\" \/><\/p>\n<p>Now the implementation has a choice of how to implement when to implement. For ex:<\/p>\n<ol>\n<li><em>FooImpl <\/em>implements new interface which is <em>Foo_V1<\/em><img loading=\"lazy\" decoding=\"async\" src=\"..\/wp-content\/uploads\/2019\/05\/7.png\" alt=\"\" width=\"760\" height=\"217\" \/><\/li>\n<li><em>FooImpl <\/em>implements <em>Foo <\/em>and <em>FooImpl_V2 <\/em>extends <em>FooImpl <\/em>and implements <em>Foo_V1<\/em><\/li>\n<\/ol>\n<p><em>\u00a0<img loading=\"lazy\" decoding=\"async\" src=\"..\/wp-content\/uploads\/2019\/05\/9.png\" alt=\"\" width=\"763\" height=\"172\" \/><\/em><\/p>\n<ol start=\"3\">\n<li>Do not implement the new interface <em>Foo_V1 <\/em>at all if there is no requirement to implement it yet<\/li>\n<\/ol>\n<h2>Pros:<\/h2>\n<ul>\n<li>Both of the aforementioned issues are resolved<\/li>\n<li>Implementations have choice which version to use<\/li>\n<li>Implementations have choice whether they want to implement new version at all<\/li>\n<li>Fully backward compatible<\/li>\n<li>Extend the previous implementation for the new API version<\/li>\n<\/ul>\n<h2>Cons:<\/h2>\n<ul>\n<li>Increase in the number of interfaces<\/li>\n<li>If <em>FooImpl_V1 <\/em>is versioned and extends previous implementation then it cannot extend any other class even if you wanted it<\/li>\n<li>May not keep up-to-date with the new<\/li>\n<li>Need to be aware of the new API changes because it won\u2019t complain in a form of compilation errors, hence leading to missing API<\/li>\n<\/ul>\n<p>Using <em>default <\/em>method (Java 8 and above)<\/p>\n<p>Java 8 introduce <em>default <\/em>methods in interfaces to provide backward compatibility. <em>default <\/em>methods allow interfaces to have implementation without affecting classes that implement such interfaces.<\/p>\n<p>So, let\u2019s use this in our example API to make it backward compatible<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"..\/wp-content\/uploads\/2019\/05\/9.png\" alt=\"\" width=\"763\" height=\"172\" \/><\/p>\n<p>The <em>default void fooBar() <\/em>method provide default implementation and it won\u2019t affect the <em>FooImpl<\/em><\/p>\n<p>class that implements <em>Foo<\/em>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"..\/wp-content\/uploads\/2019\/05\/10.png\" alt=\"\" width=\"756\" height=\"507\" \/><\/p>\n<h2>Pros:<\/h2>\n<ul>\n<li>Both of the aforementioned issues are resolved<\/li>\n<li>Fully backward compatible<\/li>\n<li>Keeps up-to-date with the new API with <em>default <\/em>implementation<\/li>\n<li>Implementations have choice whether they want to implement new version at all<\/li>\n<li>The implementation can extend another class<\/li>\n<\/ul>\n<h2>Cons:<\/h2>\n<ul>\n<li>Need to be aware of the new API changes because it won\u2019t complain in a form of compilation errors, hence leading to missing API features<\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>What is a backward compatibility? &#8211;\u00a0Deepthi Halbhavi- Backward or downward compatibility in Java API is a property of an API that allows older API usages to function without breaking their existing implementation when an API is modified. An API Interface Let\u2019s look at the simple API example below. Example: The API consists of only one &hellip; <a href=\"https:\/\/www.onlinedigitalcompany.com\/adactin\/java-api-backward-compatibility\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Java API: Backward Compatibility<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[],"class_list":["post-19708","post","type-post","status-publish","format-standard","hentry","category-blog"],"_links":{"self":[{"href":"https:\/\/www.onlinedigitalcompany.com\/adactin\/wp-json\/wp\/v2\/posts\/19708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.onlinedigitalcompany.com\/adactin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.onlinedigitalcompany.com\/adactin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.onlinedigitalcompany.com\/adactin\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.onlinedigitalcompany.com\/adactin\/wp-json\/wp\/v2\/comments?post=19708"}],"version-history":[{"count":0,"href":"https:\/\/www.onlinedigitalcompany.com\/adactin\/wp-json\/wp\/v2\/posts\/19708\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.onlinedigitalcompany.com\/adactin\/wp-json\/wp\/v2\/media?parent=19708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.onlinedigitalcompany.com\/adactin\/wp-json\/wp\/v2\/categories?post=19708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.onlinedigitalcompany.com\/adactin\/wp-json\/wp\/v2\/tags?post=19708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}