Javatpoint Logo
Javatpoint Logo

Can Abstract Classes Have Static Methods in Java?

Java classes that cannot be instantiated but can offer a set of methods and attributes for their concrete subclasses to implement are known as abstract classes. Abstract classes are frequently utilized to build a group of similar classes that share some behaviors but differ in other ways. If they wish to extend, other classes must supply the implementation for any abstract methods specified in the abstract class.

A common query in Java development is whether abstract classes can have static methods. Yes, static methods can exist in abstract classes. They are frequently used to implement utility functions or other class-level features.

In Java, abstract classes specify shared characteristics and behavior for a collection of classes. Although these classes cannot be instantiated, subclasses can extend them and then provide their implementations for the abstract methods the abstract class has specified.

Class-level functions known as static methods can be invoked without first generating an instance of the class. They have frequently been used for procedures or utility functions that don't rely on the state of an object.

Here's an example of an abstract class with a static method:

In this case, the Shape class has an abstract function called area() and a private color field. Additionally, it contains a static function called printColor() that outputs the color field's value.

It should be noted that the color field has been defined as private, so the static function cannot access it directly. We must also make the color field static to access it from a static method:

Explanation

In this instance, we have an abstract class named Shape. The constructor of this class sets a private static field named color. Additionally, each concrete subclass of Shape must implement the class's abstract function area(). The class also has a static method named printColor() that outputs the color field's value.

As there are no instances of the class to access instance-specific properties or methods, static methods in abstract classes can only access static fields and methods. Since there are no instances of the class to reference, the color field is also set in the constructor using the class name.

Advantages of static method use in abstract classes

  1. Utility methods: Static methods may offer common utility operations for all subclasses. For instance, any subclass that extends the Shape class can utilize the printColor() method in the example above of the Shape class. As the same method need not be implemented in every subclass, this can decrease code duplication and save time.
  2. Functionality at the class level: Static methods in abstract classes can offer functionality at the class level independent of any instance-specific attributes. Instead of being implemented independently in each concrete subclass, a static method that determines the area of a shape might be implemented in an abstract Shape class.

Drawbacks to employing static methods in abstract classes:

  1. Tight coupling: Because static methods cannot be overridden, they might lead to a tight coupling between the abstract class and its subclasses. The class system may become more rigid and challenging to sustain.
  2. Thread safety: Because all instances of the class share static methods and variables, they may cause problems with thread safety.

AbstractExample.java

Output:

Number of objects created: 2

Explanation

An abstract class called AbstractClass contains a constructor that increases the count static private variable each time a new instance is generated. The class also provides a static method getCount() that returns the total number of objects generated, as well as an abstract function doSomething() that needs be implemented by its concrete subclasses.

Concrete classes ConcreteClass1 and ConcreteClass2 implement the doSomething() function and extend AbstractClass.

The getCount() static function of the AbstractClass is used by the Main class to determine the total number of objects generated after it produces two objects of various concrete classes.

The count variable is increased by 1 when the constructor of the AbstractClass is invoked when the Main class generates an instance of ConcreteClass1. Similarly, the constructor of AbstractClass is called once more when an object of ConcreteClass2 is generated, increasing the count variable to 2.

The getCount() static function of the AbstractClass is finally used by the Main class to obtain the total number of instances generated. The count variable's value, 2, is returned by the getCount() function.

We can keep track of the number of objects generated by utilising a static variable and method in the abstract class, and we can access the value from any subclass of the AbstractClass.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA