×

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

Related Topics

Types of Sockets in Java

The fundamental idea behind Java's networking capability is the socket. Early in the 1980s, the Berkeley UNIX 4.2BSD version included the socket paradigm. Berkeley socket is the term employed as...

9 minutes read.

Java String trim() method

Java String trim() method returns String after eliminating leading and trailing spaces. Note:- Java String trim() method doesn't trim or omit middle spaces. Syntax: public String trim() Returns: It returns string with omitted leading and...

2 minutes read.

Application of Array in Java

In this article we are going to acknowledge about what the array is, types of arrays and their applications. What is an array? An array is often a set of interrelated elements...

4 minutes read.

Java Boolean toValue() Method

The valueOf() method of Java Boolean class returns a Boolean object representing the given Boolean or String value. It returns true, if the specified Boolean or string object is true...

2 minutes read.

AbstractSet Class in Java

The AbstractSet class is used for the implementation of the Abstract Collection class and interface. It is the part of Collection Frameworks. In the AbstractSet, the implementation is same as the...

3 minutes read.

Program to check whether a given character is present in a string or not

In this article, you will understand the logic to find out whether the given character is present in the string or not and find out the position of the specified...

3 minutes read.

AES 256 Encryption in Java

Now a days, security has grown in importance. Java programming supports a variety of encryption and hashing methods, which offers security for data transport and communication among various nodes. In...

4 minutes read.

Banking Application in Java

JDBC (Java Database Connectivity), which provides an API to connect to, execute, and fetch data from any databases, can be used to handle transactions in Java. There are several factors...

7 minutes read.

What is interpreter in Java?

The programming language Java is platform-neutral. Therefore, can use Java on any platform that supports the Java processor. The Piece of software transforms the Java bytecode contained in the class...

5 minutes read.

Minimum Lights to Activate Java Snippet Class

Minimum Lights to Activate Problem in Java In prison, there is a hallway that is N units long. Given an N-dimensional array A. If the light at the ith position is...

3 minutes read.

Stable Marriage Problem in Java

Given N men and N women, the Stable Marriage Problem asks you to match up the men and women in such a way that there are never any two people...

6 minutes read.

Uses of Java

Java is used in many real-world Java applications, including technologies and tools. This Java programming language has become the backbone for developing many applications. In areas like embedded systems and...

3 minutes read.

3N+1 problem program in Java

The 3N+1 problem is a hypothesis in the field of abstract mathematics (not yet proven). Collectively known as the Collatz problem. This tutorial will describe about the 3N+1 problem and...

3 minutes read.

Design of JDBC

Java applications may interface using database systems from many vendors using the Java Database Connectivity (JDBC) Application Software Interface (API) from Sun Microsystem. To connect spreadsheets, JDBC and database drivers...

3 minutes read.

Davis Staircase Problem in Java

Davis has several stairs in his home and prefers to ascend one, two, or three steps at a time. As a highly clever youngster, he thinks about how many ways...

3 minutes read.

Interface Program in Java

Interface Program in Java In the previous topic, we discussed that abstraction is possible through the interface and abstract class. An abstract class provides partial to 100% abstraction. 100 %abstraction in...

4 minutes read.

Java String substring() method

Java String substring() method returns a part of the String. Syntax: public String substring(int startIndex) public String substring(int startIndex, int endIndex) Parameters: startIndex : starting index endIndex : ending index Returns: It returns a specified String. Throws: StringIndexOutOfBoundsException if start...

2 minutes read.

How to Convert Date to String in Java

How to Convert Date to String in Java We need to convert Date to String in Java may for displaying purpose. We can convert Date to String in Java using the format() method of java.text.DateFormat class. There...

2 minutes read.

RMI program in Java

Remote Method Invocation is what it stands for. An object can call the method of another object in a different space address using the RMI API, which may be on the...

3 minutes read.

String isnullorempty in Java

What do you mean by String? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.