Java Reference
In-Depth Information
TOPIC
CONCEPT
Abstract Meth-
ods
An abstract method is a method that has no body defined for it and is declared using the keyword ab-
stract.
Abstract
Classes
An abstract class is a class that contains one or more abstract methods. It must be defined with the at-
tribute abstract .
Derived
Classes
You can define one class based on another. This is called class derivation or inheritance. The base class
is called a superclass, and the derived class is called a subclass. A superclass can also be a subclass of
another superclass.
The Universal
Superclass
Every class has the Object class as a base so every class inherits members from the Object class. The
toString() method is inherited from the Object class.
Abstract De-
rived Classes
A subclass of an abstract class must also be declared as abstract if it does not provide definitions for
all of the abstract methods inherited from its superclass.
Class Inherit-
ance
A subclass inherits certain members of its superclass. An inherited member of a class can be referen-
ced and used as though it were declared as a normal member of the class.
Constructors
under Inherit-
ance
A subclass does not inherit the superclass constructors.
Private and
Package-Priv-
ate Class Mem-
bers
The private members of a superclass are not inherited in a subclass. If the subclass is not in the same
package as the superclass, then members of the superclass that do not have an access attribute are not
inherited.
Implementing
Derived Class
Constructors
The first statement in the body of a constructor for a subclass should call a constructor for the super-
class. If it does not, the compiler inserts a call for the default constructor for the superclass.
Polymorphism A subclass can override a method inherited from its superclass by re-implementing it in the subclass. If
two or more subclasses, with a common base class, override a common set of methods, these methods
can be selected for execution dynamically at run time. This behavior is termed polymorphism .
Polymorphic
Behavior
The subclass method selected by a polymorphic call is determined by the type of the object that is used
to call the method.
Using Vari-
ables of a Su-
perclass Type
A variable of a superclass type can point to an object of any of its subclasses. Such a variable can then
be used to execute the subclass methods inherited from the superclass polymorphically.
You should use the @Override annotation for derived class methods that can be called polymorphic-
ally to guard against errors arising from an incorrect method signature in the derived class.
@Override
Static Import
You can import the static members of a class that is defined in a named package into a class to allow
the imported static members to be referenced by their unqualified names.
Enumeration
Class Types
An enumeration type is a specialized form of class, and the enumeration constants that you define are
instances of the enumeration class type.
Nested Classes A class defined inside another class is called a nested class or inner class. An inner class may itself
contain inner classes.
Interfaces
An interface can contain constants, abstract methods, and inner classes.
Implementing
Interfaces in a
Class
A class can implement one or more interfaces by declaring them in the class definition and including
the code to implement each of the interface methods.
Partial Inter-
face Imple-
mentation
A class that does not define all the methods for an interface it implements must be declared as ab-
stract .
Interface Types
and Poly-
morphism
If several classes implement a common interface, the methods declared as members of the interface
can be executed polymorphically by using a variable of the interface type to store references to objects
of the class types.
Search WWH ::




Custom Search