Abstract and Interface Interview Question in Java
Interview questions
What is an abstract class?
In java, an abstract class which cannot be instantiated, which means it cannot make objects out of it. It act as a foundation or model for classes that augment in it.
A class that is abstract may have both abstract and non-abstract methods. Abstract methods lack a body because they are stated without an implementation. Any subclass that extends the abstract class must implement these methods
Can we create an instance of an abstract class? Why not, if not?
An abstract class cannot be created as an instance. It is incomplete and contains abstract methods, therefore it must be subclassed and have those methods implemented before it can be instantiated.
What distinguishes an interface from a Java abstract class?
In java, an interface can have both abstract and non-abstract methods, as contrast to an abstract class which cannot. Means a class may implement any number of interface but it may only extend one or more abstract class.
Can constructors exist in abstract classes? If so, how do they function?
The answer is that a constructor can exist in an abstract class. When subclass objects are created, these constructors are used to set their shared states to starting values. Using the super() keyword, the constructor of the subclass calls the constructor of the abstract class.
In Java, can an abstract class be final? If not, why not?
An abstract class cannot be marked as final in Java. Although an abstract class must be sub-classed in order to provide implementations for its abstract methods, an abstract class cannot be sub-classed if it is designated as final.
Can non-abstract methods be used in an abstract class?
Yes, both abstract and non-abstract methods can be found in an abstract class. An abstract class has implemented non-abstract methods that offer shared functionality that subclasses can inherit.
What do abstract methods serve in an abstract class?
The correct response is that abstract methods are defined but not implemented in an abstract class. Subclasses are supposed to give their own implementation by overriding them. Subclasses must adhere to the contract that abstract methods define.
In Java, may a class extend more than one abstract class?
In Java, a class cannot directly extend more than one abstract class. Java does not provide class multiple inheritance. A class, however, can implement numerous interfaces.
Is it possible to create an abstract class without implementing its abstract methods?
No, you cannot create an abstract class without also creating its abstract methods. All the abstract methods inherited from the abstract class must have concrete implementations before they can be created in subclasses.
How does Java's abstract class support code reuse and inheritance?
By specifying shared methods and characteristics, an abstract class serves as a common foundation for related classes. Subclasses can override abstract methods to offer particular behaviour by extending an abstract class, which allows them to inherit these shared characteristics. This encourages code reuse and permits object-oriented programming to use a hierarchical structure.
Can an abstract class in Java use an interface?
The answer is true since an abstract class in Java is capable of implementing an interface. As a result, the abstract class has the ability to independently implement the interface's abstract methods in addition to inheriting them.
Can a non-abstract method in a subclass be overridden to become abstract?
No, a non-abstract method cannot be changed in a subclass to become an abstract one. A non-abstract method cannot be replaced with an abstract method since an implementation is required.
Can an abstract class be made without abstract methods?
Yes, a class can be abstract without having any abstract methods. However, since an abstract class is often used when there are abstract methods that need to be inherited and implemented by subclasses, it is advised to use an interface in these situations rather to an abstract class.
Can we make an abstract method in an abstract class private or static?
What does a Java interface mean?
In Java, a reference type called an interface defines a set of abstract methods. Classes that implement the interface must abide by the contract it provides.
What distinguishes a Java class from an interface?
In contrast to a class, an interface cannot be instantiated directly. It only includes constants, nested types, and method signatures. In contrast, a class can be created and can have instance variables as well as method implementations.
In Java, may a class implement more than one interface?
In Java, a class can implement several interfaces, the answer is yes. As a result, the class is able to provide implementations for the abstract methods specified in each interface by inheriting them.
Can one interface extend to another?
A Java interface can extend one or more other interfaces, hence the answer is yes. As a result, the abstract methods and constants from the parent interface(s) can be inherited by the extending interface.
Can instance variables be used in an interface?
In Java, instance variables are not allowed on interfaces. It can only include static, final constant variables that are implicitly public.
Can we make an interface instance in Java?
No, we are unable to construct interface instances. Interfaces cannot be implemented since they are abstract by nature. But it is possible to instantiate a class that implements the interface.
Can non-abstract (default) methods be present in an interface?
Certainly, as of Java 8, an interface may contain non-abstract methods. These methods offer a default implementation that implementing classes can inherit and are defined using the term default.
Can private methods exist on an interface?
Yes, as of Java 9, an interface may contain private methods. Common utility methods that can be shared by default or static methods within the interface are provided via private methods in interfaces.
In Java, can an interface inherit from a class?
No, a Java interface cannot directly descend from a class. Only other interfaces can be extended via interfaces. If a class and an interface need to share a common set of functions,
In Java, can an interface extend a class?
The answer is that in Java, an interface cannot extend a class. To provide an implementation for its methods, a class can implement an interface.
Can the methods of an interface have private or protected access modifiers?
No, an interface's methods are all automatically public. Any class that implements the interface should be able to access them. Modifiers for private or protected access are not permitted.
Can a constructor be used on an interface?
No, a constructor cannot be part of an interface. There is no need for a constructor because interfaces are not intended to be instantiated.
In Java, is it possible to mark an interface as final?
No, you cannot declare an interface to be final. Classes are intended to implement and extend interfaces. An interface's implementation and extension would be prevented by marking it as final.
Can an interface pass on its own traits?
An interface cannot directly inherit from itself, hence the answer is no. Due to circular inheritance, it would cause a compilation error. A hierarchy of inheritance allows an interface to indirectly inherit from itself through other interfaces.
Is it possible to extend several interfaces that have incompatible default methods?
A class that implements both interfaces will have to supply its own implementation for that method if two or more interfaces have default methods with the same signature. By overriding the method and delivering the desired implementation, it must end the conflict.
Can an interface implement another interface while extending a class at the same time?
No, a Java interface cannot implement another interface and extend a class at the same time. Java does not provide class multiple inheritance. The extends keyword allows one interface to extend multiple interfaces, though.
In Java, can an interface have static methods?
Yes, as of Java 8, an interface may contain static methods. Without the need for an implementing class, static methods in interfaces can be invoked directly on the interface itself. They offer generic utility methods that can be used with any implementation.