Midterm 2 Review Guide

Static Methods and Variables

  • What is the purpose of a static variable in a class? How does it differ from a non-static instance variable?
  • Who "owns" a static variable in a class?
  • How do you declare a static variable as part of a class?
  • What is the purpose of a static method?
  • How do you call a static method from another class?

Inheritance
  • How do you inherit from another class?
  • What is the keyword that you must use to indicate inheritance from another class?
  • What gets inherited from the parent class?
  • How do you call a constructor from a parent class inside a constructor of a child class?
  • How do you call a constructor from a constructor within the same class (this class)?
  • How do you call other methods from the parent class?
  • Can you use private variables from the parent class in the child class?
  • What is the difference between public, private, package and protected access?
  • What is the difference between overloading and overriding? Define each one and give an example.
  • Which methods are inherited from the Object class?
  • How do you override methods from the Object class?
  • What is the proper approach to overriding the toString() method from the Object class?
  • What is the proper approach to overriding the equals() method from the Object class?
  • Why do all overridden equals methods have an Object type parameter? Why is another type of parameter not allowed?
  • What is a final class? What is a final method?

Abstract Classes, Polymorphism and Interfaces

  • What is the difference between an abstract class and a regular class?
  • How do you define an abstract class?
  • What is an abstract method and how is it different from a regular method?
  • Define polymorphism.
  • Given the below code, what will be displayed to the console?
abstract class Shape {
    private int x1, x2, y1, y2;

 // regular constructor and method declarations public Shape() { } public Shape(int newX1, int newY1, int newX2, int newY2) { x1 = newX1; x2 = newX2; y1 = newY1; y2 = newY2; } public int getX1() { return x1; } public int getX2() { return x2; } public int getY1() { return y1; } public int getY2() { return y2; } // Abstract method declaration public abstract void draw(); } class Line extends Shape { public void draw() { System.out.println("Drawing a Line."); } } class Rectangle extends Shape { public void draw() { System.out.println("Drawing a Rectangle."); } } class Square extends Rectangle { public void draw() { System.out.println("Drawing a Square."); } } class Oval extends Shape { public void draw() { System.out.println("Drawing an Oval."); } } public class DrawingApp { private Shape shapes[] = {new Line(), new Rectangle(), new Square(), new Oval()}; public static void main(String args[]) { DrawingApp app = new DrawingApp(); app.drawShapes(); }     public void drawShapes() {
    for (int i = 0; i < shapes.length; i++) {         shapes[i].draw();         }
}
}
  • What is dynamic binding and why is it necessary for polymorphism?
  • What is an interface?
  • How do you declare an interface?
  • What is unique about the methods and variables in an interface?
  • How is an interface different from an abstract class?
  • What are the pros and cons of using an interface compare to an abstract class (re-read relevant section of notes if uncertain)?
  • How do you inherit from an interface? What is the keyword that you must use?
  • Can one class inherit from both another class and an interface? Can one class inherit from multiple interfaces?
  • Can an interface inherit from another interface? What is the keyword that is used in that case?
  • What is the Comparable interface? How do you define the compareTo method for a class?
  • Given a class name Person, which stores a String name and an int age, how would you need to alter the signature of the person class to inherit from the Comparable interface?
  • What is the proper way to override compareTo for the Person class (considering name to be the field of primary significance and age to be the field of secondary significance)?
  • Write an interface called childFriendly, which has one method called isChildFriendly, which takes no parameters and returns a boolean
  • Write an abstract class named Dog that inherits from the above interface. In addition to what is inherited from the interface, define two private member variables: a boolean called childFriendly and a String called breed.
    • Write a 2-argument constructor for this method
    • Write a default constructor that calls the 2-argument constructor
    • Write an equals method
    • Write getters and setters for the breed variable
    • Write a toString for this class
  • Write a class called Chihuahua that inherits from Dog and also from the Comparable interface
    • This class has a private member variable - a String called name
    • Write a 3-argument constructor for this class
    • Write a default constructor that calls the 3-argument constructor
    • Write a toString method
    • Write an equals method
    • Write a compareTo method, first comparing by name, then by breed, then by childFriendly

Generics
  • How do you define a generic class/interface?
  • What is the purpose of making a class/interface generic? What benefit does it offer?
  • Why is it preferable to use generics rather than defining a class/interface to store data of type Object?
  • Given a generic class Box, how would you declare an instance of the Box class that can store data of type integer? String? double? Employee?
public class Box<E> {
  • What is a raw type?
  • What is type erasure?
  • Define a generic class named Box that stores data of type T.
    • the class has one private member variable named contents
    • It has a default and one argument constructor
    • It has a getter and setter method for the contents variable
    • It has a toString() method that returns the result of calling toString() on contents
  • Convert the following class into a generic class that stores data of type E:
public class Container
{
    private Candy contents1;
    private Candy contents2;

    public Candy() {
        contents1 = null;
        contents2 = null;
    }

    public Candy getContents1() {
        return contents1;
    }

    public Candy getContents2() {
        return contents2;
    }

    public void setContents1(Candy c) {
        contents1 = c;
    }

    public void setContents2(Candy c) {
        contents2 = c;
    }

    @Override public String toString() {
        return contents1.toString() + "\n" + contents2.toString();
    }
}