Mech D6 Home Assignment

 Interfaces and Abstract Classes in JAVA

 

 


While learning Java and its concepts, we have come across the concept called ‘Interfaces’. 

The interface in Java can be defined as the blueprint of the class. An interface can have abstract methods and static constants. By using the interface, we achieve abstraction in java. Interfaces are one of the key features of JAVA and every java developer should know its use and applications. Though when Interfaces come into the picture, they have many advantages and disadvantages.

 

So first let’s dive into understanding interfaces. When implementing interfaces, the first term we come across is abstract classes. Now, what are abstract classes? Why they are needed? What is the interface? What are the uses of Interfaces? Why do interfaces use abstract classes in them? We will get all these answers in this blog post.

 

What are Interfaces?

The interface is a special mechanism in java that helps to achieve abstraction. It is the blueprint of a class. It has static constants and abstract methods. Though interfaces can only have abstract methods(only methods with no body).

In java, the interface keyword is used to declare the interface. Consider the following syntax to declare the interface.

Interface <interface-name> {  

//constant fields  

//abstract methods  

}  

Like class, the interface can also inherit another interface. However, a class implements the interface. Consider the following image to understand the relationship between class and interface.


From Java 9 onwards, interfaces can contain the following also:

  • Static Methods
  • Private Methods
  • Private Static Methods

Let’s ignore technical words for now and go to simple logic to understand the interface which is used to achieve abstraction.

 

So, What is Abstraction?

Let’s take a real-life example, we all use mobile phones. Whenever we switch on our phones the first task to do is to set up a password. During unlocking, when we input our password, pin, or whatever security key we have and then the phone unlocks.

 

Now here is an answer to abstraction, we know the mobile unlocks after we input the password or security key, but we don’t know how that system is working in the backend. After inputting the button what’s happening. This behavior of completing tasks successfully without displaying to the user what is actually in the backend is known as Abstraction.

 

We can achieve abstraction using interfaces and abstract classes in java.

 

Why do we use an Interface?

 


There are mainly three reasons to use the interface. They are given below.

  • It is used to achieve total abstraction.
  • Since java does not support multiple inheritances in the case of class, by using the interface, we can support the functionality of multiple inheritances.
  • It can be used to achieve loose coupling

 

How to use an interface?

The interface is declared by using the ‘interface’ keyword. It provides abstraction means it declares the structure of the class. All methods in an interface are abstract and by default are set as public, static, and final. Whichever class is implementing the interface must implement all the methods declared in an interface.

Interface <interface-name> {  

//constant fields  

//abstract methods  

}  

Similar to interfaces abstraction can be achieved using abstract classes also.

 

What are Abstract Classes?

Abstract classes are classes with an ‘abstract’ keyword before them. They contain abstract as well as concrete (methods with the body) methods. Abstract classes can not be instantiated, they need to be extended and their methods implemented.

Points to Remember

  • An abstract class must be declared with an abstract keyword.
  • It cannot be instantiated.
  • It can have a final method but an abstract method itself cannot be a final method.
  • It can have abstract and non-abstract methods.
  • It can have constructors and static methods also.

  


Example of an Abstract class that has an abstract method:

In this example, Smartphone is an abstract class that contains only one abstract method run. Its implementation is provided by the Android class.














                     The abstract class has a constructor, data member, and methods:

An abstract class can have a data member, abstract method, method body (non-abstract method), constructor, and even main() method.

 

Example of an abstract class that has abstract and non-abstract methods  

















Now the main question arises, interfaces and abstract classes both help in abstraction but which one we should use more?

Java doesn’t support multiple inheritances as c++ does. We can achieve multiple inheritances using interfaces.

For abstraction:

Abstract Classes help = 1 to 100%

Interfaces help = 100%

 

Java Interface Example

In this example, the Drawable interface has only one method. Its implementation is provided by Rectangle and Circle classes. In a real scenario, an interface is defined by someone else, but its implementation is provided by different implementation providers. Moreover, it is used by someone else. The implementation part is hidden by the user who uses the interface.

 

Interface declaration: by the first user  

Multiple inheritances in Java by interface

If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritances. 

Multiple inheritances are not supported through class in java, but it is possible by an interface, why?

As we have explained in the inheritance, multiple inheritances are not supported in the case of class because of ambiguity.

However, it is supported in the case of an interface because there is no ambiguity. It is because its implementation is provided by the implementation class.

 

We hope you understood the concept behind interfaces and why they are used?

 

Reference:

https://www.javatpoint.com/abstract-method-in-java

https://www.javatpoint.com/class-and-interface-in-java

https://www.geeksforgeeks.org/abstraction-in-java-2/

https://www.geeksforgeeks.org/interfaces-in-java/?ref=gcse

 

Blog by students of,

Vishwakarma Institute of Technology, Pune

Second Year Mechanical Department

Division D, Batch 1, Group 6

    Rohit Tamale (33)

    Tanaya Barabde (35)

    Tejas Tambe (36)

    Tejas Kasawe (37)

    Pranav Terkar (38)

 

Leave a comment and share your thoughts.

 

 

 

 

Comments

  1. It's good but not up to the mark . Good efforts though . Many things to improve but can be ignored . Better luck next time

    ReplyDelete
  2. Excellent work......Nicely written

    ReplyDelete
  3. I finally understand this topic now, thanks to your nice way of explaination !

    ReplyDelete
  4. Well explained concepts and nice writing.

    ReplyDelete

Post a Comment