OOP Syntax

Methods and Attributes

  • Methods and attributes can be assumed to be public unless otherwise stated.
  • Where the access level is relevant to the question it will always be explicit in the code denoted by the keywords.
private attempts = 3
 
public procedure setAttempts(number)
    attempts = number
endprocedure
 
public function getAttempts()
    return attempts
endfunction
  • Methods will always be instance methods. They will be called using object.method, so:
player.SetAttempts(5)
 
print(player.getAttempts())

Constructors and Inheritance

  • Inheritance is denoted by the inherits keyword, superclass methods will be called with the keyword super, i.e. super.methodname(parameters).
  • In the case of the constructor this would be super.new().
  • Constructors will be procedures with the name new.
class Pet
    
    private name
    public procedure new(givenName)
        name = givenName
    endprocedure
 
endclass
 
class Dog inherits Pet
 
    private breed
    public procedure new(givenName, givenBreed)
        super.new(givenName)
        breed = givenBreed
    endprocedure
 
endclass
  • To create an instance of an object the following format is used:
objectName = new className(parameters)
myDog = new Dog("Fido", "Scottish Terrier")

Java Syntax

Methods and Attributes

  • Methods and attributes can be assumed to be public unless otherwise stated.
  • Where the access level is relevant to the question it will always be explicit in the code denoted by the keywords.
private int attempts = 3;
 
public void setAttempts(int number) {
    attempts = number;
}
 
public int getAttempts() {
    return attempts;
}
  • Methods will always be instance methods. They will be called using object.method(), so:
player.setAttempts(5);
 
System.out.println(player.getAttempts());

Constructors and Inheritance

  • Inheritance is denoted by the extends keyword, superclass methods will be called with the keyword super, i.e. super.methodName(parameters).
  • In the case of the constructor this would be super(parameters).
  • Constructors will be methods with the same name as the class.
class Pet {
 
    private String name;
 
    public Pet(String givenName) {
        name = givenName;
    }
 
}
class Dog extends Pet {
 
    private String breed;
 
    public Dog(String givenName, String givenBreed) {
        super(givenName);
        breed = givenBreed;
    }
 
}
  • To create an instance of an object the following format is used:
objectName = new ClassName(parameters);
Dog myDog = new Dog("Fido", "Scottish Terrier");

Examples

class Dog
	private name
	private breed
	private height
	private weight
	
	public procedure new(givenName, givenBreed, givenHeight, givenWeight)
		name = givenName;
		breed = givenBreed;
		height = givenHeight;
		weight = givenWeight
	endprocedure	

endclass
class Treasure
	private value
	private level
	
	public procedure new(givenLevel, GivenValue)
		value = givenValue
		level = givenLevel
	endprocedure
	
	public function getLevel()
		return level
	endfunction
	
endclass
class video
	private name
	private views
	private stars
	
	public procedure new (givenName)
		name = givenName
		views = 0
		stars = 3
	endprocedure
	
	public procedure updateViews()
		views++
	endprocedure

endclass