I was testing the capabilities and abuse of the default keyword in Java. I decided to test multiple inheritance and what would happen if I declared two variables of the same name and signature in an interface and in the class implementing the interface.
Code: http://ideone.com/mKJjst
import java.util.*;
import java.lang.*;
import java.io.*;
interface Foo {
default void Meh() {
System.out.println("Foo.Meh");
}
}
interface Bar {
String test="t";
default String getTest(){
return test;
}
default void doTest(){
System.out.println("Bar.doTest: -- getTest" + getTest());
}
default void Bar() {
System.out.println("Bar.Bar()");
String blahh=test;
}
static void Sup(){
System.out.println("Bar.Sup -- Test: "+test);
}
default void Meh() {
System.out.println("Bar.Meh -- Test: "+test);
}
default void mega() {
Meh();
}
}
abstract class Test {
public void Meh() {
System.out.println("Test.Meh");
}
}
class Ideone extends Test implements Foo, Bar {
public static void main (String[] args) throws java.lang.Exception
{
new Ideone().Meh();
blah();
}
public void Meh() {
Bar.super.Bar();
Bar.super.Meh();
System.out.println("Ideone.Meh -- Test: "+test);
Foo.super.Meh();
super.Meh();
Bar hack = new Bar(){
public static final String test="over";
public String getTest(){
System.out.println("Ideone.Meh.hack.super.test: " + Bar.super.getTest());
return test;
}
public void Meh(){
System.out.println("Ideone.Meh.hack.Meh()");
func();
Bar.Sup();
Bar.super.Meh();
}
public void func(){
System.out.println("Ideone.Meh.hack.func -- Test: "+test);
}
};
hack.Meh();
System.out.println("Ideone.Meh.hack.test: " + hack.test);
hack.mega();
hack.doTest();
}
public static void blah() {
}
}
Results:
Bar.Bar()
Bar.Meh -- Test: t
Ideone.Meh -- Test: t
Foo.Meh
Test.Meh
Ideone.Meh.hack.Meh()
Ideone.Meh.hack.func -- Test: over
Bar.Sup -- Test: t
Bar.Meh -- Test: t
Ideone.Meh.hack.test: t
Ideone.Meh.hack.Meh()
Ideone.Meh.hack.func -- Test: over
Bar.Sup -- Test: t
Bar.Meh -- Test: t
Ideone.Meh.hack.super.test: t
Bar.doTest: -- getTestover
How is it possible that Bar hack can have a variable called test with a value of over and at the same time have a variable called test with a value of t?
Also, is Bar hack = new Bar(); inheriting from itself? If hack is an instance of Bar, how can it have a super that is of type Bar and contains a different test?
What's actually going on here?
Mehmethod, you try to assign a value toFoo.this.t.