Wednesday, January 19, 2022

Quiz yourself: Java abstract classes and access modifiers for abstract methods

Core Java, Oracle Java Certification, Oracle Java Preparation, Oracle Java Exam Preparation, Oracle Java Career, Oracle Java Guides

It’s essential to declare classes properly to ensure methods are accessible.

Download a PDF of this article

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.

Source: oracle.com

Related Posts

0 comments:

Post a Comment