It is all about abstraction...

 

Q: What is an interface in Java and why would you, as a software developer, use interfaces?

A: So far, we have got an idea of what a class is in Java. Let’s say that an interface is like a class; however, it has no attributes but contains abstract methods. If a programmer has created different classes and wanted these classes to have the same methods. It is too time-consuming to write all those methods repeatedly for each class. In this case, interfaces come in handy. One can implement one interface, containing all those methods, in all of those classes and the job is done.

Q: What is the difference between an abstract class and an interface?

In many aspects, abstract classes and interfaces are similar and both are used to achieve abstraction. However, methods declared in abstract classes are not only abstract methods, but they can also be public, private, and protected (like a doodle() method in the example below). The fields declared with abstract classes are also not only static and final like in interfaces.

abstract class Pencil {

            abstract void draw();

            abstract void write();

            protected void doodle() // protected method

            {

                        System.out.println("I also doodle");

            }

}

public class Abstraction extends Pencil {

 

            public static void main(String[] args)

            {

                        Abstraction example = new Abstraction();

                        example.draw();

                        example.write();

                        example.doodle();

            }

            @Override

            void draw() {

                        System.out.println("I draw");              

            }

            @Override

            void write() {

                        System.out.println("I write");              

            }                                              

}

Q: Why abstraction is an important concept and what role do interfaces play in abstraction?

A: What is abstraction? It is a process of hiding certain functionality from end-users. Why do we need it? When we want to make something secure for example. In the case, of bank transactions, we just need to make the payments safe, and the users don’t need to see how it is exactly working.

If one needs, for example, to have similar classes with shared functionality, then it is probably better to use abstract classes. But if you want to have completely unrelated classes have the same methods, then interfaces are the choice.  

Why do we need interfaces? Interfaces and Abstract classes are used to achieve abstraction. By implementing interfaces and using abstract classes we incorporate abstraction and increase the functionality of different classes.

It gives the classes flexibility to use the methods defined in the interfaces. If a class for example does not have a method (for example, how to create a thread in a multi-thread application), there is no need to create it from scratch if there is an available interface that can be implemented. A class “implements” interfaces (“implements” is a keyword for classes to use interfaces):

class Mytest implements Runnable

{

public void run() 

{

}

}

In this example, Mytest class implements an interface Runnable, which has an abstract method run() that Mytest class has to define (it has to adopt all the methods that this interface has). 

Q: What a class must do to implement an interface?

A class needs to use the keyword "implements" like in the example above.

Q: What are abstract methods?

A: They are the methods without a body (which means they will have no curvy brackets {}, the method will end with a semicolon (;)). Let’s see that in the example below with methods draw() and write():

 

abstract class Pencil {

            abstract void draw();

            abstract void write();

}

public class Abstraction extends Pencil {

 

            public static void main(String[] args)

            {

                        Abstraction example = new Abstraction();

                        example.draw();

                        example.write();                     

            }

            @Override

            void draw() {

                        System.out.println("I draw");              

            }

            @Override

            void write() {

                        System.out.println("I write");              

            }                                              

}

Q: Can we instantiate an interface?

Nope, interfaces cannot be instantiated (objects cannot be created within interfaces) because they don’t have attributes. 

Q: Can We declare a constructor inside an interface? Why or why not?"

Interfaces cannot have a constructor either. Constructors are used to initialize objects with certain attributes; since there are no attributes; hence is no constructor.  

Q: Can we override an interface method with visibility that is not public?

 The class that implements the methods from the interface has to have the same signature (parameters) as in the interface, which means that methods have to be public.



Sources: 

https://www.tutorialspoint.com/java/java_interfaces.htm

https://docs.oracle.com/javase/tutorial/java/IandI/override.html 

https://www.tutorialspoint.com/java/java_interfaces.htm

 

 

Comments