Download a PDF of this article
More quiz questions available here
Your software uses two classes that are part of the Object Relational Mapping (ORM) framework.
package orm.core;
public abstract class Connection {
abstract void connect(String url);
}
package orm.impl;
import orm.core.Connection;
public abstract class DBConnection extends Connection {
protected void connect(String url) { /* open connection */ }
}
You have decided to create your own concrete connection class based on the DBConnection
class.
package server;
import orm.impl.DBConnection;
public class ServerDBConnection extends DBConnection {
...
}
Which statement is correct? Choose one.
A. | The Connection class fails to compile. |
|
B. | The DBConnection class fails to compile. |
|
C. | The ServerDBConnection class cannot be properly implemented. |
|
D. | The ServerDBConnection class successfully compiles if you provide the following method inside the class body:public void connect(String url) { /* */ } |
Answer. This question investigates abstract
classes and access modifiers for abstract
methods.
Option A is incorrect because the Connection
class is properly declared: It declares an abstract
method, but that’s permitted since it is an abstract
class. However, notice that the connect()
method has a default accessibility, which means that it’s accessible only inside the orm.core
package. This has consequences for how it can be implemented.
As a side note, an abstract
method cannot have private
accessibility. A private
element of a parent class is essentially invisible from the source of a child type. Consequently a private abstract
method could never be implemented, so that combination is prohibited.
Consider option B. The DBConnection
class successfully compiles. Although it neither sees nor implements the Connection.connect()
method, that does not cause a problem. Why? Because the DBConnection
class is marked as abstract
, it’s acceptable for it to contain abstract
methods, whether from a superclass or declared in its own body. Because the class compiles, option B is incorrect.
Option D is also incorrect: Attempting to add a public connect()
method in the ServerDBConnection
class cannot provide an implementation for the abstract
method in the Connection
class because it’s not in the orm.core
package.
Unless the ServerDBConnection
class is in the package orm.core
, the ServerDBConnection
class cannot implement the Connection.connect()
method. Knowing this fact is at the heart of this question.
Because the code cannot implement all the abstract
methods from the ServerDBConnection
class’s parentage, it cannot be properly defined as a concrete class. This makes option C correct.
To fix the code, you can add the protected
access modifier before the Connection.connect()
method. The modifier will make DBConnection.connect()
implement the method properly, and the ServerDBConnection
class could even compile without providing an implementation of the connect()
method.
Alternatively, moving the ServerDBConnection
class into the orm.core
package would allow a proper implementation of the connect()
method in its current form.
Conclusion. The correct answer is option C.
Mikalai Zaikin is a lead Java developer at IBA Lithuania (part of worldwide IBA Group) and currently located in Vilnius. During his career, Zaikin has helped Oracle with development of Java certification exams, and he has been a technical reviewer of several Java certification books, including three editions of the famous Sun Certified Programmer for Java study guides by Kathy Sierra and Bert Bates.
Simon Roberts joined Sun Microsystems in time to teach Sun’s first Java classes in the UK. He created the Sun Certified Java Programmer and Sun Certified Java Developer exams. He wrote several Java certification guides and is currently a freelance educator who publishes recorded and live video training through Pearson InformIT (available direct and through the O’Reilly Safari Books Online service). He remains involved with Oracle’s Java certification projects.