Class inheritance

I was told that static methods in java do not have Inheritance, but when I try the following test

package test1;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        TB.ttt();
        TB.ttt2();
    }

}

package test1;

public class TA {
static public Boolean ttt()
{
    System.out.println("TestInheritenceA");
    return true;
}
static public String test ="ClassA";
}

package test1;

public class TB extends TA{
static public void ttt2(){
    System.out.println(test);
    }
}

printed:

TestInheritenceA ClassA

therefore, static java methods (and fields) have inheritance (if you try to call a class method, it goes along the inheritance chain, looking for class methods). Is this really not so? And are there any OO inheritance languages ​​that are confused with class methods?


So, apparently, static methods are inherited, but cannot be overridden, just as C # shares this issue? Are there any other languages?

+3
source share
3 answers

Java , . , , , " ".

, .

+5

, :

class A {
  public static void a() { system.out.println("A"); }
}

class B {
  public static void a() { system.out.println("B"); }
}

A a = new A();
a.a(); // "A"

B b = new B();
b.a() // "B"

a = b;
a.a(); // "A"
+3

This is a static value. This means every class. Static fields and methods are distributed between instances. If you change the static value, it is reflected in different instances.

0
source

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


All Articles