×

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

How to remove last character from String in Java

In java, there are predominantly three classes connected with the string. The classes are String, StringBuilder, and StringBuffer class that gives techniques connected with string control. Eliminating the first and...

5 minutes read.

Economical number in Java

Economical number is said to be economically efficient if the number of digits after prime factorization of the original number with their powers is less than the number of digits...

3 minutes read.

Difference between String Tokenizer and split Method in Java

Introduction Today, let us understand the difference between String Tokenizer and Split Method. First, let us learn about the String Tokenizer and Split method individually and then know about their differences String...

7 minutes read.

Convert IP to Binary in Java

Fundamental conversion, such as going from binary to decimal or vice versa, is a crucial activity in computers. Understanding IP addressing and subnetting is crucial for networking. The primary networking...

4 minutes read.

Zebra Puzzle Problem in Java

Complex puzzles like the zebra puzzle demand a lot of work and mental training to complete. Because it was created by renowned German scientist Albert Einstein, it is also sometimes...

10 minutes read.

Java Integer toOctalString() method

The toOctalString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 8. Syntax public static String toOctalString (int  i) Parameters The parameter ‘i’ represents...

1 minute read.

Hidden classes in Java

There specifically are some APIs available in the market that generally is harmful to be used in our programs specifically literally, and until JDK 15, there, for all intents and...

4 minutes read.

Calculator Program in Java

Calculator Program in Java A calculator performs the basic mathematical operations such as addition, subtraction, multiplication, etc. In this section, we will create a calculator program in Java using switch case...

5 minutes read.

Pernicious Number in Java

If the number of 1s in a number appearing in the binary representation is the prime number, such a number is known as a pernicious number. A pernicious number always corresponds to...

4 minutes read.

Kong Java Client

Kong is an Organization Microservice Programming interface gateway. Kong gives an adaptable deliberation layer that safely oversees correspondence among clients and microservices by means of a Programming interface. Otherwise called...

6 minutes read.

Loose Coupling in Java

Loosely coupling mechanism in java means one reference of a variable capable of holding multiple implementation class memory is called loosely coupling. Or in other words, one interface reference variable...

3 minutes read.

Java Null Keyword

Null is a term that is only used for literal values in Java. Although it appears to be a term, it is a literal opposite of true and false. Java's...

3 minutes read.

Constructor Overloading in Java

In Java, constructors can be overloaded just like methods. The idea of having multiple constructors with various parameter sets so that each function Object()  can carry out a particular task...

3 minutes read.

All the important string methods in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Diffie Hellman Algorithm in Java

In this section, you will be acknowledged about Diffie Hellman algorithm clearly step wise along with an example and also an example program. Diffie Hellman Algorithm One of the most significant algorithms...

3 minutes read.

Java IO

It is a part of java libraries but is often known as I/O streams, file I/O, and file handling. The Java I/O concept satisfies the need for input processing and...

4 minutes read.

Web Crawler in Java

In this article, you will be acknowledged with what a web crawler in java is and what are its functions. You will also be able to understand where to implement...

4 minutes read.

Method Overriding in Java

Overriding  Overriding is also known as run time polymorphism, and it is about the same method with the same signatures in different classes. Overriding can be applied only methods. Method Overriding Method Overriding...

8 minutes read.

Array Programs in Java

Array Programs in Java: An array is a data structure that stores similar elements in a contiguous memory location. In Java, an array is an object that stores the same...

7 minutes read.

Class definition in Java

The class definition in Java Java is an object-oriented programming language. We essentially know that programming languages based on object-oriented paradigms have classes and objects in their concepts as main, which...

6 minutes read.