Achala Chathuranga's Blog
Thursday, August 24, 2017
Sunday, July 9, 2017
Constructor Chaining
/*
* Constructor Chaining is a technique of calling another constructor from one constructor.
* this() is used to call same class constructor where as super() is used to call super class constructor
*
*/
class ConstructorChaining {
public ConstructorChaining() {
System.out.println("Default constructor");
}
public ConstructorChaining(String str) {
this();
System.out.println(" constructor with single arg");
}
public ConstructorChaining(String str, int num) {
this("GAC");
System.out.println(" constructor with 2 args");
}
public ConstructorChaining(int num1, int num2, int num3) {
this("GAC", 2);
System.out.println(" constructor with 3 args");
}
public static void main(String args[]) {
ConstructorChaining obj = new ConstructorChaining(4, 8, 33);
}
}
Friday, July 7, 2017
Constructor Overloading
// Constructor Overloading
public class Test1 {
private String name;
public Test1() {
System.out.println("Inside the default constructor");
}
public Test1(int i) {
System.out.println("Inside the single parameter constructor with int value");
}
public Test1(String str) {
System.out.println("Inside the single parameter constructor with string object");
}
public Test1(int i, int j) {
System.out.println("Inside the double parameter constructor with int");
}
public static void main (String args []){
Test1 t1 = new Test1();
Test1 t2 = new Test1(5);
Test1 t3 = new Test1(5,4);
Test1 t4 = new Test1("str");
}
}
Sunday, October 23, 2016
Tuesday, October 18, 2016
Interface Default Methods in Java 8 [Resources to Follow]
More details : https://dzone.com/articles/interface-default-methods-java
Excellent Article from : https://muhammadkhojaye.blogspot.com/2014/03/interface-default-methods-in-java-8.html
Java 8
introduces “Default Method” or (Defender methods) new feature, which allows developer to add new methods to the interfaces
without breaking the existing implementation of these interface
. It provides flexibility to allow interface
define implementation which will use as default in the situation where a concrete class
fails to provide an implementation for that method
.
Let consider small example to understand how it works:
public interface oldInterface {
public void existingMethod();
default public void newDefaultMethod() {
System.out.println("New default method"
" is added in interface");
}
}
The following class will compile successfully in Java JDK 8,?
public class oldInterfaceImpl implements oldInterface {
public void existingMethod() {
// existing implementation is here…
}
}
If you create an
instance
of oldInterfaceImpl:?oldInterfaceImpl obj = new oldInterfaceImpl ();
// print “New default method add in interface”
obj.newDefaultMethod();
Why Defaut Method?
Reengineering an existing
JDK
framework
is always very complex. Modify one interface
in JDK
framework
breaks all classes
that extends
the interface
which means that adding any new method
could break millions of lines of code
. Therefore, default methods have introduced as a mechanism to extending interfaces
in a backward compatible
way.
Default methods can be provided to an interface without affecting implementing
classes
as it includes an implementation. If each added method
in an interface
defined with implementation then no implementing class
is affected. An implementing class
can override the default implementation provided by the interface
.
For Java 8, the
JDK
collections
have been extended and forEach
method
is added to the entire collection
(which work in conjunction with lambdas). With conventional way, the code
looks like below,public interface Iterable<T> {
public void forEach(Consumer<? super T> consumer);
}
Since this result each implementing class with
compile
errors
therefore, a default method added with a required implementation in order that the existing implementation should not be changed.
The Iterable
interface
with the Default method is below,public interface Iterable<T> {
public default void forEach(Consumer<? super T> consumer) {
for (T t : this) {
consumer.accept(t);
}
}
}
The same mechanism has been used to add Stream in
JDK
interface
without breaking the implementing classes
.When to Use Default Method Over Abstract Classes
Abstract classes versus interfaces in Java 8
After introducing Default Method, it seems that
interfaces
and abstract classes
are same. However, they are still different concept in Java 8
.Abstract class
can define constructor
. They are more structured and can have a state associated with them. While in contrast, default method can be implemented only in the terms of invoking other interface methods
, with no reference to a particular implementation's state. Hence, both use for different purposes and choosing between two really depends on the scenario context.Default Method and Multiple Inheritance Ambiguity Problems
Since
java
class
can implement multiple interfaces
and each interface
can define default method with same method signature
, therefore, the inherited
methods
can conflict with each other.
Consider below example,
public interface InterfaceA {
default void defaultMethod(){
System.out.println("Interface A default method");
}
}
public interface InterfaceB {
default void defaultMethod(){
System.out.println("Interface B default method");
}
}
public class Impl implements InterfaceA, InterfaceB {
}
The above
code
will fail to compile
with the following error
,
java: class Impl inherits unrelated defaults for defaultMethod() from types InterfaceA and InterfaceB
In order to fix this
class
, we need to provide default method implementation:public class Impl implements InterfaceA, InterfaceB {
public void defaultMethod(){
}
}
Further, if we want to invoke default implementation provided by any of
super
interface
rather than our own implementation, we can do so as follows,public class Impl implements InterfaceA, InterfaceB {
public void defaultMethod(){
// existing code here..
InterfaceA.super.defaultMethod();
}
}
We can choose any default implementation or both as part of our new method.
Difference Between Default Method and Regular Method
Default Method is different from the regular method in the sense that default method comes with default modifier. Additionally,
methods
in classes
can use and modify method
arguments
as well as the fields
of their class
but default method on the other hand, can only access its arguments
as interfaces
do not have any state.
In summary, Default methods enable to add new functionality to existing interfaces without breaking older implementation of these interfaces.
When we extend an interface that contains a default method, we can perform following,
- Not override the default method and will inherit the default method.
- Override the default method similar to other methods we override in subclass..
- Redeclare default method as abstract, which force subclass to override it.
Subscribe to:
Posts (Atom)