• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

why these code can compile?/

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class KK{
static void doStuff()
{System.out.print("kk");}
}

class KK1 extends KK{
static void doStuff()
{System.out.print("kk1");}
}

I remembered that static methods can't be override,but why thest code can compile.I hope someone can help me,because I am preparing for the SCJP exam.
By the way,what's the difference between override and redefinition.
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

We have a strict policy on display names, which must be a real first and last name with a space between.

Please go here and fix your display name up, pronto. Thanks, pardner!
 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are correct static methods cannot be overridden.

<code>
---------------------------------------
class KK{
static void doStuff()
{System.out.print("kk");}
}

class KK1 extends KK{
static void doStuff()
{System.out.print("kk1");}
}

public class Test
{
public static void main(String args[])
{
KK obj=new KK1();
obj.doStuff();
}
}
------------------------------------
case 1 : if you remove keyword static from the method

If you try to make superclass reference pointing to subclass object.it always invoke a subclass overidden method.

case 2: In static methods. It is not invoking overridden method. Instead it will invole superclass method because static methods cannot be overridden.

I hope this will be helpfull for you

Satyajit Kadam





 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generally, a non-static method can't be overriden with a static method & vice versa. A static method is not overriden, but, it is hidden by a method in the subclass. The same rules hold for method hiding as for method overriding EXCEPT that polymorphism is not applicable.

With reference to your example, the code compiles, because, the method in KK1 class will hide the method of class KK. All the restrictions that apply to overriding, also apply to method hiding. For example,
(i) if you make the method doStuff() in class KK1 to be non-static, you will get an error;
(ii) if you make the method doStuff() in class KK1 to be private, you will get an error;
(iii) if you include a throws clause, throwing checked exceptions, in the method doStuff() in class KK1, you will get a compile-time error;
(iv) if you make the return-type incompatible in the method doStuff() in class KK1, you will get a compiler error.

So, all restrictions applying in the case of method overriding, also apply to method hiding. The difference between the two is subtle in that method hiding occurs in static methods, which belong to classes, not to individual objects. Thus a call to a static method via an object is equivalent to a call to the same method, through the class' name. This leads to the conclusion that polymorphism is not available in static methods. The actual method to be called is NOT determined at runtime, but, at compile-time (early binding).

Taking your example, the above difference between the two can be demonstrated very easily:-


The output obtained is:-

Inside foo() of class KK1.
Inside bar() of class KK.
Inside bar() of class KK1.
Inside bar() of class KK.
Inside bar() of class KK1.

Hope, I have succeeded in clarifying your concepts.
[ November 14, 2006: Message edited by: Abdul Rehman ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic