DEV Community

Cover image for Java Interview Questions - Part 2
Garvit Khamesra
Garvit Khamesra

Posted on

Java Interview Questions - Part 2

Part 2 of the [Java interview question series](https://dev.to/garvit_khamesra/java-interview-questions-part-1-3ba7). In this, we will cover static, abstract classes and methods, and interfaces.

Before we start with the questions I would recommend going through these 3 articles.

  1. Static Keyword in Java
  2. Final Keyword in Java
  3. How To Create An Immutable Class In Java?

Let's get started

Q21: Why is main a static method?

A21: Main method is an entry point of execution in java. JVM has been following this signature to locate the entry point. If we change the signature of the method, the program compiles but does not execute. The main() method in Java must be declared public, static, and void. If any of these are missing, the Java program will compile but a runtime error will be thrown.

Q22: Can we override static methods?

A22: No, We cannot override static methods because the binding is done at compile time. Even if we create an overridden method, it will not have any effect because runtime polymorphism is not possible.

Example:

class Parent {
    static void staticMethod() {
        System.out.println("Parent Method");
    }
}
class Child extends Parent {
    static void staticMethod() {
        System.out.println("Child Method");
    }
    public static void main(String[] args) {
        Parent parent = new Parent();
        Parent child = new Child();
        Child child1 = new Child();
        parent.staticMethod();
        child.staticMethod();
        child1.staticMethod();
    }
} 

Q23: Can we make constructors static?

A23: No, Contrustors are called when the object is created and static refers to class level properties and not the object level. Even if we try to put static keyword in front of the constructor it will give us an error.

Q24: Can we make the abstract methods static?

A24: We write abstract methods so that we can override them. So now this question is similar to Can we override static methods. For both, the answer is no with same reasoning.

Q25: What is covariant return type?

A25: Before Java5, it was not possible to override methods by changing the return type. But now, we can change the return type to the sub-class type.

Example:

class Parent {
     Parent parentMethod() {
        System.out.println("Parent Method");
        return new Parent();
    }
}
class Child extends Parent {
     Child parentMethod() {
        System.out.println("Child Method");
        return new Child();
    }
    public static void main(String[] args) {
        Parent parent = new Parent();
        Parent child = new Child();
        Child child1 = new Child();
        parent.parentMethod();
        child.parentMethod();
        child1.parentMethod();
    }
} 

Q26: Can we declare main method as final?

A26: Yes, we cab have main method as final. The compiler will not give any error and it will work also. But the final keyword is used such that the method cannot be overridden. But as main method already have static which makes it not supported for override.

Q27: Can we declare a constructor as final?

A27: We cannot declare constructor as final, the compiler will throw an error.

"Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding."

And final keyword is used to prevent overriding, so adding final will not make sense.

Q28: Can we declare an interface as final?

A28: No, we cannot declare an interface as final because the interface must be implemented by some class to provide its definition, and the use of final is to prevent that behavior. Therefore, there is no sense to make an interface final. If we try to do so, the compiler will show an error.

Q29: Can we declare an abstract method as final?

A29: We cannot declare an abstract method as final, because final is used to prevent overriding and abstract methods must be overridden by a subclass. If we try to do so, the compiler will show an error.

Q30: Can we declare an abstract method as private?

A30: We cannot use a private modifier with an abstract method, because a private modifier restricts access to the given class and the abstract methods must be overridden by a subclass. If we try to do so, the compiler will show an error.

#HappyLearning

Top comments (0)