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

DWQA QuestionsCategory: JavaCan we declare the static variables and methods in an abstract class?
Editor Staff asked 10 months ago

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

1 Answers
Editor Staff answered 10 months 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