Skip to main content

Default Methods in Java 8

Adding new methods in interfaces always break the implementation in class. So Java 8 come up with new feature which propose a mechanism to extend existing interfaces by adding new methods without breaking existing implementation thus, achieved the backward compatibility.

Default methods is the feature which helps you to extend existing interfaces.
If added method in interface provides the default implementation then no implementing Class get affects. 

Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older version of those interfaces.

An implementing class can override the default implementation defined in implemented interface and add its own implementation.

To understand it with example, lets say there is ABC company which wants to launch their smart Radios in market, so they have developed SmartRadio interface(contract) and ABCRadio final product implements all the functionality specified by SmartRadio.
All worked fine, ABC company has launched their first smart radio and sold it in market and gain huge popularity amongst consumer.

Later, ABC company decided to launch next generation radio in which they do not want their radio listeners to remember and provide channel frequency manually so the company decided o automate this via autotune feature which let user to tune channel automatically without asking for particular channel frequency.

Job was not that easy because they want to extend the SmartRadio contract without effecting their already launched ABCRadio in market, so to rescue them default methods has arrived.

see below code with added default method in SmartRadio interface.
Problem solved!

so default methods is one of nice feature in Java 8, you can have as much as possible default methods in your interfaces and optional to override in your class without breaking already written classes.

As you all know Java don't allow to extend multiple classes to avoid diamond problem but allows to implement multiple interfaces, so what if more than one interfaces has same default methods signature and any class implements all these such interfaces then?
if such scenario happened then java compiler wont let you compile your code by saying unrelated defaults noticed from such such interfaces and forces you to override the method in implementing class.

see below code for more explanation:
You can even call particular default method from corresponding interface like this:
Default methods are quite different from normal methods, firstly they are part of interfaces, second they are prefixed with default modifier, normal class methods can access class fields while default methods can not since interfaces do not have state.
Two different interfaces can not have same default method defined with only difference of return type in that case you will be confusing java compiler and it wont let you compile your code.

Comments

Popular posts from this blog

Lambda expression in Java 8

Behaviour Parameterisation …pattern which allows you to write such a flexible code which can cope up with any requirement change. Now you may wondering why I am talking about this weird sounding pattern instead lambda expression, right? Well, let me tell you that we will soon get to know about lambda expression in detail but understanding Behaviour Parameterization  pattern will definitely help you thoroughly how lambda expression is useful to write concise and succinct code and you will start feeling power of lambda expression which is one of prime feature of java 8 release. Lets say, we have list of Books and you need to find Books with greater than 200 pages. You could write a method getBooksGreaterThanPages(): Using this method is simple enough, what if you need to find books smaller than 200 pages? Or Books above particular prices? Or Books from particular author or publications? Or even worst book with particular publication with certain pages and below partic...

Reference data type in java

Unlike Primitive data types in java, Reference data type store the address of object in memory which they refer to, than actual value. lets say, Car class is already defined, and its object is created in memory then variable declared with type Car becomes the reference type which will be holding address of car object to which it refers. Car car = new Car(); int noOfDoors = 4; Car myCar = car; // copy the car reference into myCar reference . Here, variable car actually contains the address and variable noOfDoors will contain actual 4 value. See below diagram for more explanation: Reference data type holding address of object in heap memory and primitive data type holds actual primitive values Available reference types in java: Class type Interface type array type e.g. String name = new String("Java"); List listOfNames = new ArrayList(); Students topRankers[] = new Student[10]; Remember though you know now that Reference data type holds ...