Java Reference
In-Depth Information
The
computeArea
method in
InvalidShape
has the default access, a weaker access than
protected
. The following compiler error is generated:
InvalidShape.java:7: computeArea() in InvalidShape cannot override
computeArea() in Shape; attempting to assign weaker access privileges;
was protected
double computeArea() {
^
1 error
The
computeArea
method in
InvalidShape
can only have
public
or
protected
access.
Private and Abstract Methods
An abstract method cannot be declared
private
because a
private
method is not visible
in a child class and therefore cannot be overridden. For example, the following code does
not compile:
public abstract class Shape {
private abstract double computeArea();
}
The following compiler error is generated:
Shape.java:2: illegal combination of modifiers: abstract and private
private abstract double computeArea();
^
1 error
As you can see, the compiler states that
abstract
and
private
are not a valid
combination of modifi ers.
The abstract modifi er is only applied to classes and methods. Constructors cannot be
overridden, so it does not make sense for a constructor to be abstract. For example, the
following
Square
class does not compile:
public abstract class Square {
private int side;
public abstract Square(int s) {
side = s;
}
}
Search WWH ::
Custom Search