It seems that Java 8 allows full fledged inheritance with a simple framework as below, using Static and Default methods on interfaces.
While its always possible to misuse and write stupid code, these new features make it quite easy to achieve multiple inheritance, even if the designers of the language meant to stay away from it.
Is this a simple implementation of Multiple Inheritance using Interfaces as base classes, or a misuse of the language? Did Java designers go too far in allowing this?
package PseudoMultipleInheritance;
import java.util.HashMap;
abstract class InstanceMap<T,T2>
{
HashMap<Object,Object> instances = new HashMap<Object, Object>();
abstract T2 createMembersInstance();
T2 getMembersInstance(T thisObject )
{
if ( !instances.containsKey(thisObject) )
instances.put(thisObject,createMembersInstance());
return (T2) instances.get(thisObject);
}
}
interface A
{
class Members
{
int x; // just an example of an inheritable member
}
InstanceMap<A,A.Members> instanceMap = new InstanceMap<A, A.Members>() { A.Members createMembersInstance() {return new A.Members(); }};
default A.Members thisA() { return instanceMap.getMembersInstance(this); }
default int getX()
{
return thisA().x; // // just an example of an inheritable getter
}
default void setX(int x)
{
thisA().x = x; // just an example of an inheritable setter
}
}
interface B
{
class Members
{
int y; // just an example of an inheritable member
}
InstanceMap<B,B.Members> instanceMap = new InstanceMap<B, B.Members>() { B.Members createMembersInstance() {return new B.Members();} };
default B.Members thisB() { return instanceMap.getMembersInstance(this); }
default int getYLastDigit()
{
return thisB().y % 10; // just an example of an inheritable function
}
default void incrementY(int x)
{
thisB().y += x; // just an example of an inheritable function
}
}
class C implements A, B
{
}
public class Example04AlmostMultipleInheritance {
public static void main(String[] args) {
C c1 = new C();
C c2 = new C();
c1.setX(5);
c2.setX(3);
System.out.println(c1.getX()); // prints 5
System.out.println(c2.getX()); // prints 3
c1.incrementY(99);
System.out.println(c1.getYLastDigit()); // prints 9
}
}
///////////////////////////////////////////////////
Or for yet another option:
interface A
{
class Members
{
public int x; // just an example of an inheritable member
void showX() { System.out.println(x); } // just an example of an inheritable function
}
InstanceMap<A,A.Members> instanceMap = new InstanceMap<A, A.Members>() { A.Members createMembersInstance() {return new A.Members(); }};
default A.Members getA() { return instanceMap.getMembersInstance(this); }
}
interface B
{
class Members
{
int y; // just an example of an inheritable member
}
InstanceMap<B,B.Members> instanceMap = new InstanceMap<B, B.Members>() { B.Members createMembersInstance() {return new B.Members();} };
default B.Members getB() { return instanceMap.getMembersInstance(this); }
}
class C implements A, B
{
}
public class Example04AlmostMultipleInheritance {
public static void main(String[] args) {
C c1 = new C();
C c2 = new C();
c1.getA().x = 5;
c2.getA().x = 3;
c1.getA().showX(); // prints 5
c2.getA().showX(); // prints 3
c1.getB().y = 99;
System.out.println(c1.getB().y % 10); // prints 9
}
}