Created
August 24, 2012 15:55
-
-
Save UnquietCode/3452236 to your computer and use it in GitHub Desktop.
Simulating multiple inheritance in Java.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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