Does the behavior of static and private methods when calling a child class directly on an object sound like an override?

public class B extends A{ public static void main(String[] args) { new B().privateMethod();//no error -output B-privateMethod.Sounds like overriding new B().staticMethod(); //no error -output B-StaticMethod.Sounds like overriding } private void privateMethod() { System.out.println("B-privateMethod."); } static void staticMethod() { System.out.println("B-StaticMethod."); } } class A{ private void privateMethod() { System.out.println("A-privateMethod."); } static void staticMethod() { System.out.println("A-StaticMethod."); } } 

In R & D, I found in the case of privateMethod () - since this method was not available for the object of the child class, therefore the child class and the parent class privateMethod () are a separate method and they have no relationships, so this is not overriding. but in the case of staticMethod () - the method of the parent class was available on the object of the child class, and when we define this in the child class, the object of the child class begins to point to the method of the child class. This is similar to overriding a method, but not because the static method does not cancel.

how is the static method handled using the java development kit?

+1
source share
3 answers

Polymorphism is not for static methods. Static methods are invoked with JVM invokestatic instructions, while polymorphism is achieved using invokevirtual. Calls to static methods are defined at compile time, and polymorphic methods are dynamically dispatched at run time.

You can easily customize your code so that A.staticMethod () is called by simply assigning a new B () to a variable of type A.

 public static void main(String[] args) { new B().privateMethod(); A b = new B(); // change here. b.staticMethod(); // A.staticMethod() is called here. } 
+1
source
 new B().privateMethod(); 

this does not override since B does not see A privateMethod ().

 new B().staticMethod(); 

this is not overriding; calling a static method through an instance is allowed, although it can be misleading. This is exactly the same as calling it through the class name - B.staticMethod() . If the superclass A of B has a static method visible from B , you can call this method from B (and it doesn't matter if you write B.staticMethod() or A.staticMethod() or new B().staticMethod() .

If you later define a static method with the same name in B , this method hides the method with the same name in A , so calling B.staticMethod() or new B().staticMethod() now calls the static method B However, calling A.staticMethod() will still call the static method A

+2
source

Never talk about statics and overrides in the same sentence.

The whole concept of redefinable methods is to dynamically bind at runtime to be executed. Consider this:

 class A { void print() { out.println("A"); } class B extends A { void print() { out.println("B"); } A obj = new B(); obj.print(); 

Although the variable obj is of type A , it still outputs "B".

Static methods, on the other hand, are bound at compile time. This means that the compiler uses the type of the variable (or expression) to determine which method to execute:

 class A { static void print() { out.println("A"); } class B extends A { static void print() { out.println("B"); } A obj = new B(); obj.print(); 

Now it gives "A". Unfortunately, the Java language allows you to call static methods on variables or expressions. This is not recommended! It is better to call static methods for the type itself:

 A.print(); B.print(); 

In the first example, obj.print(); - the compiler automatically translates the operator into A.print() . The actual object is not taken into account. In fact, you could write the following:

 A obj = null; obj.print(); 

Or:

 ((A) null).print(); 

It is still typing "A".

0
source

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


All Articles