Java Interview Question Set 1
Categories: Java 8(JDK1.8)
What is the interface?
The interface is a blueprint for a class that has static constants and abstract methods. It can be used to achieve full abstraction and multiple inheritance. It is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java. In other words, you can say that interfaces can have abstract methods and variables. Java Interface also represents the IS-A relationship. It cannot be instantiated just like the abstract class. However, we need to implement it to define its methods. Since Java 8, we can have the default, static, and private methods in an interface.
Can you declare an interface method static?
No, because methods of an interface are abstract by default, and we can not use static and abstract together.
Can the Interface be final?
No, because an interface needs to be implemented by the other class and if it is final, it can't be implemented by any class.
What is a marker interface?
A Marker interface can be defined as the interface which has no data member and member functions. For example, Serializable, Cloneable are marker interfaces. The marker interface can be declared as follows.
public interface Serializable{
}
What are the differences between abstract class and interface?
Abstract classInterface
An abstract class can have a method body (non-abstract methods).The interface has only abstract methods.
An abstract class can have instance variables.An interface cannot have instance variables.
An abstract class can have the constructor.The interface cannot have the constructor.
An abstract class can have static methods.The interface cannot have static methods.
You can extend one abstract class.You can implement multiple interfaces.
The abstract class can provide the implementation of the interface.The Interface can't provide the implementation of the abstract class.
The abstract keyword is used to declare an abstract class.The interface keyword is used to declare an interface.
Can we define private and protected modifiers for the members in interfaces?
No, they are implicitly public.
When can an object reference be cast to an interface reference?
An object reference can be cast to an interface reference when the object implements the referenced interface.
How to make a read-only class in Java?
A class can be made read-only by making all of the fields private. The read-only class will have only getter methods which return the private property of the class to the main method. We cannot modify this property because there is no setter method available in the class. Consider the following example.
//A Java class which has only getter methods.
public class Student{
//private data member
private String college="AKG";
//getter method for college
public String getCollege(){
return college;
}
}
How to make a write-only class in Java?
A class can be made write-only by making all of the fields private. The write-only class will have only setter methods which set the value passed from the main method to the private fields. We cannot read the properties of the class because there is no getter method in this class. Consider the following example.
//A Java class which has only setter methods.
public class Student{
//private data member
private String college;
//getter method for college
public void setCollege(String college){
this.college=college;
}
}
What are the advantages of Encapsulation in Java?
There are the following advantages of Encapsulation in Java are :-
a) By providing only the setter or getter method, you can make the class read-only or write-only. In other words, you can skip the getter or setter methods.
b) It provides you the control over the data. Suppose you want to set the value of id which should be greater than 100 only, you can write the logic inside the setter method. You can write the logic not to store the negative numbers in the setter methods.
c) It is a way to achieve data hiding in Java because other class will not be able to access the data through the private data members.
d) The encapsulate class is easy to test. So, it is better for unit testing.
What is the package?
A package is a group of similar type of classes, interfaces, and sub-packages. It provides access protection and removes naming collision. The packages in Java can be categorized into two forms, inbuilt package, and user-defined package. There are many built-in packages such as Java, lang, awt, javax, swing, net, io, util, sql, etc. Consider the following example to create a package in Java.
//save as Simple.java
package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}