by
PRO
FE S
S OR

SECTION XII - Interfaces and Abstract Classes
Step 49: An Abstract class
ABSTRACTION
Before we discuss what an abstract class is, it would be convenient to click
on ABSTRACTION and have some general understanding of the benefits of it.
First, let's disccuss what is an abstract method.
An Abstract Method
An abstract method is a method that does not define code. It's a prototype used as a placeholder. Since it does not have code
its purpose is for interface only. Even though it does not contain code, it portrays the arguments within the set of
parentheses and the return type. By providing name, arguments and return type, the method conveys what it does. But it
does not convey how does it do it.
An Abstract Class
An abstract class is a class that contains at least one abstract method.
A class can contain many methods, but if only one method out of many is abstract, the class becomes abstract.
An Example of a abstract method defined within an abstract class
{
void concreteProcess() // this is a regular non-abstract method
{
Notice that there is no code defined in the abstract method, and it ends in semi-colon.
A class can also contains all of its methods abstract.. Such a class with all abstract methods is used only for interface.
(An Interface as discussed in next Step is a total abstract class.
An abstract class can not be instantiated. That is, no Object can be created directly from it. However, an
abstract class can have sub-classes that are not abstract.
You may wonder what's the idea of having an abstract method.
When there is an abstract method, it means that
there ought to be a method with the same name, and same signature (arguments and return type) in a derived class. That method in
that derived class is concrete (not abstract) and contains the code for its implementation.
By virtue of Inheritance (discussed in Section X), if a class that derives (inherits) from an abstract super-class
is instantiated, all the concrete methods of the super-class can be implemented (executed) via this child class.
An abstract method in an abstract super-class is used as the prototype, but the method of the child class (if concrete)
is the one that is implemented. (sort of mandatory Overriding). This counterpart method in the child class is defined
as non abstract method (concrete), with the same signature, name and return type of the abstact method.
If it's not defined as a concrete method in the child class, it has to be defined as concrete method in
the grand-child class or in any other lower level hierarchy class.
In a hierarchy (Inheritance) scheme, if the method declared abstract in the super class, is not defined as concrete
in any of the lower level classes (child, grandchild, great-grandchild, etc),
a compiler error would occur. Now, it is possible that the method is also made abstract in the child class.
If that's the case, then it still needs to be defined as concrete method in the grand-child class or
any lower level hiearchy class.
This may seem a little bit confusing, so we will illustrate various scenarios with Examples.
Example 1
(In this Example the process' method is declared abstract in the super class,
and its counterpart method IS NOT defined in the child class.)
*/
abstract class Document
{
abstract void process(); // an abstract method
void setNumber(int tmpNumber) // a concrete method
{
class Notebook extends Document // this is the child class.
{
{
Example 2
(In this Example the compilation error of Example 1 above is corrected, by defining the 'process' method
in the childe class.)
*/
abstract class Document
{
public static void main(String[] args)
{
agenda.setNumber(5); // this class method is invoked here via the child Object.
agenda.process(); // method as implemented in the child class is invoked here.
abstract void process(); // an abstract method
void setNumber(int tmpNumber) // a concrete method
{
class Notebook extends Document // this is the child class.
{
{
void scribble() // a concrete method
{
Example 3
(In this Example the 'process' method is declared as abstract
in the super class, non-existing in the child class, but defined concretely in the grandchild class.)
*/
abstract class Document
{
public static void main(String[] args)
{
agenda.setNumber(5); // this class method is invoked here via the child Object.
Magazine guide = new Magazine(); // ' guide' is a grandchild class Object.
guide.process(); // method as implemented in the grandchild class is invoked here.
abstract void process(); // an abstract method
void setNumber(int tmpNumber) // a concrete method
{
class Notebook extends Document // this is the child class.
{
void scribble() // a concrete method
{
class Magazine extends Notebook // this is the grandchild class.
{
{
Example 4
(In this Example the 'process' method is declared as
abstract in the super class,
declared also as abstract in the child class, but defined concretely in the grandchild class.)
*/
(As there is an abstract method in this child class, it's also abstract, and can't be instantiated.)
abstract class Document
{
public static void main(String[] args)
{
guide.setNumber(5); // this class method is invoked here via the grandchild Object.
guide.process(); // method as implemented in the grandchild class is invoked here.
abstract void process(); // an abstract method
void setNumber(int tmpNumber) // a concrete method
{
abstract class Notebook extends Document // this is the child class.
{
abstract void process(); // abstract method
void scribble() // a concrete method
{
class Magazine extends Notebook // this is the grandchild class.
{
{
by
PRO
FE S
S OR

SECTION XII - Interfaces and Abstract Classes
Step 50: Interfaces
INTERFACES
by
PRO
FE S
S OR

SECTION XII - Interfaces and Abstract Classes
Step 51: Implementing Multiple Interfaces
Implementing Mutliple Interfaces
by
PRO
FE S
S OR

SECTION XII - Interfaces and Abstract Classes
Step 52: Why INTERFACES?
Purpose of an interface