Java Reference
In-Depth Information
base class, it will automatically be inherited in the child. On the other hand, if you had
duplicate functionality scattered throughout your application, one minor change could
mean that you would have to change code in many places. Object inheritance also
makes it easy to designate a base class to one or more subclasses so that each class can
contain similar fields and functionality.
The Java language allows a class to extend only one other class. This differs in
concept from other languages such as C++; which contain multiple inheritance. Al-
though some look at single class inheritance as a hindrance to the language, it was de-
signed that way to add safety and ease of use to the language. When a subclass contains
multiple superclasses, confusion can ensue.
5-13. Defining a Template for Classes to
Extend
Problem
You would like to define a template that can be used to generate objects containing
similar functionality.
Solution
Define an abstract class that contains fields and functionality that can be used in
other classes. The abstract class can also include unimplemented methods, called
abstract methods , which will need to be implemented by a subclass of the abstract
class. The following example demonstrates the concept of an abstract class. The
abstract class in the example represents a team schedule, and it includes some basic
field declarations and functionality that every team's schedule will need to use. The
Schedule class is then extended by the TeamSchedule class, which will be used
to implement specific functionality for each team. First, let's take a look at the ab-
stract Schedule class:
public abstract class Schedule {
public String scheduleYear;
public String teamName;
Search WWH ::




Custom Search