Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about static or private method hidden/redefined in subclass

 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

and if you change B

result will be "From B"
Private or static method that call from another method chosen by class that originally declare caller private or static method? Right?
 
Sergei Zhylinski
Ranch Hand
Posts: 84
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

As I see from the code, replacing 'static' modifier to 'private' will lead to the same results. But the reason for both cases is different. I have replaced 'static' modifier into 'private' and got instance methods:



The overriden method in class 'B' is commented. Hence, the new instance of 'B' will call inherited method 'meth()'. Private method 'privateMeth()' of class 'A' is not inherited by class 'B' as it is private. But method A.meth() does something. Its behavior was exposed to private helper method A.privateMeth(). But when the method A.meth() is overriden in 'B', this overriding class 'meth()' relies on his own functionality, therefore it may have its own private methods. Names of these private methods can be the same as of class 'A', but they are not either hidden or overriden.

And it in case with 'static' modifiers, static method of class 'B' hides static method of class 'A'.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all your code comments are incorrect!
Sergej Smoljanov wrote:// if you changed static to private
//or add private to staticMeth in A, you can not mark as static


This code defined a private static method in A, and in class B you can have a static method with the same name without any problem. The following code compiles!


This is the code of your other suggestion (replacing static with private in class A) and again this code compiles successfully!


But if you have a static method (with at least default access in class A), you can not have an instance method in subclass B with the same method signature, as the following code illustrates:

The compiler error is:
staticMeth() in B cannot override staticMeth() in A
private void staticMeth() {
^
overridden method is static
1 error


The other way around is also not allowed. So if you have an instance method (with at least default access in class A), you can not have a static method in subclass B with the same method signature. Again illustrated in this code snippet:

You'll get a slightly different compiler error:
staticMeth() in B cannot override staticMeth() in A
private static void staticMeth() {
^
overriding method is static
1 error


Hope it helps!
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i didn't see "shadowing" mentioned, which is the way to explain what you're seeing. Static methods aren't overridden, they're shadowed. Polymorphism is only for overridden methods, nothing else. If you remove static in both cases, you'll eliminate the shadow and create an override in its place it will be polymorphism going for the win!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic