Skip to content

Instantly share code, notes, and snippets.

@UnquietCode
Created August 24, 2012 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UnquietCode/3452236 to your computer and use it in GitHub Desktop.
Save UnquietCode/3452236 to your computer and use it in GitHub Desktop.
Simulating multiple inheritance in Java.
public class NestedClassesExample {
public static void main(String args[]) {
Child child = new Child();
child.print();
}
public static class ParentA {
public void printA() {
System.out.println("parent a");
}
}
public static class ParentB {
public void printB() {
System.out.println("parent b");
}
}
public static class ParentC {
public void printC() {
System.out.println("parent c");
}
}
public static class Child {
private final MixA.MixB.MixC mixABC = new MixA().mixB.mixC;
public void print() {
mixABC._print();
}
private class MixA extends ParentA {
private final MixB mixB = new MixB();
private class MixB extends ParentB {
private final MixC mixC = new MixC();
private class MixC extends ParentC {
private void _print() {
printA();
printB();
printC();
}
}
}
}
}
// Think of it as a super-delegate. You can always support multiple inheritance with delegates,
// but this gives you the option to create one class with access to all parents simultaneously.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment