Can we declare the static variables and methods in an abstract class?

Category: JavaCan we declare the static variables and methods in an abstract class?
Editor">Editor Staff asked 2 years ago

Can we declare the static variables and methods in an abstract class?

1 Answers
Editor">Editor Staff answered 2 years ago

Yes, we can declare static variables and methods in an abstract method. As we know that there is no requirement to make the object to access the static context, therefore, we can access the
static context declared inside the abstract class by using the name of the abstract class. Consider the following example.
abstract class Test
{
static int i = 102;
static void TestMethod()
{
System.out.println(“hi !! I am good !!”);
}
}
public class TestClass extends Test
{
public static void main (String args[])
{
Test.TestMethod();
System.out.println(“i = “+Test.i);
}
}
Output
hi !! I am good !!
i = 102
Core Java – OOPs Concepts: Inheritance Interview Questions