abstract Keyword Java
abstract keyword is used to achieve abstraction in Java. it’s use to create abstract class and method.
- abstract class is to contain abstract methods
- contain non-abstract methods
- must used abstract keyword
abstract class Employee
{
abstract void work();
}
Roles
- An abstract keyword cannot be use with variables and constructors.
- If a class is abstract, it cannot be instantiated.
- If a method is abstract, it doesn’t contain the body.
- We cannot use the abstract keyword with the final.
- We cannot declare abstract methods as private.
- We cannot declare abstract methods as static.
Example code
abstract class Animal
{
abstract void cat();
}
class dog extends Animal
{
@Override
void cat() {
System.out.println("cat running");
}
}
public class AbstractExample1 {
public static void main(String[] args) {
dog obj=new dog();
obj.cat();
}
}
Output
cat running