As per the old dictionary, abstraction is the quality of dealing with ideas rather than events. For example, we can consider the case of an e-mail, where already complex details such as what happens as soon as you send an e-mail, the protocol your e-mail server uses are hidden from the user.
Therefore, to send an e-mail you just need to type the content, mention the address of the receiver, and click send that’s it.
Same as in Object-oriented programming, abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user.
In other words, we can say that the user will have the information on what the object does instead of how it does it.
In Java, abstraction is achieved by two things which are Abstract classes and Interfaces.
Abstract Class in Java
A class that contains mainly abstract keyword in its declaration is known as an abstract class. But, if a class has at least one abstract method, then the class must be declared to be abstract directly and it cannot be instantiated. To using this abstract class, you have to just inherit it from another class, provide implementations to the abstract methods in it. If you inherit this class, you have to provide implementations to all the abstract methods in it.
abstract class Bollywood{ //abstract method public abstract void sound(); } //Dog class extends Animal class public class Actor extends Bollywood{ public void sound(){ System.out.println("Yaa Hooo !!!"); } public static void main(String args[]){ Actor obj = new Actor(); obj.sound(); } } Output : Yaa Hooo !!!
Interface in Java
The interface is just like a reference type in Java. It is the main collection of abstract methods. A class only implements an interface, thereby inheriting the abstract methods of that interface.
Interface may also contain constants, default methods, static methods, and nested types. Method bodies mainly exist only for default methods and static methods.
interface Bollywood { public void eat(); public void travel(); } public class Actor implements Bollywood { public void eat() { System.out.println("Actor eats"); } public void travel() { System.out.println("Actor traveling"); } public int noOfLegs() { return 0; } public static void main(String args[]) { Actor m = new Actor(); m.eat(); m.travel(); } } Output : Actor eats Actor traveling
Difference Between Interface and Abstract Class in Java
- The major difference is that methods of a Java interface only implicitly abstract and cannot have implementations. So, the abstract class can have instance methods that implement a default behavior.
- The variables that declared in a Java interface are by default final. An abstract class may contain non-final variables only.
- Members of a Java interface are mainly public by default. A Java abstract class basically has the usual flavors of class members just like private, protected, etc..
- The “implements” keyword is used for implemented in Interface Java and abstract class in java should be extended using the “extends” keyword.
- The interface is mainly abstract and cannot be instantiated and Java abstract class also cannot be instantiated but can be invoked if a main() exists.
- A Java class can use to implementing multiple interfaces but it can extend only one abstract class.
- We can use interface to extend another Java interface only, but the abstract class can be used to extend another Java class and implement multiple Java interfaces.
- In comparison with java, abstract classes with java interfaces are slow as it requires very extra indirection.