in JAVA by
Q:
Can we declare the static variables and methods in an abstract class?

1 Answer

0 votes
by
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
Click here to read more about JAVA
Click here to read more about Insurance

Related questions

0 votes
asked Dec 7, 2020 in JAVA by SakshiSharma
0 votes
asked Oct 17, 2020 in JAVA by rahuljain1
+2 votes
asked May 31, 2020 in JAVA by SakshiSharma
0 votes
asked May 24, 2019 in JAVA by rajeshsharma
+1 vote
+2 votes
0 votes
asked Oct 13, 2020 in JAVA by SakshiSharma
...