Abstraction

Abstraction is a process of hiding the implementation details and showing only the functionality to the user. In other words abstraction may also be defined as the process of seeing only the required features of an object and disregard the irrelevant details. In Java, abstraction is achieved using abstract classes and interfaces.

Abstract Class

An abstract class can have abstract methods(Methods without body) and also have methods with implementation.
Below are few points through which we can understand abstract class more clear.
1. An abstract class is declared with abstract keyword.
2. An abstract class can have constructors.
3. An abstract class may or may not have all abstract methods. Some of methods can be non abstract or concrete methods.
4. An abstract method is declared without an implementation or without any body.
5. An abstract method must defined in its subclass or make subclass itself abstract.
6. We can not directly instantiate or create object of an abstract class.
7. Any class that contains one or more abstract methods must also be declared with abstract keyword.

Abstract class syntax:

abstract class AbstractTest {
  abstract void testDrive(); //Abstract method with no body
  void testCheck() {
    System.out.println("This is for checking purpose"); //Method with body 
  }
}

Abstract method:

Let us see an example:

abstract class Drive {
  abstract void testDrive(); //Abstract method with no body
}
class Bike extends Drive {
  void testDrive() {
    System.out.println("Test drive for Bike");
  }
}
class Car extends Drive {
  void testDrive() {
    System.out.println("Test drive for Car");
  }
}

class AbstractTest {
  public static void main(String[] args) {
    Drive bikeDrive = new Bike();
    bikeDrive.testDrive();

    Drive carDrive = new Car();
    carDrive.testDrive();
  }
}

Output:

Test drive for Bike
Test drive for Car

As we have discussed earlier that Abstract class can have constructors. Sometime interviewer ask us like why do abstract classes have constructors in Java?

Answer

Abstract classes are basically used to initialise the data value and enable users to provide customised value to variable which are used in concrete classes. And when the object of respective class created these fields are called and provided the user defined value irrespective of value provided in concrete classes.

Interface

An interface is a reference type or a blue print of a class in Java. It is a collection of abstract methods and constants. Along with abstract methods and constants, interface can also contains default or static methods and nested types(From JDK 1.8). A class can implements an interface, thereby inheriting the abstract methods of the interface. With the help of interface we can achieve abstraction and multiple inheritance in Java.

Note:

We can not instantiate or create object of interface as similar to abstract class.

Interface syntax:

interface <Interface_name>{
  // declare constant fields
  // declare abstract methods
}

Interface data fields are public, static and final by default, and its methods are public and abstract.

Example:

interface drawable
{
  int LENGTH=10; //public static final int LENGTH=10;
  void draw();   // public abstract void draw();
}

 
Let’s see an example:

interface Driving {

	void drive();

}

class Car implements Driving {

	public void drive() {

		System.out.println("Car driving");

	}

}

class Bus implements Driving {

	public void drive() {

		System.out.println("Bus driving");

	}

}

class TestDrive {

	public static void main(String args[]) {

		Driving d = new Car();
		d.drive();

	}

}
Output:

Car driving

Key points:

  • We can not instantiate an interface.
  • An interface is implemented by class not by extended.
  • An interface can extend multiple interfaces.
  • An interface does not contain constructors.
  • An interface is also saved as similar to Java program like .java file and also after compiling the code it converted into the .class file.

Why we use interface?

Multiple inheritance is not possible in Java because of ambiguity. But it is possible through interface because the method implementation is described in the implementation class.

Marker interface:

An interface which has empty body or we can say which has no members is known as marker or tagged interface.

For example: Cloneable. Serializable, Remote etc, they are used to provide some essential information to the JVM so that JVM perform some useful operation on it.

Declaration:
public interface Cloneable {
}