Skip to main content

Interfaces - Need to know:

What is an Interface?

An Interface is like a class in that it can have methods and variables. However, methods declared in an Interface are abstract by default, meaning they only contain the method signature, not the method body.


Facts about Interfaces:

  • Interfaces specify what the class must do and not how the class must do it. They are considered as the class blueprint.
  • If a class implements an interface, the class must provide all the method bodies for the interface's functions. Otherwise, the class must be declared abstract.
  • They are used to achieve total abstraction.
  • An Interface can extend one or many Interfaces. A class can extend many Interfaces, but only one class.
  • Nested Interfaces is when an Interface is declared inside another interface.
  • It is also used to achieve loose coupling.
  • Interfaces are used to implement abstraction. 


Difference between Abstract Class and Interface:

Differences between an Abstract class and an Interface include:

  1. An interface can have strictly abstract methods. In comparison, an abstract class can have abstract and non-abstract methods. 
  2. An abstract class doesn't support multiple inheritances, whereas an interface does.
  3. Interface variables are only static and final. In contrast, an abstract class can have final, non-final, static and non-static variables.  
  4. An abstract class can provide the implementation of an interface. However, an Interface can't provide the implementation of an abstract class.
  5. The interface keyword declares an interface, and the abstract keyword declares an abstract class.
  6. An interface can only extend another Java interface. However, an abstract class can extend another Java class as well as implement multiple Java interfaces.
  7. The interface can be implemented using the keyword "implements". The abstract class can be extended using the keyword "extends". 

Can you declare a constructor inside an Interface?

No, you cannot have a constructor within an interface in Java. A Constructor initializes the non-static members of a particular class concerning an object, therefore:

  • You can only have public, static, final variables and abstract methods.
  • An interface doesn't have a constructor because all data members in an interface are public static final by default. They are constants.
  • The interface has no data members that the constructor can initialize.
  • To call a method, we need an object. Since the interface methods don't have a body, there is no need for calling the methods in an interface.
  • Since we cannot call the interface methods, there is no need to create an object for an interface, and there is no need to have a constructor.


Popular posts from this blog

Executer vs ExecuterSevice: What's the Difference?

Executer and ExcuterService: Executor and ExecutorService are part of Java's Executor framework, which provides  thread pool  facilities to Java applications. Since creating and managing Threads are expensive, It's a better idea to create a thread pool that can execute in parallel rather than every time a new thread is requested, helping reduce the load. This improves the response time of the application. Differences between Executer and ExecuterService: One of the key differences between the Executor and ExecutorService interface is that the former is a parent interface while ExecutorService extends Executor.  A second significant difference between ExecutorService and Executor is that Executor defines an execute() method, which accepts the Runnable interface object. Simultaneously, a submit() method takes both Runnable and Callable object interfaces. Another difference between Executor and ExecutorService interface is that the execute() methods' return type is void, wh...

What happens if we don't override the run() method:

Consequences of not overriding the run() method: If we don’t override the run() method, the compiler will not flash any error, and it will execute the run() method of the Thread class that has been emptily implemented, So, there will be no output for this Thread. Below is an example of the method returning no output: public class Example extends Thread { public static void main(String[] args) { Thread thread = new Thread( new Example()); thread.start(); // will create a new thread and call thread class's run() method which has no implementation. } }

Creating and Running a Java Thread:

Ways to create a Thread: You can create a Thread in two ways: By extending the Thread class. By implementing a Runnable Interface. Thread class: The Thread class provides constructors and methods to create and perform operations on a thread. Runnable Interface: Runnable Interfaces have only one method,  named run() . The Runnable Interface should be performed by classes where their instances plan to be executed by a thread.  public void run() : is used to perform the action for a Thread. Starting a thread: The  start() method  of a Thread class is used to start a newly created thread. It performs the following tasks: A new thread starts(with their stack). The Thread moves to the Runnable state from New state. The  run() method  will run when the Thread gets a chance. Java Thread Example by extending Thread class: class  Multi  extends  Thread{   public   void  run(){   System.out.println( "Extended Thread Class E...