Abstract
Abstract
Abstract
The abstract keyword is used to achieve abstraction in Java. It is a non-access modifier which is used to
create abstract class and method.
The role of an abstract class is to contain abstract methods. However, it may also contain non-abstract
methods. The method which is declared with abstract keyword and doesn't have any implementation is
known as an abstract method.
Syntax:
Do's
1. An abstract keyword can only be used with class and method.
2. An abstract class can contain constructors and static methods.
3. If a class extends the abstract class, it must also implement at least one of the abstract method.
4. An abstract class can contain the main method and the final method.
5. An abstract class can contain overloaded abstract methods.
6. We can declare the local inner class as abstract.
7. We can declare the abstract method with a throw clause.
8. We cannot declare abstract methods in non abstract class.
Abstract class containing the abstract method
1. class Vehicle
2. {
3. abstract class Car
4. {
5. abstract void display();
6. }
7.
8. class Honda extends Car
9. {
10.
11. void display() {
12.
13. System.out.println("inner abstract class is invoked");
14. }
15. }
16. }
17. public class AbstractExample5 {
18.
19. public static void main(String[] args) {
20. Vehicle obj=new Vehicle();
21. Vehicle.Car c=obj.new Honda();
22. c.display();
23. }
24. }
Nested abstract class