Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Abstract Class

Java Abstract Class


In object-oriented concepts, all objects are depicted through classes, but conversely, not all classes are used to depict objects. If a class does not contain enough information to depict a specific object, such a class is an abstract class.

Besides the inability to instantiate objects, an abstract class retains all other functionality; member variables, member methods, and constructors are accessed in the same way as in a normal class.

Because an abstract class cannot be instantiated, it must be inherited to be used. For this reason, whether to design an abstract class is usually decided during the design phase.

The parent class contains the common methods of the set of child classes, but because the parent class itself is abstract, these methods cannot be used directly.

In Java, an abstract class represents an inheritance relationship: a class can inherit from only one abstract class, but a class can implement multiple interfaces.


abstract classes

In the Java language, the abstract class keyword is used to define an abstract class, as shown in the following example:

Employee.java File Code:

/*

File name: Employee.java

*/

public

abstract

class

Employee

{

private

String

name

;

private

String

address

;

private

int

number

;

public

Employee

(

String

name

,

String

address

,

int

number

)

{

System

.

out

.

println

(

"

Constructing an Employee

"

)

;

this

.

name

=

name

;

this

.

address

=

address

;

this

.

number

=

number

;

}

public

double

computePay

(

)

{

System

.

out

.

println

(

"

Inside Employee computePay

"

)

;

return

0

.0

;

}

public

void

mailCheck

(

)

{

System

.

out

.

println

(

"

Mailing a check to

"

this

.

name

"

"

this

.

address

)

;

}

public

String

toString

(

)

{

return

name

"

"

address

"

"

number

;

}

public

String

getName

(

)

{

return

name

;

}

public

String

getAddress

(

)

{

return

address

;

}

public

void

setAddress

(

String

newAddress

)

{

address

=

newAddress

;

}

public

int

getNumber

(

)

{

return

number

;

}

}

Note that the Employee class is no different; although it is an abstract class, it still has 3 member variables, 7 member methods, and 1 constructor. Now if you try the following example:

AbstractDemo.java File Code:

/*

File name: AbstractDemo.java

*/

public

class

AbstractDemo

{

public

static

void

main

(

String

[

]

args

)

{

/*

The following is not allowed and will cause an error

*/

Employee

e

=

new

Employee

(

"

George W.

"

,

"

Houston, TX

"

,

43

)

;

System

.

out

.

println

(

"

\n

Call mailCheck using Employee reference--

"

)

;

e

.

mailCheck

(

)

;

}

}

When you try to compile the AbstractDemo class, the following error will be produced:


Employee.java:46: Employee is abstract; cannot be instantiated
      Employee e = new Employee("George W.", "Houston, TX", 43);
                   ^
1 error

Inheriting an abstract class

We can inherit the properties of the Employee class in the following way:

Salary.java File Code:

/*

File name: Salary.java

*/

public

class

Salary

extends

Employee

{

private

double

salary

;

//

Annual salary

public

Salary

(

String

name

,

String

address

,

int

number

,

double

salary

)

{

super

(

name

,

address

,

number

)

;

setSalary

(

salary

)

;

}

public

void

mailCheck

(

)

{

System

.

out

.

println

(

"

Within mailCheck of Salary class

"

)

;

System

.

out

.

println

(

"

Mailing check to

"

getName

(

)

"

with salary

"

salary

)

;

}

public

double

getSalary

(

)

{

return

salary

;

}

public

void

setSalary

(

double

newSalary

)

{

if

(

newSalary

=

0

.0

)

{

salary

=

newSalary

;

}

}

public

double

computePay

(

)

{

System

.

out

.

println

(

"

Computing salary pay for

"

getName

(

)

)

;

return

salary

/

52

;

}

}

Although we cannot instantiate an object of the Employee class, if we instantiate an object of the Salary class, that object will inherit 7 member methods from the Employee class, and through those methods we can set or get the three member variables.

AbstractDemo.java File Code:

/*

File name: AbstractDemo.java

*/

public

class

AbstractDemo

{

public

static

void

main

(

String

[

]

args

)

{

Salary

s

=

new

Salary

(

"

Mohd Mohtashim

"

,

"

Ambehta, UP

"

,

3

,

3600

.00

)

;

Employee

e

=

new

Salary

(

"

John Adams

"

,

"

Boston, MA

"

,

2

,

2400

.00

)

;

System

.

out

.

println

(

"

Call mailCheck using Salary reference --

"

)

;

s

.

mailCheck

(

)

;

System

.

out

.

println

(

"

\n

Call mailCheck using Employee reference--

"

)

;

e

.

mailCheck

(

)

;

}

}

The compilation and execution results of the above program are as follows:


Constructing an Employee
Constructing an Employee
Call mailCheck using  Salary reference --
Within mailCheck of Salary class
Mailing check to Mohd Mohtashim with salary 3600.0

Call mailCheck using Employee reference--
Within mailCheck of Salary class
Mailing check to John Adams with salary 2400.

Abstract method

If you want to design a class that contains a special member method whose concrete implementation is determined by its subclasses, you can declare that method as an abstract method in the parent class.

The abstract keyword can also be used to declare abstract methods; an abstract method contains only a method name and no method body.

An abstract method has no definition; a semicolon follows the method name directly, rather than curly braces.

public

abstract

class

Employee

{

private

String

name

;

private

String

address

;

private

int

number

;

public

abstract

double

computePay

(

)

;

//

The remaining code

}

Declaring an abstract method results in the following two consequences:

A subclass that inherits an abstract method must override it; otherwise, that subclass must also be declared as an abstract class. Ultimately, some subclass must implement the abstract method, otherwise neither the original parent class nor the final subclass can be used to instantiate objects.

If the Salary class inherits from the Employee class, then it must implement the computePay() method:

Salary.java File Code:

/*

File name: Salary.java

*/

public

class

Salary

extends

Employee

{

private

double

salary

;

//

Annual salary

public

double

computePay

(

)

{

System

.

out

.

println

(

"

Computing salary pay for

"

getName

(

)

)

;

return

salary

/

52

;

}

//

The remaining code

}


Summary rules of abstract classes