What is the difference between an interface and an abstract class in Java?

In Java, an interface and an abstract class are both used to define a contract for classes that implement them, but they have some key differences:

  • An interface only contains method signatures and constants, whereas an abstract class can contain method signatures, constants, and also implemented methods, fields and constructors.
  • All methods declared in an interface are public and abstract by default, whereas in an abstract class, they can have any access level and can be either abstract or concrete.
  • A class can implement multiple interfaces, but can only inherit from one abstract class.
  • An interface cannot have any constructor, whereas an abstract class can have constructors.
  • An interface cannot be instantiated, whereas an abstract class can be instantiated with the use of the “new” keyword.
  • In general, interfaces are used to define a contract for functionality that multiple classes should implement, whereas abstract classes are used when you want to provide a base implementation for a certain functionality and allow other classes to inherit and extend it

What is the purpose of the final keyword in Java?

  • Final variables: A variable declared as final cannot be reassigned a new value. This is useful when you want to make sure a variable’s value stays constant throughout the program’s execution.
  • Final methods: A method declared as final cannot be overridden by subclasses. This is useful when you want to prevent subclasses from modifying the behavior of a method defined in a superclass.
  • Final classes: A class declared as final cannot be extended by other classes. This is useful when you want to prevent other classes from inheriting the properties and methods of a class.
  • Finalize method: The finalize() method is called by the garbage collector on an object when garbage collection determines that there are no more references to the object. The method can be used to perform cleanup activities, such as releasing resources.
  • Final parameter: A parameter can be declared final, indicating that the method cannot change the value of the parameter.

In general, the final keyword is used to indicate that something cannot be changed or overridden, making the code more robust and predictable.

Can you explain the difference between a constructor and a method in Java?

In Java, a constructor and a method are both used to define operations that can be performed on an object, but they have some key differences:

  • A constructor is used to create and initialling an object, whereas a method is used to perform operations on an object after it has been created.
  • A constructor does not have a return type, whereas a method has a return type or void.
  • The name of a constructor must be the same as the name of the class, whereas a method can have any name.
  • A constructor is called when an object is created using the “new” keyword, whereas a method is called by using the name of the method on an object.
  • A class can have multiple methods but can only have one or multiple constructors, depending on the implementation.

In summary, a constructor is a special type of method that is used to create and initialise an object, whereas a method is used to perform operations on an object after it has been created. Constructors are called only once when an object is created whereas methods can be called multiple times to perform different actions on the object.

How do you handle exceptions in Java?

In Java, exceptions are handled using try-catch blocks. The code that may throw an exception is placed in a try block, and the exception handling code is placed in a catch block

try {
   // code that may throw an exception
} catch (ExceptionType e) {
   // exception handling code
} finally {
   // code to be executed always
}

You can also define your own custom exception class by extending Exception class and use it in your code.

What is the use of the transient keyword in Java?

the transient keyword is used to indicate that a field should not be serialized. When an object is serialized, its state is converted to a byte stream and saved to disk, or sent over a network. By default, all non-static and non-transient fields of an object are serialized. However, sometimes there may be fields that should not be serialized, for example, fields that are derived from other fields, or fields that are only used transiently.

By marking a field as transient, you can prevent it from being serialized, even though it would be serialized by default. When the object is deserialized, the field will be initialized with its default value (e.g. null for objects and 0 for numeric types).

Here is an example of how you can use the transient keyword to prevent a field from being serialised:

class User implements Serializable {
    private int id;
    private transient String password;
    // ...
}

In the example above, id field will be serialised and password field will not be serialized. It’s worth noting that the transient keyword only affects the serialisation process, it doesn’t affect the visibility or accessibility of the field in the code.

Can you explain the difference between a checked and an unchecked exception in Java

In Java, there are two types of exceptions: checked exceptions and unchecked exceptions

A checked exception is a type of exception that the Java compiler forces you to handle or specify that your method can throw. Examples of checked exceptions include IOException, SQLException, FileNotFoundException, and so on. If a method calls another method that throws a checked exception, the calling method must either handle the exception or specify that it can throw the exception by including it in the method’s throws clause.

public void myMethod() throws IOException {
    FileInputStream fis = new FileInputStream("file.txt");
    // ...
}

An unchecked exception is a type of exception that the Java compiler does not force you to handle or specify that your method can throw. Examples of unchecked exceptions include NullPointerException, IllegalArgumentException, and ArrayIndexOutOfBoundsException. You don’t have to include these exceptions in the throws clause of a method or catch them in a try-catch block, although it is considered good practice to do so.

public void myMethod() {
    String str = null;
    System.out.println(str.length()); // this will throw a NullPointerException
    // ...
}

In summary, the main difference between checked and unchecked exceptions is that checked exceptions are mandatory to handle or specify that a method can throw, while unchecked exceptions are optional to handle or specify.