COMP2511
25T1 Week 2
Thursday 9am-12pm (H09B)
Start 9:05am
By: Sam Zheng (z5418112)
Original Slides by: Alvin Cherk
Today
- Some VSCode tips
- Classes
- Commenting & Documentation
- Basic Inheritance
- Abstract Classes & Interfaces
- Access Modifiers
slido.com 7530 335
VSCode Config Tips
Ensure that you open the correct folder
example: cs2511-project


Good
Bad
Name of folder open
The folder I want to actually open
slido.com 7530 335
VSCode Config Tips
Ensure you have the correct Java extension installed

slido.com 7530 335
VSCode Config Tips
Ensure that you are running code using the "Run" button

Always use this to run code in this course
slido.com 7530 335
Inheritance
slido.com 7530 335
Inheritance
What is it?
In Java, a class can inherit attributes and methods from another class. The class that inherits the properties is known as the sub-class or the child class. The class from which the properties are inherited is known as the superclass or the parent class.
Known as a "is-a" relationship
slido.com 7530 335
Documentation
JavaDoc
slido.com 7530 335
Documentation
- Why is documentation important? When should you use it
- What does the term "self-documenting" code mean?
- Code that documents itself. It is readable inherently. Usually accomplished through variable name and function names
- When can comments be bad (code smell)?
- Comments become stale & does not get updated with new changes
- Possibly hinting that your design/code is too complex
slido.com 7530 335
Documentation
Single Line
// Single line commentMulti-line comment
/**
* This is multi-line
* documentation
*/JavaDoc Documentation
/**
* Constructor used to create a file
* @param fileName the name of the file
* @param content contents of the file
*/slido.com 7530 335
JavaDoc
- 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. Though, it is a good idea to do it anyway in assignments.
slido.com 7530 335
JavaDoc
/**
* 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() {}
}slido.com 7530 335
Code Demo
Employee.java & Manager.java
slido.com 7530 335
Code Demo
- Create a Employee class with a name and salary
- Create setters & getters with JavaDoc
- Create a Manager class that inherits Employee with a hireDate
- Override toString() method
- Write equals() method
slido.com 7530 335
Code Demo
How many constructors does a class need?
Technically none. If a class is defined without a constructor, Java adds a default constructor
However, if a class needs attributes to be assigned (e.g., has a salary), then a constructor must be assigned.
If your class has attributes with no default values, then the constructor must set these attributes. This is because variables with no values are dangerous (null), and is also the constructor responsibility.
Each class's constructor is also only responsible for setting its own attributes. Do not set the superclass's attributes within the subclasses without using a super(...) constructor call
slido.com 7530 335
Code Demo
How many constructors does a class need?
import java.time.LocalDate;
public class BadConstructor {
public String name;
public double salary;
public LocalDate hireDate;
public BadConstructor() {
this.hireDate = LocalDate.now();
// salary and hireDate aren't assigned a value
// Technically, they're defaulted to null
}
public static void main(String[] args) {
BadConstructor e = new BadConstructor();
System.out.println(e.name);
System.out.println(e.salary);
System.out.println(e.hireDate);
}
}slido.com 7530 335
Code Demo
How do you write a good equals method?
Since we are overriding an existing method (in the super most class called Object), we must follow the conditions described.
The conditions can be found in the Java Docs

The semantics of this was explored in a past exam
slido.com 7530 335
Code Review
package circle;
public class Circle extends Object {
// Every class extends Object, it is not needed though
private static final double pi = 3.14159;
private int x, y;
private int r;
// Only 1 variable for all Circle objects
static int no_circles = 0;
public Circle() {
super(); // not needed
no_circles++;
}
public double circumference() {
return 2 * pi * r;
}
}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.
Methods are a block of code that perform a task. You can think of them as functions of a class.
slido.com 7530 335
Interfaces
& Abstract Classes
Interfaces
- Allow you to define a 'template' that a class must follow
- For instance, an interface
Dogmight contain a method tobark() - All classes that implement
Dogmust have abark()method defined, but now you know that all Dog classes will have a bark method. - A class can implement as many interfaces as you like
public interface Dog {
public void bark();
}
public class Chihuahua implements Dog {
public void bark() {
System.out.println("arf arf");
}
}
public class Greyhound implements Dog {
public void bark() {
System.out.println("woof woof");
}
}Abstract Classes
- In a nutshell, an incomplete class
- Allows you to define common methods and attributes for all subclasses that inherit it
- Not all methods have to be defined (abstract methods)
- You cannot instantiate an abstract class by itself
- Unlike interfaces, classes can only inherit from one abstract class
public abstract class Dog {
private String colour;
public String getColour() {
return colour;
}
public abstract void bark();
}
public class Chihuahua extends Dog {
public Chihuahua(String colour) {
this.colour = colour;
}
@Override
public void bark() {
System.out.println("arf arf");
}
}
public class Greyhound extends Dog {
public GreyHound(String colour) {
this.colour = colour;
}
@Override
public void bark() {
System.out.println("woof woof");
}
}Access Modifiers
slido.com 7530 335
Access Modifiers

slido.com 7530 335
Private

It is accessible only to the same class (not including main). The most restrictive modifier.
slido.com 7530 335
Public

It is accessible to everything. The least restrictive modifier.
slido.com 7530 335
Protected

Can be accessed in the same package and in inheritance.
slido.com 7530 335
Default

The default access modifier is also called package-private, which means that all members are visible within the same package but aren't accessible from other packages
slido.com 7530 335
COMP2511 Week 2 25T1
By Sam Zheng
COMP2511 Week 2 25T1
- 126