Abstract Class in Java - GeeksforGeeks
Abstract Class in Java - GeeksforGeeks
Abstract Class in Java - GeeksforGeeks
Java Arrays Java Strings Java OOPs Java Collection Java 8 Tutorial Java Multithreading Java Exception H
In Java, the following some important observations about abstract classes are
as follows:
https://www.geeksforgeeks.org/abstract-classes-in-java/ 1/20
4/25/24, 6:25 PM Abstract Class in Java - GeeksforGeeks
6. We can use the abstract keyword for declaring top-level classes (Outer
class) as well as inner classes as abstract
7. If a class contains at least one abstract method then compulsory should
declare a class as abstract
8. If the Child class is unable to provide implementation to all abstract
methods of the Parent class then we should declare that Child class as
abstract so that the next level Child class should provide implementation to
the remaining abstract method
AD
Java
// Abstract class
abstract class Sunstar {
abstract void printInfo();
}
https://www.geeksforgeeks.org/abstract-classes-in-java/ 2/20
4/25/24, 6:25 PM Abstract Class in Java - GeeksforGeeks
System.out.println(name);
System.out.println(age);
System.out.println(salary);
}
}
// Base class
class Base {
public static void main(String args[])
{
Sunstar s = new Employee();
s.printInfo();
}
}
Output
avinash
21
222.2
data member
abstract method
method body (non-abstract method)
constructor
main() method.
Java
https://www.geeksforgeeks.org/abstract-classes-in-java/ 3/20
4/25/24, 6:25 PM Abstract Class in Java - GeeksforGeeks
void Learn(){
System.out.println("Preparing Right Now!");
}
}
class GFG {
public static void main(String[] args) {
Subject x=new IT();
x.syllabus();
x.Learn();
}
}
Output
Learning Subject
C , Java , C++
Preparing Right Now!
Observation 1
Example
Java
https://www.geeksforgeeks.org/abstract-classes-in-java/ 4/20
4/25/24, 6:25 PM Abstract Class in Java - GeeksforGeeks
// Class 1
// Abstract class
abstract class Base {
abstract void fun();
}
// Class 2
class Derived extends Base {
void fun()
{
System.out.println("Derived fun() called");
}
}
// Class 3
// Main class
class Main {
Output
Observation 2
Like C++, an abstract class can contain constructors in Java. And a constructor
of an abstract class is called when an instance of an inherited class is created.
It is as shown in the program below as follows:
Example:
https://www.geeksforgeeks.org/abstract-classes-in-java/ 5/20
4/25/24, 6:25 PM Abstract Class in Java - GeeksforGeeks
Java
// Class 1
// Abstract class
abstract class Base {
// Constructor of class 1
Base()
{
// Print statement
System.out.println("Base Constructor Called");
}
// Class 2
class Derived extends Base {
// Constructor of class2
Derived()
{
System.out.println("Derived Constructor Called");
}
// Method of class2
void fun()
{
System.out.println("Derived fun() called");
}
}
// Class 3
// Main class
class GFG {
https://www.geeksforgeeks.org/abstract-classes-in-java/ 6/20
4/25/24, 6:25 PM Abstract Class in Java - GeeksforGeeks
Output
Observation 3
In Java, we can have an abstract class without any abstract method. This
allows us to create classes that cannot be instantiated but can only be
inherited. It is as shown below as follows with help of a clean java program.
Example:
Java
// Class 1
// An abstract class without any abstract method
abstract class Base {
// Class 2
class Derived extends Base {
// This class only inherits the Base class methods and
// properties
}
// Class 3
class Main {
Output
Observation 4
Abstract classes can also have final methods (methods that cannot be
overridden)
Example:
Java
// Class 1
// Abstract class
abstract class Base {
// Class 2
class Derived extends Base {
// Class 3
// Main class
class GFG {
https://www.geeksforgeeks.org/abstract-classes-in-java/ 8/20
4/25/24, 6:25 PM Abstract Class in Java - GeeksforGeeks
{
{
// Creating object of abstract class
b.fun();
}
}
}
Output
Observation 5
For any abstract java class we are not allowed to create an object i.e., for an
abstract class instantiation is not possible.
Java
// Main class
// An abstract class
abstract class GFG {
Output:
https://www.geeksforgeeks.org/abstract-classes-in-java/ 9/20
4/25/24, 6:25 PM Abstract Class in Java - GeeksforGeeks
Observation 6
Similar to the interface we can define static methods in an abstract class that
can be called independently without an object.
Java
// Class 1
// Abstract class
abstract class Helper {
// Abstract method
static void demofun()
{
// Print statement
System.out.println("Geeks for Geeks");
}
}
// Class 2
// Main class extending Helper class
public class GFG extends Helper {
https://www.geeksforgeeks.org/abstract-classes-in-java/ 10/20
4/25/24, 6:25 PM Abstract Class in Java - GeeksforGeeks
Output
Observation 7
We can use the abstract keyword for declaring top-level classes (Outer class)
as well as inner classes as abstract
Java
import java.io.*;
abstract class B {
// declaring inner class as abstract with abstract
// method
abstract class C {
abstract void myAbstractMethod();
}
}
class D extends B {
class E extends C {
// implementing the abstract method
void myAbstractMethod()
{
System.out.println(
"Inside abstract method implementation");
}
}
}
https://www.geeksforgeeks.org/abstract-classes-in-java/ 11/20
4/25/24, 6:25 PM Abstract Class in Java - GeeksforGeeks
inner.myAbstractMethod();
}
}
Output
Observation 8
Java
import java.io.*;
https://www.geeksforgeeks.org/abstract-classes-in-java/ 12/20
4/25/24, 6:25 PM Abstract Class in Java - GeeksforGeeks
Output
Hello
Observation 9
Java
class GFG {
public static void main(String[] args)
{
// if we remove the abstract keyword from FirstChild
// Class and uncommented below obj creation for
// FirstChild then it will throw
// compile time error as did't override all the
// abstract methods
https://www.geeksforgeeks.org/abstract-classes-in-java/ 13/20
4/25/24, 6:25 PM Abstract Class in Java - GeeksforGeeks
Output
Inside m1
Inside m2
Inside m3
In C++, if a class has at least one pure virtual function, then the class becomes
abstract. Unlike C++, in Java, a separate keyword abstract is used to make a
class abstract.
Conclusion
Points to remember from this article are mentioned below:
An abstract class in Java is a class that can not be initiated on its own but
can be used as a subclass by another class.
https://www.geeksforgeeks.org/abstract-classes-in-java/ 14/20
4/25/24, 6:25 PM Abstract Class in Java - GeeksforGeeks
The main purpose of the abstract class is to create a base class from
which many other classes can be derived.
Also, Read
Difference between Abstract class and Interface in Java
Difference between Abstract class and Abstract Methods
Constructors in Java Abstract Class
Feeling lost in the vast world of Backend Development? It's time for a change!
Join our Java Backend Development - Live Course and embark on an exciting
journey to master backend development efficiently and on schedule.
What We Offer:
Comprehensive Course
Expert Guidance for Efficient Learning
Hands-on Experience with Real-world Projects
Proven Track Record with 100,000+ Successful Geeks
https://www.geeksforgeeks.org/abstract-classes-in-java/ 15/20
4/25/24, 6:25 PM Abstract Class in Java - GeeksforGeeks
Previous Next
Similar Reads
Difference Between Abstract Class and Difference between Abstract Class and
Abstract Method in Java Concrete Class in Java
Java | Abstract Class and Interface | Java | Abstract Class and Interface |
Question 1 Question 2
Difference between Abstract Class and Why can't static methods be abstract in
Interface in Java Java?
GeeksforGeeks
https://www.geeksforgeeks.org/abstract-classes-in-java/ 16/20
4/25/24, 6:25 PM Abstract Class in Java - GeeksforGeeks
Company Explore
About Us Hack-A-Thons
Legal GfG Weekly Contest
Careers DSA in JAVA/C++
In Media Master System Design
Contact Us Master CP
Advertise with us GeeksforGeeks Videos
GFG Corporate Solution Geeks Community
Placement Training Program
Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL Top 100 DSA Interview Problems
R Language DSA Roadmap by Sandeep Jain
Android Tutorial All Cheat Sheets
Tutorials Archive
https://www.geeksforgeeks.org/abstract-classes-in-java/ 17/20
4/25/24, 6:25 PM Abstract Class in Java - GeeksforGeeks
https://www.geeksforgeeks.org/abstract-classes-in-java/ 18/20
4/25/24, 6:25 PM Abstract Class in Java - GeeksforGeeks
https://www.geeksforgeeks.org/abstract-classes-in-java/ 19/20
4/25/24, 6:25 PM Abstract Class in Java - GeeksforGeeks
Linux CS Subjects
Excel
https://www.geeksforgeeks.org/abstract-classes-in-java/ 20/20