abstract keyword is used as a non access modifier for:
- Classes
- Methods
abstract class and abstract methods
abstract class is a class having partial implementation. It can have methods without any implementation. abstract keyword is used for the unimplemented methods.
abstract class can not be instantiated. You will get compilation error is you try to do so.
Any subclass of an abstract class must either implement all of the abstract methods in the super-class, or be declared abstract itself.
package com.java.core;
public abstract class TestAbstractClass {
public void nonAbstractMethod() {
System.out.println("Non Abstract Method");
}
public abstract void abstractMethod();
public static void main(String[] args) {
TestAbstractClass testAbstractClass = new ChildClass();
testAbstractClass.nonAbstractMethod();
testAbstractClass.abstractMethod();
}
}
class ChildClass extends TestAbstractClass {
@Override
public void abstractMethod() {
System.out.println("Overrriden Abstract Method");
}
}
Non Abstract Method
Overrriden Abstract Method
Please note that abstract keyword cannot be used with final, native, synchronized, static or private keywords.