Can we use static method in an abstract class?
Asked Answered
E

5

55

In Java Programming, Can we call a static method of an abstract class?
Yes I know we can't use static with a method of an abstract class. but I want to know why.. ?

Emmuela answered 1/7, 2013 at 14:42 Comment(0)
A
60

In Java you can have a static method in an abstract class:

abstract class Foo {
   static void bar() { }
}

This is allowed because that method can be called directly, even if you do not have an instance of the abstract class:

Foo.bar();

However, for the same reason, you can't declare a static method to be abstract. Normally, the compiler can guarantee that an abstract method will have a real implementation any time that it is called, because you can't create an instance of an abstract class. But since a static method can be called directly, making it abstract would make it possible to call an undefined method.

abstract class Foo {
   abstract static void bar();
}

// Calling a method with no body!
Foo.bar();

In an interface, all methods are implicitly abstract. This is why an interface cannot declare a static method. (There's no architectural reason why an interface couldn't have a static method, but I suspect the writers of the JLS felt that that would encourage misuse of interfaces)

Amygdaline answered 1/7, 2013 at 14:54 Comment(3)
Note: as of java 8, you can declare static and default methods in an interfaceCounterforce
@Counterforce Not quite - the static method on the interface must be implemented on the interface. This: public interface Rabbit { static boolean doIt (); } …results in error: Compilation failure [ERROR] /Users/…/Rabbit.java:[4,42] missing method body, or declare abstractGasaway
@BasilBourque yes of course. Sorry that wasnt clear. static methods must always be implemented where they are "declared". they're static, they don't change.Counterforce
A
26

If you are talking about java, answer is Yes But you need to define the static method. You cannot create an abstract static method. What you can create is non abstract static method.

Reason is you do not need a object instance to access a static method, so you need the method to be defined with a certain functionality.

so you cannot have,

  abstract class AbstractClassExample{
     abstract static void method();


}  

But you can have,

abstract class AbstractClassExample{

     static void method(){}
}  

Hope this helps...

Aman answered 1/7, 2013 at 14:55 Comment(0)
B
9

Here is a simple explanation.Abstract methods must be implemented later.We know that static methods cannot be overridden because static methods do not belong to any particular instance, rather it belongs to the class.Then different implementation of abstract method,which is static, in different classes is counter-intuitive.

Bendix answered 27/5, 2016 at 22:31 Comment(0)
C
7

Yes, of course you can define the static method in abstract class. you can call that static method by using abstract class,or by using child class who extends the abstract class.Also you can able to call static method through child class instance/object. To illustrate further test following example.

//Parent class
public abstract class TestAbstractClass {
    static void testStaticMethod(){
        System.out.println("In Parent class static method");
    }
}

//child class
public class ChildClass extends TestAbstractClass {
    public static void main(String[] args) {
        TestAbstractClass parentObj = new ChildClass();
        parentObj .testStaticMethod();
        ChildClass childObj = new ChildClass();
        childObj.testStaticMethod();
        TestAbstractClass.testStaticMethod();
        childClass.testStaticMethod();
    }
}
Clydesdale answered 26/8, 2015 at 14:50 Comment(0)
H
0

From Java 9 onwards you can have static methods in an interface. However, the implementation must be provided in the block itself. Unlike static methods in a class, a static method in an interface is not inherited by implementation through a class or subinterface.

An abstract can contain a static method. It is because a static method though not overridden can be hidden.

But an abstract method cannot be declared static at the same time as an abstract method must be overridden ans implemented by a subclass's method and declaring it static will prevent overriding. In other words, you cannot use abstract and static keywords to declare the same method. However, you can have a static method inside an abstract class.

Haggai answered 16/7, 2019 at 8:31 Comment(1)
Actually, from Java 8 onwards we can have static methods in interfaces and not 9.Zeidman

© 2022 - 2024 — McMap. All rights reserved.