COMP2511 Tute02

Agenda

  • Admin Stuff
  • Code Review
  • Documentation
  • Basic Inheritance & Polymorphism (Code Demo)
  • Overriding toString() and equals()
  • Access Modifiers & Packages

Admin Stuff

  • Assignment 1 should be released (soon)
    • Due Week 5 Friday
    • 15% of the course mark 
    • Help sessions are super helpful :OO

 

Code Review

Inheritance

In Java, it is possible to inherit attributes and methods from one class to another.

  • subclass (child) - the class that inherits from another class
  • superclass (parent) - the class being inherited from

Shape

Rectangle

Circle

super class

sub classes

Inheritance

package shapes;

public class Shape {
    public String color;

    public Shape(String color) {
        System.out.println("Inside Shape constructor");
        this.color = color;
    }
}
public class Rectangle extends Shape {
    public int height;
    public int width;

    public Rectangle(String color) {
        super(color);
        System.out.println("Inside Rectangle constructor with one argument");
    }

    public Rectangle(String name, int width, int height) {
        this(name);
        this.width = width;
        this.height = height;
        System.out.println("Inside Rectangle constructor with three arguments");
    }

    public static void main(String[] args) {
        Rectangle r = new Rectangle("red", 10, 20);
        System.out.println(r.color);
        System.out.println(r.width);
        System.out.println(r.height);
    }
}

Shape.java

Rectangle.java

package shapes;

public class Shape {
    public String color;

    public Shape(String color) {
        System.out.println("Inside Shape constructor");
        this.color = color;
    }
}
public class Rectangle extends Shape {
    public int height;
    public int width;

    public Rectangle(String color) {
        super(color);
        System.out.println("Inside Rectangle constructor with one argument");
    }

    public Rectangle(String name, int width, int height) {
        this(name);
        this.width = width;
        this.height = height;
        System.out.println("Inside Rectangle constructor with three arguments");
    }

    public static void main(String[] args) {
        Rectangle r = new Rectangle("red", 10, 20);
        System.out.println(r.color);
        System.out.println(r.width);
        System.out.println(r.height);
    }
}
  • What is the difference between super and this?

Shape.java

Rectangle.java

super vs. this

package shapes;

public class Shape {
    public String color;

    public Shape(String color) {
        System.out.println("Inside Shape constructor");
        this.color = color;
    }
}
public class Rectangle extends Shape {
    public int height;
    public int width;

    public Rectangle(String color) {
        super(color);
        System.out.println("Inside Rectangle constructor with one argument");
    }

    public Rectangle(String color, int width, int height) {
        this(color);
        this.width = width;
        this.height = height;
        System.out.println("Inside Rectangle constructor with three arguments");
    }

    public static void main(String[] args) {
        Rectangle r = new Rectangle("red", 10, 20);
        System.out.println(r.color);
        System.out.println(r.width);
        System.out.println(r.height);
    }
}
  • What is the difference between super and this?
    • super refers to the superclass whereas this refers to the current class

Shape.java

Rectangle.java

super vs. this

package shapes;

public class Shape {
    public String color;

    public Shape(String color) {
        System.out.println("Inside Shape constructor");
        this.color = color;
    }
}
public class Rectangle extends Shape {
    public int height;
    public int width;

    public Rectangle(String color) {
        super(color);
        System.out.println("Inside Rectangle constructor with one argument");
    }

    public Rectangle(String color, int width, int height) {
        this(color);
        this.width = width;
        this.height = height;
        System.out.println("Inside Rectangle constructor with three arguments");
    }

    public static void main(String[] args) {
        Rectangle r = new Rectangle("red", 10, 20);
        System.out.println(r.color);
        System.out.println(r.width);
        System.out.println(r.height);
    }
}
  • What is the difference between super and this?
    • super refers to the superclass whereas this refers to the current class
  • What about super(...) and this(...)?

Shape.java

Rectangle.java

super vs. this

  • What is the difference between super and this?
    • super refers to the superclass whereas this refers to the current class
  • What about super(...) and this(...)?
    • super() calls the parent class constructor
    • this() calls the current class constructor

Shape.java

Rectangle.java

public class Rectangle extends Shape {
    public int height;
    public int width;

    public Rectangle(String color) {
        super(color);
        System.out.println("Inside Rectangle constructor with one argument");
    }

    public Rectangle(String color, int width, int height) {
        this(color);
        this.width = width;
        this.height = height;
        System.out.println("Inside Rectangle constructor with three arguments");
    }

    public static void main(String[] args) {
        Rectangle r = new Rectangle("red", 10, 20);
        System.out.println(r.color);
        System.out.println(r.width);
        System.out.println(r.height);
    }
}
package shapes;

public class Shape {
    public String color;

    public Shape(String color) {
        System.out.println("Inside Shape constructor");
        this.color = color;
    }
}

super vs. this

public class Shape {
    public String color;

    public Shape(String color) {
        System.out.println("Inside Shape constructor");
        this.color = color;
    }
}
public class Rectangle extends Shape {
    public int height;
    public int width;

    public Rectangle(String color) {
        super(color);
        System.out.println("Inside Rectangle constructor with one argument");
    }

    public Rectangle(String color, int width, int height) {
        this(color);
        this.width = width;
        this.height = height;
        System.out.println("Inside Rectangle constructor with three arguments");
    }

    public static void main(String[] args) {
        Rectangle r = new Rectangle("red", 10, 20);
    }
}

What will the main code in Rectangle print and why?

Shape.java

Rectangle.java

super vs. this

public class Shape {
    public String color;

    public Shape(String color) {
        System.out.println("Inside Shape constructor");
        this.color = color;
    }
}
public class Rectangle extends Shape {
    public int height;
    public int width;

    public Rectangle(String color) {
        super(color);
        System.out.println("Inside Rectangle constructor with one argument");
    }

    public Rectangle(String color, int width, int height) {
        this(color);
        this.width = width;
        this.height = height;
        System.out.println("Inside Rectangle constructor with three arguments");
    }

    public static void main(String[] args) {
        Rectangle r = new Rectangle("red", 10, 20);
    }
}

What will the main code in Rectangle print and why?

Shape.java

Rectangle.java

super vs. this

Inside Shape constructor


Inside Rectangle constructor with one argument


Inside Rectangle constructor with three arguments

public abstract class Shape {
    private String colour;

	// Only one variable for all Shape objects
    private static int count = 0;

    public Shape(String colour) {
        System.out.println("Inside Shape constructor");
        this.colour = colour;
        count++;
    }

    public void setColour(String colour) {
        this.colour = colour;
    }

    public String getColour() {
        return colour;
    }

    public static int getCount() {
        return count;
    }

    public abstract int getArea();

}
  • What are static fields and methods?

Code Review

public abstract class Shape {
    private String colour;

	// Only one variable for all Shape objects
    private static int count = 0;

    public Shape(String colour) {
        System.out.println("Inside Shape constructor");
        this.colour = colour;
        count++;
    }

    public void setColour(String colour) {
        this.colour = colour;
    }

    public String getColour() {
        return colour;
    }

    public static int getCount() {
        return count;
    }

    public abstract int getArea();

}
  • What are static fields and methods?
    • ​Static fields are variables that are common and available to all instances of a Class. They belong to the Class, rather than an instance.

Code Review

  • What are static fields and methods?
    • ​Static fields are variables that are common and available to all instances of a Class. They belong to the Class, rather than an instance.
    • For static methods, you do not need to create an instance in order to use the method

Code Review

public abstract class Shape {
    private String colour;

	// Only one variable for all Shape objects
    private static int count = 0;

    public Shape(String colour) {
        System.out.println("Inside Shape constructor");
        this.colour = colour;
        count++;
    }

    public void setColour(String colour) {
        this.colour = colour;
    }

    public String getColour() {
        return colour;
    }

    public static int getCount() {
        return count;
    }

    public abstract int getArea();

}

Code Review

Rectangle r2 = new Square("blue", 20);

What is happening here?

How can we assign an instance of Square to Rectangle?

  • r2 can store any object that is Rectangle or subclass of Rectangle (i.e. Square)

 

  • This lets us write code that works with general types (Rectangle) but still use specific subtypes (Square) behind the scenes

Code Review

Rectangle r2 = new Square("blue", 20);

At compile time, Java only knows r2 as Rectangle:

  • CAN only call methods defined in Rectangle (or superclass)
  • CANNOT call methods that exist in Square

At runtime:

  • r2 is actually pointing to a Square in memory
  • You call method of Rectangle but this is overridden at runtime using the Square implementation

This is called DYNAMIC DISPATCH

 

 

// Since in memory r2 is of type Square,
// this will run Square implementation of printMessage()
r2.printMessage();

JavaDoc (Documentation)

Documentation

  • What is meant by self-documenting code?

Documentation

  • What is meant by self-documenting code?
    • It is inherently easy to read through the use of meaningful variables, function names, clear logic.

Documentation

  • What is meant by self-documenting code?
    • It is inherently easy to read through the use of meaningful variables, function names, clear logic 
  • When can comments be bad (code smell)?

Documentation

  • What is meant by self-documenting code?
    • It is inherently easy to read through the use of meaningful variables, function names, clear logic 
  • When can comments be bad (code smell)?
    • Comments go stale/outdated
    • Comments are literally reiterating your code.

Documentation

  • Rule of thumb
    • Is the code understandable to someone who has never seen the code before, who is at the same level as you? 
    • Though there can be cases when there is very domain-specific code, and you have to understand the domain to understand the code. Comments can be helpful here. 

Documentation

  • JavaDoc is one way of documenting in Java.
  • JavaDoc is a way of writing your comments
  • It mainly targets class definitions and method/function definitions.
  • In COMP2511, you will not have to use JavaDoc documentation unless asked.
/**
 * File class that stores content under a file name
 */
public class File {
    /**
     * Constructor used to create a file
     * @param fileName the name of the file
     * @param content contents of the file
     */
    public File(String fileName, String content) {}
 
    /**
     * Constructor used to make a partial file when receiving a new file
     * I.e., content.length() != fileSize with no compression
     * @param fileName
     * @param fileSize
     */
    protected File(String fileName, int fileSize) {}
    
    /**
     * Checks if transfer has been completed
     * @return true if it has been completed
     */
    public boolean hasTransferBeenCompleted() {}
}

Documentation

VSCode Extensions

Code Demo

Employee

Code Demo

 

  1. Create a Manager Class that inherits Employee with a hireDate
  2. Override toString() method for both classes
  3. Override equals() method for both classes

Inheritance

Employee

Manager

Manager is-a Employee

But not all Employees are Managers

toString()

toString() returns a string that "textually represents" the object. This is useful for easily viewing data inside the class and for debugging. 

  • toString() is inherited from the Object class which means all classes can override toString()

Default implementation

Contains the class name and hash code for the instance

toString()

Overriden Implementation

Contains the class name and now the attributes of the class and its values

toString() returns a string that "textually represents" the object. This is useful for easily viewing data inside the class and for debugging. 

  • toString() is inherited from the Object class which means all classes can override toString()

equals()

Compares whether the contents of two instances are equal

  • To have the contents to be considered equal, they must be of the same class and all field values are equal
  • equals() is similar to strcmp() in C which comparing the contents of strings

equals() vs. ==

equals() ==
A method defined in Object class An operator
Compares if two objects are meaningfully equivalent (i.e. same class, same attributes) Compares references (memory addresses) and primitive data types (i.e. int, char, boolean, float, etc.)

equals()

What is the relationship between a super type and a sub type in terms of equality? Can a concrete instance of an Employee be equal to an instance of a Manager?

equals()

What is the relationship between a super type and a sub type in terms of equality? Can a concrete instance of an Manager be equal to an instance of a Employee?

Although a subclass can be treated as its super class, they are not equal. A subtype is more specific version and so cannot be treated as equal to its supertype.

 

Manager is more specific version than Employee with additional fields.

Abstract Classes and Interfaces

Abstract Classes

The abstract class is a class that cannot be instantiated and is meant to be subclassed.

It can have abstract methods (methods without a body) that must be implemented by subclasses, as well as concrete methods (methods with body).

Abstract Classes

// Abstract class Shape (acts as a blueprint)
abstract class Shape {
    String name;

    // Constructor
    public Shape(String name) {
        this.name = name;
    }

    // Abstract method (must be implemented by subclasses)
    abstract double area();

    // Concrete method (can be used by all shapes)
    public void display() {
        System.out.println("This is a " + name);
    }
}
class Circle extends Shape {
    double radius;

    public Circle(double radius) {
        super("Circle");
        this.radius = radius;
    }

    @Override
    double area() {
        return Math.PI * radius * radius;
    }
}

Abstract class

Subclasses

class Rectangle extends Shape {
    double width, height;

    public Rectangle(double width, double height) {
        super("Rectangle");
        this.width = width;
        this.height = height;
    }

    @Override
    double area() {
        return width * height;
    }
}

Abstract Classes

Think of an abstract class as a class that gives a basic structure but leaves some details for subclasses to fill in. 

For example...

You are designing a template of different animals. Every animal makes a sound, but the exact sound depends on the specific animal. Instead of defining a general sound, we create an abstract class that forces each specific animal to define its own sound. 

woof

meow

Interfaces

Interfaces define a set of methods that a class must implement, but do not provide the actual implementation of these methods.

Think of interfaces like a contract - if a class agrees to follow it, it must provide its own versions of methods specified in the interface. 

Interfaces

class Rectangle extends Shape implements Drawable {
    double width, height;

    public Rectangle(double width, double height) {
        super("Rectangle");
        this.width = width;
        this.height = height;
    }

    @Override
    double area() {
        return width * height;
    }

    @Override
    public void draw() {
        System.out.println("Drawing a Rectangle with width " 
        	+ width + " and height " + height);
    }
}
// Drawable interface
interface Drawable {
    void draw();
}

Differences between abstract classes and interfaces

Abstract Class Interface
Can store state (attributes) Cannot store state
Can have both concrete and abstract methods All methods are abstract by default
Classes can inherit from only one abstract class A class can implement multiple interfaces 
Mostly used for inheritance (subclass/superclass), think of nouns (e.g. Dog -> Labrador Mostly used for functionality/behaviours, think of verbs (Flyable, Drawable)

Access Modifiers

Access Modifiers

public class Circle {
    int a; // Default
    private int b; // Private
    protected int c; // Protected
    public int d; // Public
}

COMP2511 Tute02

By rebeccahsu

COMP2511 Tute02

  • 399