Unit 3 · OOP Concepts

3.1.8Self-check and practice

Self-check questions

Write your answers first, then open the answers to check them.

  1. Explain the difference between a class and an object using your own example.
  2. What is the difference between an attribute and a method?
  3. What does the new keyword do?
  4. List three rules for constructors.
  5. Why does Java not have destructors like C++? What is used instead?
  6. Is int add(int a) and double add(int a) valid overloading? Why?
Show answers
  1. A class is a blueprint; an object is one actual thing built from it. Example: the class Phone describes a brand, a model and a battery level, and the methods call() and charge(). My phone and your phone are two objects of that class, each with its own values.

  2. An attribute stores data, the state of an object (for example marks). A method is code that defines behaviour (for example getGrade()); it can read and change the attributes.

  3. new allocates memory for a new object on the heap, runs the constructor to initialise it, and returns a reference to the object, which is usually stored in a variable.

  4. Any three:

    • The name is exactly the class name.
    • It has no return type, not even void.
    • It runs automatically when an object is created with new.
    • It can take parameters and can be overloaded.
    • If you write no constructor, Java supplies a default no-argument one; once you write any constructor, it does not.
  5. Java frees memory automatically with the garbage collector, so programmers never destroy objects themselves, and the moment of collection cannot be predicted. Resources such as files and connections are released with a close() method instead, usually called automatically by try-with-resources (AutoCloseable). The old finalize() method is deprecated.

  6. No. Overloaded methods must have different parameter lists. Both have (int a); only the return type differs, so the compiler reports that the method is already defined.

Practice exercises

  1. Create a Rectangle class with width and height, a constructor, and methods area() and perimeter(). Create three objects and print their results.
    Hint
    Java
    class Rectangle {
        double width, height;
        Rectangle(double w, double h) { width = w; height = h; }
        Rectangle(double side)        { this(side, side); }   // square (exercise 3)
        double area()      { return width * height; }
        double perimeter() { return 2 * (width + height); }
    }
  2. Create a Car class with model, speed, and methods accelerate(int amount) and brake(int amount) so that speed never becomes negative. Test with a trace table.
    Hint

    In brake(int amount), subtract the amount, then set speed = 0 if the result is below zero (or use Math.max(0, speed - amount)).

  3. Add an overloaded constructor to Rectangle that creates a square from one value.
  4. Write a class Printer with overloaded print() methods for an int, a String and a double.
  5. Create a Student copy constructor. Show with a test that changing the copy does not change the original.
  6. Write a lab report that lists three test cases for your Car class (normal, boundary, invalid) with expected and actual outputs.