Object Oriented Programming

Classes and Objects

Classes and Objects

Table of Contents

OOP overview

Classes

Objects

Object Oriented Features

Class definition

<visibility modifier> class <ClassName> {
    // attribute declarations
    <visibility modifier> <type> <variable name>; 
    // method declarations
    <visibility modifier> <typeReturned> myMethod(paramList) {
        variable declarations
        statements
    }
}

Class instantiation

Circle aCircle;
Circle bCircle;

Garbage collection

Using instance variables and methods

<objectName>.<variableName>
<objectName.<methodName>(<args>);

main method

Example: Drinking glass

Getters and Setters

// getter public double getCentreY() { return centreY; }


## Constructors

- **constructor**: method used to initialise objects
  - **cannot** return values
  - has same name as class
- **overloading**: class can have _1+ constructors_ with different sets of parameters

```java
public <Classname>(<arguments>) {
    <execute things>
}

e.g. Circle constructor:

public Circle(double newCentreX, double newCentreY, double newRadius) {
    centreX = newCentreX;
    centreY = newCentreY;
    radius = newRadius;
}

Method Overloading

this

Static Attributes and Methods

Static variables example

// Circle.java
public class Circle {
    public static int numCircles = 0;   // <- static (class) variable, one instance
                                        //    for Circle class
    public double centreX, centreY, radius;

   // constructor
   public Circle(double x, double y, double r) {
       centreX = x;
       centreY = y;
       radius = r;
       numCircles++;    // <- updating the static variable
   }
   
   // other methods here
}

Instance vs static variables

instance_vs_static_vars

Static Methods

e.g. static method to print the number of circles

// in Circle.java
public static void printNumCircles() {
    System.out.println("Number of circles = " + numCircles);
}

Standard Methods

equals

public boolean equals(<ClassName> var) {
    return <boolean expression>;
}

e.g. for Circle:

public boolean equals(Circle circle) {
    return Double.compare(circle.centreX, centreX) == 0 &&
           Double.compare(circle.centreY, centreY) == 0 &&
           Double.compare(circle.radius, radius) == 0;
}

toString

e.g. Circle class

public String toString() {
    return "I am a cirlce with {" + "centreX=" + centreX + 
           ", centreY=" + centreY +
           ", radius=" + radius + '}';
}

copy

public <ClassName>(<ClassName> var) {
    <code block>
}

Operator Overloading

Packages in Java

Package creation

public class Circle { // Code for Circle goes here }

- `Circle.class` must be in directory `shapes`, a sub-directory of `utilities`

### Using packages

- use packages with the `import` statement, which has different forms:
```java
import <packageName>.*; // import all classes in the package
import <packageName>.<className>; // import a particular class from the package

e.g.

import utilities.shapes.Circle;
public class CircleTest {
    public static void main(String args[]) {
        Circle my_circle = new Circle();
    }
}

default package (Non-assessable)

Information Hiding

encapsulation

Visibility modifiers

Keywords applied to class, method, or attribute:

Modifier Class Package Subclass Outside
public Y Y Y Y
protected Y Y Y N
default Y Y N N
private Y N N N

Circle class with visibility modifiers

public class Circle {
    private double centreX, centreY, radius;

    // methods to get/set instance variables
    public double getX() { return centreX; }
    public double getY() { return centreY; }
    public double getR() { return radius; }
    public double setX(double centreX) { this.centreX = centreX;}
    public double setY(double centreY) { this.centreY = centreY;}
    public double setR(double radius) { this.radius = radius;}
    // other methods

Mutability

e.g Circle class

// Circle.java
public class Circle {
    private double centreX, centreY, radius;
    private static int numCircles;

    public Circle(double newCentreX, double newCentreY, double newRadius) {...};
    public double getCentreX() {...};
    public void setCentreX(double centreX) {...};
    public double getCentreY() {...};
    public void setCentreY(double centreY) {...};
    public double getRadius() {...};
    public void setRadius(double radius) {...};
    public double computeCircumference() {...};
    public double computeArea() {...};
    public void resize(double factor) {...};
    public static int getNumCircles() {...};
}
// ImmutableCircle.java
public class ImmutableCircle {
    private final double centreX, centreY, radius;  // <- note: these are now final
    private static int numCircles;

    public ImmutableCircle(double newCentreX, double newCentreY, double newRadius) {...};
    // all setters have been removed
    public double getCentreX() {...};
    public double getCentreY() {...};
    public double getRadius() {...};
    public double computeCircumference() {...};
    public double computeArea() {...};
    public static int getNumCircles() {...};
}

Delegation through Association

e.g. Association relationship and Delegation through a Point class contained within Circle class. Is there a better way to store information about the centre of the Circle?

public class Circle {
    private Point centre;
    private double radius;

    public Circle(Point centre, double radius) {
        this.centre = centre;
        this.radius = radius;
    }

    public double getX() {          // <- interface has not changed
        return centre.getXCoord();
    }

    public double getY() {
        return centre.getYCoord();
    }
    //other methods here
}

Wrapper Classes

Integer class


Edit this page.