• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

static method overridden rule

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi peopple,

I know that non-static method in superclass cannot be overridden to be a static method in the subclass. But I just cant remember this rule. Can anyone please expalin to me why so I can remember it.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Method overriding is done to avail the polymorphic behavior in method call.
First remember you don't use term overriding for static method redefinition.
If super class is having static method, you re-implement that method in the
subclass to give your own definition to that method.

Non-static methods can only be overridden or overloaded by the non-static
method of the subclass. You can't put static keyword before the method
name in the subclass if the method is defined in the super class.

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ciao Wonkty,

I guess that the way to remember it is quite easy and is called "method signture" + "class inheritance".
One method, inside a class and/or its subclasses, is identified uniquely by:
1) the method name
2) the parameters AND their order.
Plus there are some rules to remember because of the inheritance. So for the signature it is easy to remember.

Everithing have be consistent with the situation such that, given two class A and B extends A; if you do declare a will use the overridden methods of B class but that methods have to be consistent with the role that A has choosen first, that is:
1) this method has (for example) at minimum protected visibility
2) this method has this parameters
3) this method will return this kind of paramter
4) this method will throws at least a IOException (so the subclass can throw a FileNotFoundException but not an a Exception)


So for inheritance the following operations on a method override by a subclass are legal:
1) override it (trivial)
2) increase it visibility (for example if the parent's method is protected, the subclass can override the method and promote it to public)
3) throw less general exceptions than the parent's method
4) make it final (because regards the childrens of subclass)
5) make it synchronized (because regards the threads's treatement)

This mean that you will have a compilation error if in the subclass method:
1) change the return type (not consistent with the rules of A)
2) make it static (not consistent with the rules of A)

I hope that all this discussion made you feel better

ciao
Maurizo
 
Maurizio Nagni
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ciao Wonkty,

I guess that the way to remember it is quite easy and is called "method signture" + "class inheritance".
One method, inside a class and/or its subclasses, is identified uniquely by:
1) the method name
2) the parameters AND their order.
Plus there are some rules to remember because of the inheritance. So for the signature it is easy to remember.

Everithing have be consistent with the situation such that, given two class A and B extends A; if you do declare a will use the overridden methods of B class but that methods have to be consistent with the role that A has choosen first, that is:
1) this method has (for example) at minimum protected visibility
2) this method has this parameters
3) this method will return this kind of paramter
4) this method will throws at least a IOException (so the subclass can throw a FileNotFoundException but not an a Exception)


So for inheritance the following operations on a method override by a subclass are legal:
1) override it (trivial)
2) increase it visibility (for example if the parent's method is protected, the subclass can override the method and promote it to public)
3) throw less general exceptions than the parent's method
4) make it final (because regards the childrens of subclass)
5) make it synchronized (because regards the threads's treatement)

This mean that you will have a compilation error if in the subclass method:
1) change the return type (not consistent with the rules of A)
2) make it static (not consistent with the rules of A)

I hope that all this discussion made you feel better

ciao
Maurizo
 
He baked a muffin that stole my car! And this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic