Abstract Class Example In Java

LEVEL: Intermediate

Whats in this post?

  1. Code
  2. Output
  3. Background and Explanation of the code
  4. Download Code

Code


To copy code: Click  View Source “View Source” on the right of this code snippet.

Background and Explanation of the code


An abstract class is a class that is declared abstract—it may or may not include abstract methods.

Abstract classes cannot be instantiated, but they can be subclassed.
An abstract method is a method that is declared without
an implementation (without braces, and followed by a semicolon)
When an abstract class is subclassed, the subclass usually provides implementations for all of the
abstract methods in its parent class. However, if it does not, the subclass must
also be declared abstract.
Creating an abstract class called “animals” with default
constructor and methods for onomatopoeia for the animal and it’s name.
Then, subclass for all the animals species we want to include are created and
we implement the already created methods in the abstract class. In the main class
we initialize array from the abstract class animals and cycle through the methods with a for loop.

An abstract class is a class that is declared abstract—it may or may not include abstract methods.  Abstract classes cannot be instantiated, but they can be subclassed.An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon) When an abstract class is subclassed, the subclass usually provides implementations for all of the  abstract methods in its parent class. However, if it does not, the subclass must  also be declared abstract.  Creating an abstract class called “animals” with default constructor and methods for onomatopoeia for the animal and it’s name.Then, subclass for all the animals species we want to include are created and we implement the already created methods in the abstract class. In the main classwe initialize array from the abstract class animals and cycle through the methods with a for loop.