Explore BrainMass
Share

Explore BrainMass

    Inheritance, Polymorphism and Scope

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    Example 1: Which access modifier (public, private, protected, default) would you use on a data field in the following cases?
    a. You plan to inherit from this class from without the package.
    b. You want only those classes within the package to have access to this field.
    c. You want to restrict access to within the class itself.
    d. The client should have access to this field.

    Example 2: Examine the following driver for classes Book and LibBook and determine what is printed
    // Class TestBook is a driver for classes
    // Book and TestBook
    import java.util.Scanner;
    public class TestBook
    {
    public static void main(String[] args)
    {
    LibBook first;
    Book second;
    first =
    new LibBook("How to Solve It",
    "G. Polya",
    "0-691-02356-5",
    2, 511);
    second =
    new Book("Programming and Problem Solving with Java",
    "Nell Dale and Chip Weems",
    "10: 0-7637-3402-0");
    System.out.println("Book object: " + "n" + second);
    System.out.println("LibBook object: " + "n" + first);
    }
    }

    © BrainMass Inc. brainmass.com October 10, 2019, 1:26 am ad1c9bdddf
    https://brainmass.com/computer-science/java/inheritance-polymorphism-scope-334339

    Attachments

    Solution Preview

    Example #1
    a. protected
    b. package
    c. private
    d. public

    Example #2
    The output ...

    Solution Summary

    The expert examines inheritance, polymorphism and scope.

    $2.19