- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are abstract classes in Java?
A class which contains the abstract keyword in its declaration is known as abstract class.
- Abstract classes may or may not contain abstract methods, i.e., methods without body ( public void get(); )
- But, if a class has at least one abstract method, then the class must be declared abstract.
- If a class is declared abstract, it cannot be instantiated.
- To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it.
- If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.
Declaring an abstract class:
To declare an abstract class just use the abstract keyword before it.
abstract class AbstractExample { public abstract void sample(); public abstract void demo(); }
Since you cannot instantiate an abstract class to use its methods extend the super class and override the implementations of those methods and use them.
Example
abstract class SuperTest { public abstract void sample(); public abstract void demo(); } public class Example extends SuperTest{ public void sample(){ System.out.println("sample method of the Example class"); } public void demo(){ System.out.println("demo method of the Example class"); } public static void main(String args[]){ Example obj = new Example(); obj.sample(); obj.demo(); } }
Output
sample method of the Example class demo method of the Example class
- Related Articles
- What are abstract classes in C#?
- Abstract Classes in Java
- Abstract Method and Classes in Java
- What is the difference between interfaces and abstract classes in Java?
- Abstract Classes in C#
- what are abstract methods in Java?
- Abstract Classes in Dart Programming
- What are Java classes?
- Abstract Base Classes in Python (abc)
- What are final classes in Java?
- What are inner classes in Java?
- What are wrapper classes in Java?
- How to create abstract classes in TypeScript?
- Python Abstract Base Classes for Containers
- What are anonymous inner classes in Java?

Advertisements