Abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it does it. In Java programming, abstraction is achieved using Abstract classes and interfaces.
Example: ATM GUI, Mobile GUI, Windows GUI etc...
Let’s take, ATM GUI (Graphical User Interface) screen.
Bank people are highlighting the set of services what they are offering without highlighting internal implementation.
The main advantages of Abstraction are:
There are two ways to achieve abstraction in java
abstract is a keyword, which is applicable to classes and methods. If we declare a class as abstract then will call it
as a abstract class and if a declare a method as abstract then will call it as a abstract method, these methods does not
have any implementation. Using abstract class we cannot achive 100% abstraction. abstract classes will have both
abstract and concret methods.
abstract method: A methods doesn't have the body. Its implementation will be in child classes.
concret method: These are the normal methods which contain the body.
we cannot instantiation abstract classes, if we want to use the methods available in abstract class, through child class only will be able to access it.
// Class 1
package com.java.session.six;
public abstract class AbstractExamples {
public void display() { // normal method
System.out.println("display method");
}
public abstract String absMethod(); // abstract method
}
// Class 2
package com.java.session.six;
public class Examples extends AbstractExamples {
public void test() {
display();
}
public static void main(String[] args) {
Examples e = new Examples();
e.test();
String str = e.absMethod();
System.out.println(str);
}
@Override
public String absMethod() {
return "Overridden abstract method";
}
}
Java interface is a collection of abstract methods. The interface is used to achieve 100% abstraction in which you can define methods without their implementations. Inside the interface will have only abstract methods. Inside the interfaces all the methods by default public and abstract it means we no need to specify the methods as public abstract. We cannot instantiate interfaces. Thorugh child class implementation we can access the methods of an interface. Inside interfaces we can also declare variables, by default all the variables inside the interfaces are constants (static fianl).
public interface NameOfInterface {
// Any number of final, static fields
// Any number of abstract method declarations\
}
interface Animal {
public void eat();
public void travel();
}
If we want to use methods inside the interface, we need to implement the interface so that we can access all the data members. implements is a keyword which only used to implement the interfaces in the java program. Two interfaces cannot implement it can only extends. See below examples
Two interfaces can only extends
interface Car {
public void speed();
public void insurance();
}
interface Bike extends Car {
public void speed();
}
interface Animal {
public void eat();
public void travel();
}
class Mamals implements Animal {
@Overrides
public void eat() {
System.out.println("Mamal is eating");
}
@Overrides
public void travel() {
System.out.println("Mamal will change it location bases on the weather conditions");
}
}