2

Why cant we declare an instance method in sub Class B which shares the same signature of a static method in parent Class A?

It throws a compile time error if i try to do that.

My question is, since static method of parent class is restricted to parent class, why does instance method of child class does not compile.

Lets see by code:

`

public class A{
     static void testStatic(){}
}


public class B extends  A{
      void testStatic (){}
}


public class Test{

public static void main (String[] args){

        A a = new B()

        a.testStatic();

}

`

In the above code,since A does not have an instance method by that name, and since Java allows static methods to be accessed by objects, Object a of type 'A' pointing to 'B' can call static method present in it(class A). But complier throws an error "The instance method cannot override a static method" why?

Note: I can understand if a class does not allow same method name for two methods, even if one is instance and other is static. But I fail to understand why it does not allow a sub class to have an instance of same name. Especially considering the fact that static methods cannot be overridden. And yet, Java allows subclass to have same name as parent class static method, which is called information hiding, but not overriding.

javaAndBeyond
  • 520
  • 1
  • 9
  • 26

6 Answers6

3

The compiler throws an error because those are the rules of the language. From the Java Language Specification §8.4.8.2:

If a class C declares or inherits a static method m, then m is said to hide any method m', where the signature of m is a subsignature (§8.4.2) of the signature of m', in the superclasses and superinterfaces of C that would otherwise be accessible to code in C.

It is a compile-time error if a static method hides an instance method.

(emphasis in the original). The language is dense (as in most places in the JLS) but it matches the situation you are describing. The JLS doesn't provide a rationale for this rule that I could find on first reading. But a little thought about how one might try to make this rule unnecessary shows why it's there.

Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Thanks for the answer. I can understand if a class does not allow same method name for two methods, even if one is instance and other is static. But I fail to understand why it does not allow a sub class to have an instance of same name. Especially considering the fact that static methods are not inherited, cannot be overridden. And yet, Java allows subclass to have same name as parent class static method, which is called information hiding, but not overriding. – javaAndBeyond Dec 30 '15 at 07:32
  • Suppose some other code in your class `B` contains a call `testStatic();`. How is the compiler supposed to figure out whether you meant `this.testStatic();` (an instance method call) or `A.testStatic();` (a static method call)? Note that even the JLS uses language suggesting that `static` methods are inherited. It's legal to access `A.testStatic()` using `B.testStatic()` (although lint tools will warn about that). It's just a guess, but I think the language designers thought that allowing instance methods to hide `static` methods as you describe was just too hard to get right. – Ted Hopp Dec 30 '15 at 07:45
1

It's illegal in Java. The call to the static method is allowed on an instance as well, so there'd be no way to distinguish which method to call in some cases:

  A a = new A ();
  B b = new B ();
  A.testStatic (); // ok
  B.testStatic (); // ok
  a.testStatic (); // ok calling static
  b.testStatic (); // undefined. What to call? Based on what?

Last call is the reason why it's not allowed.

Jan
  • 13,738
  • 3
  • 30
  • 55
  • why cant last line call testStatic instance method in class B? – javaAndBeyond Dec 30 '15 at 07:37
  • Then what would line before call? This situation is undefined, so the language was setup to prevent this by issuing compile errors as you got. – Jan Dec 30 '15 at 07:41
  • My point is, if B has testStatic as a static method, then compiler allows it and b.testStatic calls static method of class B. Why cant the same way be followed here? – javaAndBeyond Dec 30 '15 at 07:42
  • If B's method was static as well, it would hide method from A **in all cases** - calls 2 and 4 from my list. But with non-static thats not the case. – Jan Dec 30 '15 at 07:47
1

Calls to static methods are resolved at compile time itself. So, they cannot be overridden. And by defining another method with the same signature as the static method, the compiler complains.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • you can define in subclass with same name, even if its a static method. Its called information hiding. But my case in point is different. – javaAndBeyond Dec 30 '15 at 07:34
1

static method is bound with class whereas instance method is bound with object. Static belongs to class area and instance belongs to heap area.

Khan Abdulrehman
  • 816
  • 2
  • 10
  • 22
1
I am not hundred percent sure but I guess answer as below.

Static method means it can be used without an instance of the the class in which it is defined. Also static method can access only static variables of the class. Now if we override non static method and create an instance of sub class with reference of the super class, compiler will be confused for above two basic functioning of static method. Please debate if any thing wrong in this.

Ketan Bhavsar
  • 5,338
  • 9
  • 38
  • 69
0

4 line showing error. so do this public class B extends A