I would like to use JAVA 8 default methods as a means for implementing multiple inheritance. So I have a repetitive code represented by the addValue()
method that I need to move from the implementations to the interface.
interface IX {
ArrayList list = new ArrayList();
default void addValue(Object o) {
this.list.add(o);
}
}
class A implements IX {
//no addValue implementation here
}
class B implements IX {
//no addValue implementation here
}
class Main {
public static void main(String [] args) {
A a = new A();
B b = new B();
a.addValue(this);
b.addValue(this);
}
}
I would like to know
a.addValue(this)
syntax is correct Java does not allow you to have multiple inheritance of state, only behavior.
If you want to share the declaration of an instance field you would need to place it in an abstract class.
if this is a valid usage of default methods
I use default methods heavily, but since I value immutability, i rarely place methods that mutate state in an interface.
Since (currently) all interface methods are public, if I need to inherit methods that mutate state I will place them (as protected) in abstract classes.
If the a.addValue(this) syntax is correct
No. Since you are in the static main method there is no "this". What do you want to add to this list?
If two different list objects will be created In your example only one (global) list will be created. It is also important to note that ArrayList is not thread safe and in general should not be used in a global field, CopyOnWriteArrayList (or similar) should be used instead.
The example below:
/**
* The Interface IX.
*/
public static interface IX {
/**
* Gets the list.
*
* @return the list
*/
List<Object> getList();
/**
* Adds the value.
*
* @param o the o
*/
default void addValue(Object o) {
this.getList()
.add(o);
}
}
/**
* The Class AbstractIX.
*/
public static abstract class AbstractIx implements IX {
/** The list. */
protected List<Object> list = new ArrayList<>();
@Override
public List<Object> getList() {
return this.list;
}
}
/**
* The Class A.
*/
public static class A extends AbstractIx {
// no addValue implementation here
}
/**
* The Class B.
*/
public static class B extends AbstractIx {
// no addValue implementation here
}
/**
* The Class Main.
*/
public static class Main {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
A a = new A();
B b = new B();
a.addValue(1);
a.addValue(2);
b.addValue(1);
System.out.println("List a size should be 2: " + a.getList()
.size());
System.out.println("List b size should be 1: " + b.getList()
.size());
}
}
If two different list objects will be created
A single object is created
If the a.addValue(this) syntax is correct
No, try to add some string. The size of the list comes out to be 2
you can use sysout statements to figure such things out. :)
Thanks,
Amar
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.