You are on page 1of 6

Abstract Keyword

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:

1. abstract class Employee


2. {
3. abstract void work();
4. }

Rules of abstract keyword


Don'ts
1. An abstract keyword cannot be used with variables and constructors.
2. If a class is abstract, it cannot be instantiated.(Object not created)
3. If a method is abstract, it doesn't contain the body.
4. We cannot use the abstract keyword with the final.
5. We cannot declare abstract methods as private.
6. We cannot declare abstract methods as static.
7. An abstract method can't be synchronized.
8. We cannot declare abstract methods in non abstract class.

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. abstract class Vehicle


2. {
3. abstract void bike();
4.
5. }
6. class Honda extends Vehicle
7. {
8.
9. void bike() {
10. System.out.println("Bike is running");
11.
12. }
13.
14. }
15.
16. public class AbstractExample1 {
17.
18. public static void main(String[] args) {
19.
20. Honda obj=new Honda();
21. obj.bike();
22. }
23. }

Abstract class containing the abstract and non-abstract method

1. abstract class Vehicle


2. {
3. abstract void bike();
4.
5. void car()
6. {
7. System.out.println("Car is running");
8. }
9.
10. }
11. class Honda extends Vehicle
12. {
13. void bike() {
14. System.out.println("Bike is running");
15.
16. }
17. }
18. public class AbstractExample2 {
19.
20. public static void main(String[] args) {
21.
22. Honda obj=new Honda();
23. obj.bike();
24. obj.car();
25. }
26. }

Abstract class containing the constructor

1. abstract class Vehicle


2. {
3. String msg;
4.
5. Vehicle(String msg)
6. {
7. this.msg=msg;
8. }
9.
10. void display()
11. {
12. System.out.println(msg);
13. }
14.
15. }
16. class Honda extends Vehicle
17. {
18.
19. Honda(String msg) {
20. super(msg);
21.
22. }
23.
24. }
25. public class AbstractExample3 {
26.
27. public static void main(String[] args) {
28.
29. Honda obj=new Honda("Constructor is invoked");
30. obj.display();
31. }
32. }

Abstract class containing overloaded abstract methods

1. abstract class Vehicle


2. {
3.
4. abstract void display();
5. abstract void display(String msg);
6.
7. }
8. class Honda extends Vehicle
9. {
10.
11. void display() {
12.
13. System.out.println("abstract method is invoked");
14. }
15.
16. void display(String msg) {
17.
18. System.out.println(msg);
19. }
20.
21. }
22. public class AbstractExample4 {
23.
24. public static void main(String[] args) {
25. Honda obj=new Honda();
26. obj.display();
27. obj.display("overloaded abstract method is invoked");
28. }
29. }

Inner abstract class

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

1. abstract class Vehicle


2. {
3. abstract class Car
4. {
5. abstract void display();
6. }
7. }
8. class Honda extends Vehicle
9. {
10. class FourWheller extends Car
11. {
12. void display() {
13.
14. System.out.println("nested abstract class is invoked");
15. }
16. }
17. }
18. public class AbstractExample6 {
19.
20. public static void main(String[] args) {
21. Vehicle obj=new Honda();
22. Honda h=(Honda)obj;
23. Honda.FourWheller fw=h.new FourWheller();
24. fw.display();
25. }
26. }

You might also like