Can Abstract Classes have Static Methods in Java
Abstract Class
An abstract class in Java is one that explicitly uses the keyword "abstract" in its declaration. There are options for both non-abstract and abstract techniques (method with the body). A class that has been designated as being abstract is one that is such. Approaches can be both non-abstract and abstract. It needs to grow and have its plan implemented. It is not instantiable.
Points to Remember:
- The keyword abstract must be used when defining an abstract class.
- It is capable of both abstract and non-abstract methods, but it cannot be instantiated.
- It may also have static methods and constructors.
- It may have final methods, which would prevent the subclass from changing the method body.
Static Methods
Methods that will be present whether or not any instances of the class are formed are created using the static keyword. A static method is one that makes use of the static keyword. Methods that are linked to a class rather than an object are known as static methods. The keyword static is used to declare them.
Without generating a class instance, we can call static methods. Static methods are frequently used for tasks that are not specific to any one object, including validating an object's type or performing mathematical calculations.
Features of static method:
- In Java, a static method is one that is a component of a class rather than an instance of that class.
- The method is accessible to each instance of a class.
- Without requiring the class's object, static methods have access to class variables (static variables) (instance).
- A static method can only access static data. It cannot access data that is dynamic (instance variables).
- Static methods can be accessed directly in non-static methods as well as static methods.
- A static method in Java cannot be abstract. Compilation errors will result from doing this.
Example program to determine whether the abstract classes have static methods in java
Java example application for an abstract static method
import java.io. *;
// A is super class
abstract class A {
// abstract static method func () has no body
abstract static void func ();
}
//Bclass is sub class
class B extends A {
// func () method must override the class B
static void func ()
{
System.out.println("Static abstract"+ " method implemented.");
}
}
// class is a driver class
public class Demo {
public static void main (String s[])
{
// abstract class is called
// static method func ()
B. func ();
}
}
Output:
Because static methods cannot be abstract, the code above is wrong. The Compilation Error that happens during execution is:
Compilation Error:
prog.java:12: error: illegal combination of modifiers: abstract and static
abstract static void func();
^
1 error
What will happen if a static method is made abstract?
Illustrating that we abstract a static method. The procedure will then be expressed as:
public abstract static void func ();
Step 1: Because the super-class does not specify how to implement a method when it is described as abstract using the abstract type of modifier, it falls to the subclass to do so. Therefore, in order to give method definition, a subclass must override them.
Step 2: Since static members are compile-time components and overriding them turns them into runtime elements, it is now evident when a method is declared as static that it cannot be overridden by any subclass (making the static method hidden) (Runtime Polymorphism).
Let's now examine the first stage or process. The subclass is required to give a definition of the func method if it is referred to be abstract. The static func method, however, cannot be overridden in any subclass, according to step 2 of the procedure, hence it is unable to have a definition. Thus, the situations appear to be at odds with one another. Thus, our presumption that a static func method is abstract is incorrect. Consequently, a static method is unable to be abstract.
This procedure will then be coded as:
public static void func ();
Example:
// Programming in Java to show an abstract static method
import java.io. *;
// A is a super class
abstract class A {
// func () is a static method
static void func ()
{
System.out.println("Static method implemented.");
}
//func1 is a abstarct method it has no body
abstract void func1();
}
//class B is sub class
class B extends A {
// class B must override func1() method
void func1()
{
System.out.println("implemention of the abstract class is done.");
}
}
// Driver class
public class Demo {
public static void main (String s[])
{
// abstract class is called
// static method func ()
B. func();
B b = new B ();
b. func1();
}
}
Output:
Static method implemented.
implementation of the abstract class is done