Java Questions 11 - 20  «Prev  Next»

Abstract Classes Access Modifiers

  1. Can a method be marked as both 1) abstract and 2) final?

    Answer:
    No. To have a method marked as both 1) abstract and 2) final is a contradiction.

  2. Why can't a method be marked both abstract and private?

    Answer:
    An abstract method is intended to be overriden and a private method cannot be overridden. A method can never be marked both abstract and private.

  3. What does the abstract modifier indicate about a method in Java SE 22?

    Answer:
    In Java SE 22, the `abstract` modifier applied to a **method** indicates the following:
    1. 🔹 Characteristics of an abstract method:
      • No implementation:
        • The method does not have a body; it ends with a semicolon (;), not a block of code.
        •             public abstract void processData(); // valid
                    
    2. Must be overridden:
      • Any non-abstract subclass must provide an implementation of the method, or the subclass itself must also be declared abstract.
    3. Only in abstract classes or interfaces:
      • An abstract method must be declared inside an abstract class or interface.
      •         public abstract class DataProcessor {
                  public abstract void processData();
                }
              
    4. Used for polymorphism and design abstraction:
      • It enforces a contract: all concrete subclasses must implement the behavior, allowing polymorphic design.

    🔸 Example:
    abstract class Animal {
        public abstract void makeSound();
    }
    
    class Dog extends Animal {
        @Override
        public void makeSound() {
            System.out.println("Woof");
        }
    }
    

    🔍 In interfaces (Java 8 and beyond):
    • All methods in interfaces are implicitly abstract unless they are:
      • default (have a default implementation)
      • static
      • or private
    So you don't need to explicitly use `abstract` in interfaces—though it's allowed.
    ✅ Summary:
    In Java SE 22, the `abstract` modifier on a method tells the compiler:
    • "This method has no body, and it must be implemented by a subclass."
    • It enforces incomplete behavior meant to be completed by concrete subclasses.
    An abstract designation means the superclass does not know aything about how the subclass will implement the method.



  4. Why are the abstract and final modifiers opposites of each other?

    Answer:
    An abstract class is intended to be extended and a final class cannot be extended.

  5. Why can't a private method be marked abstract?

    Answer:
    Private methods cannot be marked "abstract" because
    1. private methods cannot be overridden.
    2. private methods cannot be inherited because their visibility is only within the class that defined the private method.

  6. Why can't an abstract modifier be combined with the static modifier?

    Answer:
    The abstract modifier can never be combined with the static modifier because an abstract method is a member of an abstract class, and abstract classes are intended to be overridden.


  7. What does the synchronized keyword indicate when applied to a method?

    Answer:
    The sycnrhonized keyword indicates that a method can be accessed by only one thread at a time.


  8. Can the synchronized keyword be applied to variables?

    Answer:
    No. The synchronized modifier can be applied only to methods.The synchronized modifier can not be applied to variables or classes.

  9. Which access control level can be placed in front of the synchronized modifier?

    Answer:
    The synchronized modifier can be matched with any of the four access control levels 1) public 2) private 3) protected and 4) default.

  10. In which context can native methods be applied?

    Answer:
    Native is a modifier that can only be applied to methods.
    A method that is native is implemented in platform-dependent code, typically written in another programming language such as C. The body of a native method is given as a semicolon only, indicating that the implementation is omitted, instead of a block.