javaAndBeyond
javaAndBeyond

Reputation: 540

static and instance method in java -Parent and Sub class

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.

Upvotes: 2

Views: 936

Answers (6)

Ketan Bhavsar
Ketan Bhavsar

Reputation: 5396

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.

Upvotes: 1

Khan Abdulrehman
Khan Abdulrehman

Reputation: 854

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

Upvotes: 1

akash Chauhan
akash Chauhan

Reputation: 11

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

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234795

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.

Upvotes: 3

TheLostMind
TheLostMind

Reputation: 36304

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.

Upvotes: 1

Jan
Jan

Reputation: 13858

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.

Upvotes: 1

Related Questions