Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Static method overriding

  Asked By: Carolina    Date: Feb 16    Category: Java    Views: 576
  

Can a static method of a parent class be overridden in subclass in Java?

class A{
public static int method1(int aiSrc){
// code here
}
}

class B extends A {
// code here specific to sub class

public static int method1(String asSrc){
// code here
}
}

referring to the above example , is it possible to write this code?
is it called overrding or hiding?
can I say that signature of method1 is same in both classess?

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Emma Brown     Answered On: Feb 16

For code  like this, the compiler and debugger are your best friends.....
Try to compile and run the code :)

"Compilers understand the code better than humans do"

 
Answer #2    Answered By: Willie Gomez     Answered On: Feb 16

This code  will compile fine with no error.

However what you have written is not method  overriding



To override method you must declare them with same signature

I.e.

1) The return type, method name, and number and type of the parameters for
the overriding  method must match those in the overridden method.

2) The overriding method can have a different throws clause as long as it
doesn't declare any types not declared by the throws clause in the
overridden method.

3) Access specifier for the overriding method can allow more access than the
overridden method, but not less. For example, a protected method in the
superclass can be made public but not private.

 
Didn't find what you were looking for? Find more on Static method overriding Or get search suggestion and latest updates.




Tagged: