• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static Override?? Please Help...

 
Ranch Hand
Posts: 48
1
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read a line in Kathy Sierra that "STATIC METHOD CANNOT BE OVERRIDDEN".
I had a debate with some of my friends on this topic.

They say: "Static method can be overridden but there is no use of it because Polymorphism does not apply to it".

But i say: "No, We simply cannot override Static methods and if we do so by modifying the subclass method as static then it is not called overriding but called as redefining of the method according to Kathy Sierra"

I go over the code:



The above code throws a compile error that "aMethod()" cannot be overridden; aMethod() in A is static.

If the compiler error has been developed so, why my friends do not agree with me?
I had an argument over this topic in one of my interviews too...


PLEASE CLEAR MY DOUBT FRIENDS...
 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This should cover it for you and your friends.

http://faq.javaranch.com/view?OverridingVsHiding
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are trying to make static method in A to a non static method in B which is not allowed. Same way a non static method in superclass can not be a static method in subclass.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Srivastav
I read a line in Kathy Sierra that "STATIC METHOD CANNOT BE OVERRIDDEN".



So static methods cannot be overriden. If you try overriding a static method like the following code..



We get as an output:
A - teste()
A - teste()

So.. the teste() method was redefined not overriden.

When we talk about static method, the instance type doesn't matter once static members belongs to the class. The reference type is what matter. And both references, a and aa, are of type A.

Hope it helps! ;P
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
A very useful way of finding whether a method is actually overriden or redefined is the annotation.
Suppose you are overriding a method named myMethod() just add the annotation @Override before the method.If it can be overriden it runs else shows compilation error.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello Mr.Arkha,.., will they provide us the code with the annotation "@Override" as you mentioned in the exam,
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Annotations are not part of the SCJP exam - you will not get any questions about annotations such as @Override.
 
Senthil Kumaran
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes mr.jesper i too know that we wont get such annotations, but i was just kidding mr.arkha abt his comment ,
 
reply
    Bookmark Topic Watch Topic
  • New Topic