Saturday, September 18, 2021

Class Declaration and Modifiers

 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 most
method and variable declarations, a class can be declared with only public or
default access;

Class 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.

Default Access A class with default access has no modifier preceding it in the
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

package cert;
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

You can modify a class declaration using the keyword final, abstract, or
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.

You won't need to know how strictfp works, so we're focusing only on
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
method-by-method basis, by declaring a method as strictfp.

 Final Classes:

When used in a class declaration, the final keyword means the class can't be
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.

final gives you the security that nobody can change the implementation out from
under you.

You'll notice many classes in the Java core libraries are final. For example, the
String class cannot be subclassed

package cert;
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

Abstract Classes:

     An abstract class can never be instantiated. Its sole purpose, mission in life, raison d'être, is to be extended (subclassed). (Note, however, that you can compile and execute an abstract class, as long as you don't try to make an instance of it.) Why make a class if you can't make objects out of it? Because the class might be just too, well, abstract.

abstract class Car {
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
}

The preceding code will compile fine. However, if you try to instantiate a Car in
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

Notice that the methods marked abstract end in a semicolon rather than
curly braces.

We'll look at abstract methods in more detail later in this objective, but always
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:

he following exercise will test your knowledge of public, default, final, and
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:

import food.Fruit;
class Apple extends Fruit{ /* any code you want */}

3. Create a directory called food off the directory in your class path setting.

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

Variable Declarations

  There are two types of variables in Java: ■ Primitives A primitive can be one of eight types: char , boolean , byte , short , int , long...