Abstract Class in Java
Abstract Class in Java
In Java, abstract class is declared with the abstract keyword. It may have
both abstract and non-abstract methods(methods with bodies). An
abstract is a Java modifier applicable for classes and methods in Java
but not for Variables
What is Abstract Class in Java?
Java abstract class is a class that can not be instantiated by itself, it needs
to be subclassed by another class to use its properties. An abstract class
is declared using the “abstract” keyword in its class definition.
Illustration of Abstract class
abstract class Shape
{
int color;
// An abstract function
abstract void draw();
}
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
2. Abstract Class having constructor, data member, and methods
Elements abstract class can have
data member
abstract method
method body (non-abstract method)
constructor
main() method.
Below is the implementation of the above topic:
// Java Program to implement Abstract Class
// having constructor, data member, and methods
import java.io.*;
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!
Properties of Abstract class
Let us elaborate on these observations and do justify them with help of
clean java programs as follows.
Observation 1
In Java, just like in C++ an instance of an abstract class cannot be
created, we can have references to abstract class type though. It is as
shown below via the clean Java program.
Example
// Java Program to Illustrate
// that an instance of Abstract
// Class can not be created
// 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
Derived fun() called
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:
// Java Program to Illustrate Abstract Class
// Can contain Constructors
// 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 Main {
Output
Base Constructor Called
Derived Constructor Called
Derived fun() called
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 Program to illustrate Abstract class
// Without any abstract method
// 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
Function of Base class is called
Observation 4
Abstract classes can also have final methods (methods that cannot be
overridden)
Example:
// Java Program to Illustrate Abstract classes
// Can also have Final Methods
// Class 1
// Abstract class
abstract class Base {
// Class 2
class Derived extends Base {
// Class 3
// Main class
class GFG {
b.fun();
}
}
}
Output
Base fun() called
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 Program to Illustrate Abstract Class
// Main class
// An abstract class
abstract class GFG {
Observation 6
Similar to the interface we can define static methods in an abstract
class that can be called independently without an object.
// Java Program to Illustrate
// Static Methods in Abstract
// Class Can be called Independently
// Class 1
// Abstract class
abstract class Helper {
// Abstract method
static void demofun()
{
// Print statement
System.out.println("hello");
}
}
// Class 2
// Main class extending Helper class
public class GFG extends Helper {
Output
hello
Observation 7
We can use the abstract keyword for declaring top-level classes (Outer
class) as well as inner classes as abstract
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");
}
}
}
import java.io.*;
Output
Hello
Observation 9
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.
// Java Program to demonstrate
// Observation
import java.io.*;
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
Output
Inside m1
Inside m2
Inside m3