Abstraction in Java
An Abstraction is a process of exposing all the necessary details and hiding the rest. In Java, Data Abstraction is defined as the process of reducing the object to its essence so that only the necessary characteristics are exposed to the users.
Abstraction defines an object in terms of its properties (attributes), behaviour (methods), and interfaces (means of communicating with other objects).
Abstract Classes in Java
-
An Abstract class is a class whose objects can’t be created. An Abstract class is
created through the use of the abstract keyword. It is used to represent a concept. -
An abstract class can have abstract methods (methods without body) as well as non-abstract methods or concrete methods (methods with the body). A non abstract class cannot have abstract methods.
-
The class has to be declared as abstract if it contains at least one abstract method.
-
An abstract class does not allow you to create objects of its type. In this case, we can only use the objects of its subclass.
-
Using an abstract class, we can achieve 0 to 100% abstraction.
-
There is always a default constructor in an abstract class, it can also have a parameterized constructor.
-
The abstract class can also contain final and static methods.
Abstract class: It is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
Abstract method: It can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).
Examples:
abstract class Demo {
//abstract method
abstract void display();
}
//extends the abstract class
public class MainClass extends Demo {
//defining the body of the method of the abstract class
void display() {
System.out.println("Abstract method called.");
}
public static void main(String[] args) {
MainClass obj = new MainClass();
//invoking abstract method
obj.display();
}
}
Interface in Java
An interface in java is a blueprint of a class. It has static constants and abstract methods. The interface in java is a mechanism to achieve abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve abstraction and multiple inheritances in Java.
Declaring Interfaces
The interface keyword is used to declare an interface. Here is a simple example to declare an interface.
Example:
public interface NameOfInterface {
// Any number of final, static fields
// Any number of abstract method declarations
}
Example
interface printable {
void print();
}
class A6 implements printable {
public void print() {
System.out.println("Hello");
}
public static void main(String args[]) {
A6 obj = new A6();
obj.print();
}
}
Multiple inheritance in Java by interface
If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known as multiple inheritance.
interface Printable {
void print();
}
interface Showable {
void show();
}
class Kalokalam implements Printable, Showable {
public void print() {
System.out.println("Hello");
}
public void show() {
System.out.println("Welcome");
}
public static void main(String args[]) {
Kalokalam obj = new Kalokalam();
obj.print();
obj.show();
}
}