Abstract Classes in Java

Abstract Classes in Java

The programming conventions in Java are quite different to that of the conventions of C++. If a class in C++ contains at least one pure virtual function, the class becomes an abstract class. However, in Java, for a class to be declared an abstract class, the ‘abstract’ keyword has to be used. Consider the following example of an abstract class:

abstract class Shape
{
int colour;
//the following is an abstract method that does not have any method body
abstract void draw ();
}

 

Similar to C++, abstract classes in Java cannot be instantiated, that is, objects of abstract classes cannot be made. Refer to the following code:

abstract class Base {
abstract void fun ();
}
class Derived extends Base
{
void fun ()
{
System.out.println (“Derived fun () called”);
}
}
class Main
{
public static void main (String args [])
{
//Base b = new Base()
//If the above line is uncommented, it will throw a compile time error as abstract classes cannot be instantiated.
Base b = new Derived ();
b.fun();
}
}

 

OUTPUT

Derived fun() called

Constructor in Abstract Classes in Java

In Java, abstract classes are allowed to have a constructor. The constructor of an abstract class is called when an instance of an inherited class is created. The program below illustrates the execution better:

abstract class Base
{
Base ()
{
System.out.println (“Base Constructor called”);
}
abstract void fun ();
}
class Derived extends Base
{
Derived ()
{
System.out.println (“Derived Constructor called”);
}
void fun ()
{
System.out.println (“Derived fun() called”);
}
}
class Main
{
public static void main (String args [])
{
Derived d = new Derived ();
}
}

 

OUTPUT

Base Constructor called
Derived Constructor called

Abstract methods in Abstract classes

It is also not mandatory for an abstract class in Java to have abstract methods. An abstract class can have no abstract methods. In such cases, the class can only be inherited and not instantiated.

abstract class Base
{
void fun ()
{
System.out.println (“Base fun () called”);
}
}
class Derived extends Base
{
}
class Main
{
public static void main (String args [])
{
Derived d = new Derived();
d.fun ();
}
}

 

OUTPUT

Base fun() called

Final methods in Abstract classes

Abstract classes can also contain final methods, that is, methods that cannot be overridden.

abstract class Base
{
final void fun ()
{
System.out.println (“Derived fun () called”);
}
}
class Derived extends Base
{
}
class Main
{
public static void main (String args [])
{
Base b = new Derived ();
b.fun ();
}
}

 

OUTPUT

Derived fun() called

Instances of Abstract class

As mentioned before, instances of an abstract class cannot be created.

abstract class Test
{
public static void main (String args [])
{
Test t = new Test ();
}
}

 

OUTPUT

Compile time error. Test is abstract;
cannot be instantiated Test t = new Test ();

Static methods inside Abstract Class

Static methods can also be defined inside abstract classes in the same manner in which static methods are defined in interfaces. These methods can be called independently without the requirement of an object.

abstract class Party
{
static void doParty ()
{
System.out.println (“Lets have some fun!”);
}
}
public class Main extends Party
{
public static void main (String args [])
{
Party.doParty();
}
}

 

OUTPUT

Lets have some fun!!

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.