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:
    In Java, each class that is public should be declared in its own file with the filename matching the class name. Moreover, the package structure is typically mirrored by the filesystem, so classes declared in different packages should reside in different directories. However, you can declare multiple classes within a single file under the following conditions:
    1. Non-Public Classes: You can have one public class and multiple non-public classes in the same file. The non-public classes won’t be accessible from classes in other packages.
    2. Inner Classes: You can declare inner classes within a public class, and these can be public, protected, package-private, or private.
    3. Local Classes: You can declare classes within a block scope, such as within a method. These are not accessible outside the block.
    4. Anonymous Classes: You can declare and instantiate anonymous classes within any block of code.
    But there is no way to declare multiple classes in a single file and have them reside in different packages. Each package is associated with a directory structure. So, if you want classes to be in different packages, they must be in separate files and directories corresponding to their package names.
    For example:
    // File located in src/myapp/util/UtilityClass.java
    package myapp.util;
    
    public class UtilityClass {
        // ...
    }
    
    // This non-public class can be in the same file as UtilityClass
    class HelperClass {
        // ...
    }
    
    And another class in a different package would be in a different file:
    // File located in src/myapp/models/DataModel.java
    package myapp.models;
    
    public class DataModel {
        // ...
    }
    

    Each public class should be in a file named after the class and in a directory structure that matches its package name. The `src/myapp/util/` and `src/myapp/models/` paths correspond to the `myapp.util` and `myapp.models` packages, respectively. Non-public classes can be in any of these files but are only accessible within the same package.

  2. Can a file have more 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.

SEMrush Software