Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How do static methods interact with inheritance?

0
Posted

How do static methods interact with inheritance?

0

Ans : Static (per-class, rather than per-object) methods do not participate in overriding (choosing the right method at runtime based on the class of the object). Probably the best and simplest way to think about this (and to write your code) is to write every invocation of a static method using the fully qualified class name: class A { public static method1() { A.method2(); } public static method2() { } } class B extends A { public static method3() { A.method1(); } public static method2() { } } Now it is perfectly clear that the static method2() that is called is A.method2(), not B.method2(). A.method2() will be called regardless of whether you use the fully-qualified class name or not, but using “A.” makes it obvious to all.

0

Static (per-class, rather than per-object) methods do not participate in overriding (choosing the right method at runtime based on the class of the object).

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.