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.
// An example abstract class in Java
abstract class Shape {
int color;
// An abstract function (like a pure virtual function in
// C++)
abstract void draw();
}
Following are some important observations about abstract classes in Java.
1) Like C++, in Java, an instance of an abstract class cannot be created, we can have references to abstract class type though.
abstract class Parent {
abstract void fun();
}
class Child extends Parent {
void fun()
{
System.out.println("Derived fun() called");
}
}
class Main {
public static void main(String args[])
{
// Uncommenting the following line will cause
// compiler error as the line tries to create an
// instance of abstract class. Base b = new Parent();
// We can have references of Base type.
Base b = new Child();
b.fun();
}
}
Output
Derived fun() called
2) Like C++, an abstract class can contain constructors in Java. And a constructor of abstract class is called when an instance of an inherited class is created. For example, the following is a valid Java program.
// An abstract class with constructor
abstract class Parent {
Base()
{
System.out.println("Base Constructor Calling");
}
abstract void fun();
}
class Child extends Parent {
Derived()
{
System.out.println("Derived Constructor Calling");
}
void fun()
{
System.out.println("Derived fun() called");
}
}
class Main {
public static void main(String args[])
{
Derived d = new Child();
}
}
Output
Base Constructor Calling
Derived Constructor Calling
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.
// An abstract class without any abstract method
abstract class Parent {
void fun() { System.out.println("Base fun() called"); }
}
class Child extends Parent {
}
class Main {
public static void main(String args[])
{
Derived d = new Child();
d.fun();
}
}
Output
Base fun() called
4) Abstract classes can also have final methods (methods that cannot be overridden). For example, the following program compiles and runs fine.
// An abstract class with a final method
abstract class Parent {
final void fun()
{
System.out.println("Derived fun() called");
}
}
class Child extends Parent {
}
class Main {
public static void main(String args[])
{
Base b = new Child();
b.fun();
}
}
Output
Derived fun() called
5) For any abstract java class we are not allowed to create an object i.e., for abstract class instantiation is not possible.
// An abstract class example
abstract class Test {
public static void main(String args[])
{
// Try to create an object
Test t = new Test();
}
}
Output:
Compile time error. Test is abstract;
cannot be instantiated Test t=new Test();
6) Similar to the interface we can define static methods in an abstract class that can be called independently without an object.
abstract class Party {
static void celebration()
{
System.out.println("Lets do some party!!");
}
}
public class Main extends Party {
public static void main(String[] args)
{
Party.celebration();
}
}
Output
Lets do some party!!