Preview

09 - object Superclass

 1. The Object class is the _________ of all other classes in Java and a part of the built-in java.lang package.

  subclass

  inherited subclass

  superclass

  default object

 2. If a_______________ using the extends keyword, the class will inherit from the Object class.

  method is not created as a constructor

  parent class is specified

  child class isn't specified

   parent class isn?t specified

 3. In the following code, the Person class overrides the ____________ and the Student class overrides the Person toString() method.
public class Person
  {
     private String name;
     public Person(String name)
     {
        this.name = name;
     }
     public String toString()
     {
        return name;
     }
     public static void main(String[] args)
     {
        Person p = new Person("Stephen");
        Student s = new Student("Thomas", 1001);
        System.out.println(p); //call Person toString
        System.out.println(s);  //call Student toString
     }
  }

  class Student extends Person
  {
     private int id;
     public Student(String name, int id)
     {
        super(name);
        this.id = id;
     }
     public String toString()
     {
        return super.toString() + " " + id;
     }
  }

  name variables

  Person class constructor method

  main method

  Object toString() method

 4. The output from the above code would be:

  Run-time error

  Stephen

  Stephen, Thomas 1001

  Compile-time error

 5. The output from the code below would be: true, true, true, false.
public class Person
{
   private String name;

   public Person(String theName)
   {
      this.name = theName;
   }

   public static void main(String[] args)
   {
      Person p1 = new Person("Karen");
      Person p2 = new Person("Josephus");
      Person p3 = new Person("Kyle");
      Person p4 = p3;
      System.out.println(p1.equals(p2));
      System.out.println(p2.equals(p3));
      System.out.println(p1.equals(p3));
      System.out.println(p3.equals(p4));

   }
}

  FALSE

  TRUE

 6. The equals method inherited from the Object class only returns ______ when the two references point to the same object as shown in the code above.

  FALSE

  TRUE

 7. If you want to change how the inherited equals method works you can ______so that the new method is called instead of the inherited one.

  overload it

  override it

  duplicate it

  delete it

 8. What is the output of the following code?
public class StringTest
{
   public static void main(String[] args)
   {
      String s1 = "Test";
      String s2 = "Track";
      String s3 = new String("Test");
      System.out.println(s1.equals(s2));
      System.out.println(s2.equals(s3));
      System.out.println(s1.equals(s3));
   }
}

  true,true,false

  false,true,true

  false,false,false

  false,false,true

 9. Any class can override the inherited equals method by providing a method with _________________.

  the same variables and no return type

  the same method signature and no return type

  a different method signature (method name and parameter list must differ)

  the same method signature (method name and parameter list) and return type

 10. Read the following excerpt and fill in the blanks.
The following Object class methods and constructors,
including what they do and when they are used, are 
part of the Java Quick Reference:

>String toString()

>boolean equals(Object other)

Subclasses of Object often override the equals 
and toString methods with ________________________.

  non-class specific duplications

  methods that exhibit polymorphism

  methods that override the superclass constructor

  class-specific implementations.