English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
A method without a subject is called an abstract method. It only contains a semi-colon and a comma before it.AbstractMethod signature of the keyword.
public abstract myMethod();
To use an abstract method, you need to inherit it by extending its class and providing an implementation for it.
A class that contains 0 or more abstract methods is called an abstract class. If it contains at least one abstract method, it must be declared as abstract.
Therefore, if you want to directly prevent the instantiation of a class, you can declare it abstract.
Since an abstract class cannot be instantiated, its instance methods cannot be accessed either. You can only call the static methods of an abstract class (since they do not require an instance).
abstract class Example{ static void sample() { System.out.println("static method of the abstract class"); } public void demo() { System.out.println("Method of the abstract class"); } } public class NonStaticExample{ public static void main(String args[]) { Example.sample(); } }
Output Result
static method of the abstract class
The only way to access a non-static method of an abstract class is to extend it, implement the abstract method (if any), and then use an object of the subclass to call the required method.
abstract class Example{ public void demo() { System.out.println("Method of the abstract class"); } } public class NonStaticExample extends Example{ public static void main(String args[]) { new NonStaticExample().demo(); } }
Output Result
Method of the abstract class