19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

564 Chapter 15 Abstract Classes and Interfaces<br />

abstract method in abstract<br />

class<br />

object cannot be created from<br />

abstract class<br />

abstract class without abstract<br />

method<br />

superclass of abstract class<br />

may be concrete<br />

concrete method overridden<br />

<strong>to</strong> be abstract<br />

abstract class as type<br />

✓Point✓ 15.2.2<br />

The following<br />

Interesting Points about Abstract Classes<br />

points about abstract classes are worth noting:<br />

objects[0] = new Circle();<br />

■ An abstract method cannot be contained in a nonabstract class. If a subclass of an<br />

abstract superclass does not implement all the abstract methods, the subclass must be<br />

defined as abstract. In other words, in a nonabstract subclass extended from an<br />

abstract class, all the abstract methods must be implemented. Also note that abstract<br />

methods are nonstatic.<br />

■ An abstract class cannot be instantiated using the new opera<strong>to</strong>r, but you can still<br />

define its construc<strong>to</strong>rs, which are invoked in the construc<strong>to</strong>rs of its subclasses. For<br />

instance, the construc<strong>to</strong>rs of GeometricObject are invoked in the Circle class<br />

and the Rectangle class.<br />

■ A class that contains abstract methods must be abstract. However, it is possible <strong>to</strong><br />

define an abstract class that doesn’t contain any abstract methods. In this case, you<br />

cannot create instances of the class using the new opera<strong>to</strong>r. This class is used as a<br />

base class for defining subclasses.<br />

■ A subclass can be abstract even if its superclass is concrete. For example, the Object<br />

class is concrete, but its subclasses, such as GeometricObject, may be abstract.<br />

■ A subclass can override a method from its superclass <strong>to</strong> define it as abstract. This is<br />

very unusual, but it is useful when the implementation of the method in the superclass<br />

be<strong>com</strong>es invalid in the subclass. In this case, the subclass must be defined as abstract.<br />

■ You cannot create an instance from an abstract class using the new opera<strong>to</strong>r, but an<br />

abstract class can be used as a data type. Therefore, the following statement, which<br />

creates an array whose elements are of the GeometricObject type, is correct.<br />

GeometricObject[] objects = new GeometricObject[10];<br />

You can then create an instance of GeometricObject and assign its reference <strong>to</strong><br />

the array like this:<br />

15.1 Which of the following classes defines a legal abstract class?<br />

class A {<br />

abstract void unfinished() {<br />

}<br />

}<br />

public class abstract A {<br />

abstract void unfinished();<br />

}<br />

(a)<br />

class A {<br />

abstract void unfinished();<br />

}<br />

(c)<br />

abstract class A {<br />

abstract void unfinished();<br />

}<br />

(b)<br />

abstract class A {<br />

protected void unfinished();<br />

}<br />

(d)<br />

abstract class A {<br />

abstract int unfinished();<br />

}<br />

(e)<br />

(f)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!