This code compiles just fine, but you can also add modifiers before the class declaration. In general, modifiers fall into two categories:
■ Access modifiers (public, protected, private)
■ Nonaccess modifiers (including strictfp, final, and abstract)
Access control in Java is a little tricky, because there are four access controls (levels of access) but only three access modifiers. The fourth access control level (called default or package access) is what you get when you don't use any of the three access modifiers. In other words, every class, method, and instance variable you declare has an access control, whether you explicitly type one or not.
Although all four access controls (which means all three modifiers) work for mostmethod and variable declarations, a class can be declared with only public or
default access;
What does it mean to access a class? When we say code from one class (class A) has access to another class (class B), it means class A can do one of three things:
■ Create an instance of class B.
■ Extend class B (in other words, become a subclass of class B).
■ Access certain methods and variables within class B, depending on the access
control of those methods and variables.
In effect, access means visibility. If class A can't see class B, the access level of the
methods and variables within class B won't matter; class A won't have any way to
access those methods and variables.
declaration! It's the access control you get when you don't type a modifier in the
class declaration. Think of default access as package-level access, because a class with default access can be seen only by classes within the same package
class Beverage { }
Now look at the second source file:
package exam.stuff;
import cert.Beverage;
class Tea extends Beverage { }
As you can see, the superclass (Beverage) is in a different package from the
subclass (Tea). The import statement at the top of the Tea file is trying (fingers
crossed) to import the Beverage class. The Beverage file compiles fine, but when
we try to compile the Tea file, we get something like this:
Public Access:
A class declaration with the public keyword gives all classes from all packages
access to the public class. In other words, all classes in the Java Universe (JU) have
access to a public class. Don't forget, though, that if a public class you're trying to use is in a different package from the class you're writing, you'll still need to import the public class.
n the example from the preceding section, we may not want to place the subclass
in the same package as the superclass. To make the code work, we need to add the
keyword public in front of the superclass (Beverage) declaration, as follows:
package cert;
public class Beverage { }
This changes the Beverage class so it will be visible to all classes in all packages.
The class can now be instantiated from all other classes, and any class is now free to
subclass (extend from) it—unless, that is, the class is also marked with the nonaccess
modifier final. Read on.
Other (Nonaccess) Class Modifiers
strictfp. These modifiers are in addition to whatever access control is on the class,
so you could, for example, declare a class as both public and final. But you can't
always mix nonaccess modifiers. You're free to use strictfp in combination with
final, for example, but you must never, ever, ever mark a class as both final and
abstract.
modifying a class as final or abstract. For the exam, you need to know only that
strictfp is a keyword and can be used to modify a class or a method, but never a
variable. Marking a class as strictfp means that any method code in the class will
conform to the IEEE 754 standard rules for floating points. Without that modifier,
floating points used in the methods might behave in a platform-dependent way. If
you don't declare a class as strictfp, you can still get strictfp behavior on a
Final Classes:
subclassed. In other words, no other class can ever extend (inherit from) a final
class, and any attempts to do so will result in a compiler error.
under you.
String class cannot be subclassed
public final class Beverage {
public void importantMethod() { }
}
Now let's try to compile the Tea subclass:
package exam.stuff;
import cert.Beverage;
class Tea extends Beverage { }
We get an error—something like this:
Can't subclass final classes: class
cert.Beverage class Tea extends Beverage{
1 error
private double price;
private String model;
private String year;
public abstract void goFast();
public abstract void goUpHill();
public abstract void impressNeighbors();
// Additional, important, and serious code goes here
}
another body of code, you'll get a compiler error something like this:
AnotherClass.java:7: class Car is an abstract
class. It can't be instantiated.
Car x = new Car();
1 error
curly braces.
remember that if even a single method is abstract, the whole class must be
declared abstract. One abstract method spoils the whole bunch. You can,
however, put nonabstract methods in an abstract class. For example, you might
have methods with implementations that shouldn't change from Car type to Car
type, such as getColor() or setPrice(). By putting nonabstract methods in an
abstract class, you give all concrete subclasses (concrete just means not abstract)
inherited method implementations. The good news there is that concrete subclasses
get to inherit functionality and need to implement only the methods that define
subclass-specific behavior.
Creating an Abstract Superclass and Concrete Subclass:
abstract classes. Create an abstract superclass named Fruit and a concrete
subclass named Apple. The superclass should belong to a package called food and
the subclass can belong to the default package (meaning it isn't put into a package
explicitly). Make the superclass public and give the subclass default access.
1. Create the superclass as follows:
package food;
public abstract class Fruit{ /* any code you want */}
2. Create the subclass in a separate file as follows:
class Apple extends Fruit{ /* any code you want */}
4. Attempt to compile the two files. If you want to use the Apple class, make
sure you place the Fruit.class file in the food subdirectory.
No comments:
Post a Comment