Abstraction
Abstraction
Data abstraction is the process of hiding certain details and showing only essential information
to the user.
Abstraction can be achieved with either abstract classes or interfaces
The abstract keyword is a non-access modifier, used for classes and methods:
Abstract class: is a restricted class that cannot be used to create objects (to access it, it
must be inherited from another class).
Abstract method: can only be used in an abstract class, and it does not have a body. The
body is provided by the subclass (inherited from).
System.out.println("Zzz");
From the example above, it is not possible to create an object of the Animal class:
To access the abstract class, it must be inherited from another class. Let's convert the Animal
class we used in the Polymorphism chapter to an abstract class:
Remember from the Inheritance chapter that we use the extends keyword to inherit from a class.
Example
// Abstract class
// Regular method
System.out.println("Zzz");
class Main {
myPig.sleep();
To achieve security - hide certain details and only show the important details of an object.