Abstraction is process of hiding the implementation details and showing only the functionality. It is separating the functions and properties that logically can be separated to a separate entity which the main type depends on. It is one of the five fundamentals along with Encapsulation, Inheritance, Polymorphism, Class and Object.
Also, What is abstraction in Java, Difference between Abstraction and Encapsulation are also very popular core Java interview questions because strong OOPS skill is one of the primary requirement for Java developers.
Abstraction in java is achieved by using an interface and abstract class. Interface gives 100% abstraction while abstract class gives 0-100% abstraction. An interface or abstract class is something which is not concrete or incomplete. In order to use an interface or abstract class, we need to extend and implement all abstract method with concrete behavior. An example of Abstraction is creating an interface to denote common behavior without specifying any details about how that behavior works.
Ways to achieve Abstaction
There are two ways to achieve abstraction in java:- Abstract class (0 to 100%)
- Interface (100%)
Abstract Class
A class that is declared as abstract is known as abstract class. It is something which is incomplete and you can not create instance of abstract class. If you want to use it, you need to make it complete or concrete by extending it. A class is called concrete if it doesn't contain any abstract method and implements all abstract method inherited from extended abstract class or implemented interface. In Java, we can have abstract classes, abstract methods but a variable can not be abstract.Syntax to declare an abstract class
abstract class{}
Abstract Method
An abstract method in Java doesn't have body , its just a declaration. In order to use abstract method you need to override that method in SubClass.Syntax to define an abstract method
abstract return_type();//no braces{}
Abstract Class Examples
Example 1 :( Without abstract method)
abstract class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } class Employee extends Person { private String empCode; public String getEmpCode() { return empCode; } public void setEmpCode(String empCode) { this.empCode = empCode; } }
Example 2: (with abstract method)
abstract class Person { public abstract void run(); } class Employee extends Person { public void run(){ System.out.println("Running.."); } }
Interface in Java
An interface is a blueprint of a class. Interface is an another way of providing abstraction and provides full abstraction. Interfaces are by default abstract and only contains public static, final constant or abstract methods.An interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements. All the methods of the interface need to be defined in the class that implements the interface.
An interface is different from a class in several ways:
- You cannot instantiate an interface.
- An interface does not contain any constructors.
- All of the methods in an interface are abstract.
- An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
- An interface is not extended by a class; it is implemented by a class.
- An interface can extend multiple interfaces.
Syntax to declare an interface
public interface{}
Interface Examples
interface Animal { public void eat(); public void travel(); } public class MammalInt implements Animal { public void eat(){ System.out.println("Mammal eats"); } public void travel(){ System.out.println("Mammal travels"); } public int numberOfLegs(){ return 0; } }
When overriding methods defined in interfaces there are several rules to be followed:
- Checked exceptions should not be declared on implementation methods other than the ones declared by the interface method or subclasses of those declared by the interface method.
- The signature of the interface method and the same return type or subtype should be maintained when overriding the methods.
- An implementation class itself can be abstract and if so interface methods need not be implemented.
- A class can implement more than one interface at a time.
- A class can extend only one class, but implement many interfaces.
- An interface can extend another interface, similarly to the way that a class can extend another class.
When do you use abstraction?
When you know something needs to be there but not sure how exactly it should look like.Advantages of Abstraction
- We can separate the things that can be grouped to another type.
- Frequently changing properties and methods can be grouped to a separate type so that the main type need not undergo changes. This adds strength to the OOAD principle -"Code should be open for Extension but closed for Modification".
- Simplifies the representation of the domain models.
Summary
- Use abstraction, if you know something needs to be in class but implementation of that varies.
- In Java, you cannot create instance of abstract class , it gives a compiler error.
- abstract is a keyword in java.
- interface is a keyword in java.
- A class automatically becomes abstract class when any of its method declared as abstract.
- Abstract method and methods of an interface doesn't have method body.
- Variable cannot be made abstract, only class and methods can be abstract.
- If a class extends an abstract class or interface it has to provide implementation to all its abstract method to be a concrete class. Otherwise, that class also needs to be declared as abstract.
Please feel free to post your comments if you have any question. Also, if you find any error then please let me know.
0 comments