Java Questions 11 - 20  «Prev  Next»

Abstract, Final, Synchronized, and Native in Java SE 25

These ten questions examine legal modifier combinations and explain how abstraction, locking, accessibility, and native implementations differ.

  1. Can a method be both abstract and final?

    Answer: No. An abstract method leaves an implementation obligation for concrete subtypes, while final prevents overriding. Java rejects this combination at compile time.

    A concrete final method inside an abstract class is legal. Such a method can implement a fixed algorithm that calls overridable abstract operations. Distinguish modifiers on the enclosing class from modifiers on an individual method; they do not need to match.

  2. Why can a method not be both abstract and private?

    Answer: A private method is not inherited or overridden by subclasses, so it cannot impose an abstract implementation obligation on them. Java explicitly prohibits the combination.

    Use an appropriate accessible abstract method when subclasses must supply behavior. Use a concrete private method when the operation is an internal implementation detail. Giving a subclass method the same signature as a private superclass method creates a separate method rather than satisfying an override relationship.

  3. What does abstract indicate about a method in Java SE 25?

    Answer: The declaration specifies a contract but no method body. A concrete subtype must have a compatible implementation, which may be inherited. Parameters, return type, access, and checked exceptions still constrain overriding.

    public class AbstractContract {
        abstract static class Animal {
            abstract String sound();
            final String describe() { return "Sound: " + sound(); }
        }
        static final class Dog extends Animal {
            @Override String sound() { return "Woof"; }
        }
        public static void main(String[] args) {
            Animal animal = new Dog();
            System.out.println(animal.describe());
        }
    }

    This prints Sound: Woof. The abstract nested class is static, which is legal; an abstract method cannot be static. The final describe method is concrete and can call the subtype's implementation of sound.


  4. Why are abstract and final incompatible on a class?

    Answer: Abstract prevents direct instantiation and allows a subclass to provide a concrete realization. Final prevents subclassing. Java does not permit a class to declare both.

    An abstract class does not need to have an actual subclass merely to compile. It can also contain only concrete methods. A sealed abstract class is a different, valid design: it permits a controlled set of direct subclasses instead of forbidding extension altogether.

  5. Are private interface methods an exception to the abstract-private prohibition?

    Answer: No. Java permits private interface methods as concrete helpers, not as abstract obligations. They must have bodies and are not inherited by implementing classes. They can be instance helpers or static helpers.

    This is why the modern existence of private interface methods does not invalidate the rule that abstract and private cannot be combined. Choose a public abstract interface method for required implementer behavior; choose a private helper to share code among the interface's implemented methods.

  6. Why can a method not be both abstract and static?

    Answer: Abstract methods participate in instance overriding. Static methods belong to a type and can be hidden rather than overridden through instance dispatch. Java therefore disallows abstract static methods.

    Do not extend that rule to nested classes: a static nested class can be abstract, as the example above demonstrates. An abstract class can also declare ordinary static methods, including a classic public static main. The restriction concerns combining the modifiers on the same method declaration.



  7. What does synchronized mean on a method?

    Answer: An instance synchronized method acquires its receiver's monitor; a static synchronized method acquires the declaring Class object's monitor. Only one thread can own a particular monitor at a time, with reentrant acquisition allowed by the owner.

    Methods on different receivers may execute concurrently. Unsynchronized code is not automatically excluded, and class and instance monitors are distinct. Correct synchronization also requires a consistent policy for related reads and writes; releasing and subsequently acquiring the same monitor supplies a memory-ordering relationship.

  8. Can synchronized modify a variable or class?

    Answer: No. As a declaration modifier, synchronized applies to eligible methods, not variables or classes. It also appears in synchronized statements that protect a block using an object expression.

    A volatile field is not a “synchronized variable”: volatile has its own visibility and ordering rules and does not make compound updates such as increment atomic. Similarly, declaring a reference final fixes the reference rather than providing mutual exclusion. Select the mechanism according to the operation that must be protected.


  9. Which access levels can a synchronized class method have?

    Answer: Public, protected, private, or package access. Access and synchronization serve different purposes: one controls who may call the method, and the other controls monitor acquisition during execution. Package access uses no access keyword.

    The textual order of legal method modifiers is not what determines behavior, although conventional ordering improves readability. A synchronized method cannot also be abstract. An overriding method does not automatically inherit synchronization, so an extension design must consider whether the overriding implementation preserves the required locking contract.

  10. Where is native legal, and what does it mean?

    Answer: Native is a method modifier indicating an implementation supplied in native code rather than a Java method body, commonly through JNI. A native declaration ends with a semicolon. Native cannot modify a field, class, or constructor, and a native method cannot also be abstract.

    Declaration alone does not provide the implementation; missing or mismatched native linkage can cause UnsatisfiedLinkError. Native code also introduces platform and memory-safety responsibilities. Java SE 25 provides the Foreign Function and Memory API as another way to interoperate with native libraries, but using that API does not make a Java method declaration native.

References: Method-modifier rules, Interface methods, and Foreign Function and Memory API.


SEMrush Software