Java multiple inheritance declaration?

I tested the features and abuse of default keywords in Java. I decided to check for multiple inheritance and what happens if I declare two variables with the same name and signature in the interface and in the class that implements 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 it Bar hackcan have a variable with a name testwith a value overand at the same time have a variable with a name testwith a value t?

Also Bar hack = new Bar();inherits from itself? If hack is an instance Bar, how can it have superone that is of type Barand contains another test?

What is really going on here?

+4
2

, Bar hack test over test t?

(static final ). Bar hack - [instance], , , .

, Bar hack = new Bar(); ? hack Bar, super, Bar test?

, Bar hack = new Bar() { ... };. , hack , , Bar. , Bar.

?

Java:)

+4

1.

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?

. java . / . ,

Bar hack = new Bar() {

         public static final String test="over"; 

test bar annonymous (Bar $1.class), Bar.class. test "t" - , Bar.class. , func, , , , "over"

public void func(){
         System.out.println("Ideone.Meh.hack.func -- Test: "+test);
}

, Bar.test, test , Bar, .

public void func(){
         System.out.println("Ideone.Meh.hack.func -- Test: "+Bar.test);
}

2.

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?

, , Bar.

         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);
             }
        };

True hack - , - , , , .

+1

Source: https://habr.com/ru/post/1616308/


All Articles