- On
- By
- 0 Comment
- Categories: keyword, Keyword in Java
The abstract keyword in Java language
The abstract keyword in Java language
Here, we learn about The abstract keyword in Java language.
The abstract is a keyword in Java programming language. it can be applied to class and methods
when a class is declared as abstract it called as the abstract class. In other words, if a class has any abstract methods, the entire class must be declared as abstract.
for example
abstract class My_class{ }
- The keyword abstract in Java can be used for method(called as the abstract method) declaration.
for example
abstract int mymetod();
- When a class is declared as abstract, it cannot be instantiated.
- When a method is declared as abstract, it is known as the abstract method. This does not have any implementation. The abstract method declaration must be ended with a semicolon.
- Abstract classes must have one or more abstract methods. However, the abstract class does not necessarily require all its methods to be abstract.
Syntax
the syntax of the abstract class given below
//declare a abstract class abstract class my_class{ //class properties }
When a class is declared abstract, then its methods may also be declared abstract.
the syntax of the abstract method given below
//diclare a abstract method abstract my_Method (list_of_parameters);
When a method is declared abstract, the method does not have a definition and ends with a semicolon.
Example
abstract class My_Class{ //class diclared as abstract abstract void my_Method();//abstract method inside the class void method_One(){ //non abstract method with implementation System.out.println("This is a non abstract method"); } } class Second_Class extends My_Class{ void my_Method(){ System.out.println("Implementation of abstract method"); } } class Test_Class2{ public static void main(String[] args){ My_Class obj=new Second_Class(); obj.my_Method(); obj.method_One(); } }
When the above code is executed, it produces the following results
implementation of abstract method This is a non abstract method
There are other Java language keywords that are similar to this keyword
Suggested for you