Java Questions 11 - 20  «Prev  Next»

Java Packages, Abstract Methods

  1. Is there a way to declare multiple classes in a file and have them in different packages.

    Answer:
    No.

  2. Can a file have nore than one non-public class?

    Answer:
    Yes.
    Only 1) public, 2) abstract, and 3) final are permitted for top-level Classes.

  3. What are examples of non-access modifiers?

    Answer:
    1. Strictfp
    2. final
    3. abstract

  4. What does one have to pay attention to regarding access control in Java?

    Answer:
    There are 4 levels of access [1) public, 2) private, 3) protected, 4) (package)] but only 3 access modifiers.

  5. Which two access modifiers are allowed for a top-level class?

    Answer:
    A class can be declared using only 1) public or 2) default access modifiers.

  6. If a class is declared public and you are using that class in a different package from the class you are writing, what must be done?

    Answer:
    You still need to import the public class.

  7. When you should you make a class final?

    Answer:
    If you need an absolute guarantee that none of the methods in that class will ever be overridden. When used in a class declaration, the final keyword means the class cannot be subclassed. In other words, no other class can ever extend (inherit from) a final class, and any attempts to do so will give you a compiler error. The methods in a final class cannot be overridden.

  8. What is the syntax for an abstract method in an abstract class?

    Answer:
    The method ends with a semicolon rather than a curly brace.
    abstract void draw();
    

  9. What must be changed on a method when it is changed from abstract to non-abstract?

    Answer:
    Change the semicolon at the end of the method declaration to a curly brace pair and remove the keyword abstract.

  10. Why are the abstract and final classes opposites of each other?

    Answer:
    An abstract class must be subclassed, whereas a final class cannot be subclassed.
    public abstract class Graphic Object{
    abstract void draw();
    }
    

    An abstract class can never be instantiated. Its sole purpose, mission in life is to be extended (subclassed).
    However, you can compile and execute an abstract class, as long as you do not try to make an instance of it.