COMP 213
Advanced Object-oriented Programming Lecture 5 Modifiers: final and static.
COMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: - - PowerPoint PPT Presentation
COMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static. Access & non-access modifiers Access modifiers help you set the level of access you want for your classes, your variables and your methods, e.g.,
Advanced Object-oriented Programming Lecture 5 Modifiers: final and static.
variables and your methods, e.g., private or public.
functionalities:
variables and your methods, e.g., private or public.
functionalities:
variables and your methods, e.g., private or public.
functionalities:
variables and your methods, e.g., private or public.
functionalities:
variables and your methods, e.g., private or public.
functionalities:
variables and your methods, e.g., private or public.
functionalities:
instances created for the class. Only one copy of the static variable exists regardless
in a class, it only has scope "within" the method, i.e., it is local.
instances created for the class. Only one copy of the static variable exists regardless
in a class, it only has scope "within" the method, i.e., it is local.
instances created for the class.
defined in.
parameters, with no reference to variables.
the name of the variable or method.
instances created for the class.
defined in.
parameters, with no reference to variables.
the name of the variable or method.
instances created for the class.
defined in.
parameters, with no reference to variables.
the name of the variable or method.
instances created for the class.
defined in.
parameters, with no reference to variables.
the name of the variable or method.
public class CounterOfInstances { // Stores the total number of instances created. private static int numberOfInstances = 0; // Returns the total number of instances created. static int getCounter() { return numberOfInstances; } // Increases the total number of instances created. private static void addInstance() { numberOfInstances++; } // The default constructor CounterOfInstances() { CounterOfInstances.addInstance(); } }
public class CounterOfInstances { // Stores the total number of instances created. private static int numberOfInstances = 0; // Returns the total number of instances created. static int getCounter() { return numberOfInstances; } // Increases the total number of instances created. private static void addInstance() { numberOfInstances++; } // The default constructor CounterOfInstances() { CounterOfInstances.addInstance(); } }
public class CounterOfInstances { // Stores the total number of instances created. private static int numberOfInstances = 0; // Returns the total number of instances created. static int getCounter() { return numberOfInstances; } // Increases the total number of instances created. private static void addInstance() { numberOfInstances++; } // The default constructor CounterOfInstances() { CounterOfInstances.addInstance(); } }
public class CounterOfInstances { // Stores the total number of instances created. private static int numberOfInstances = 0; // Returns the total number of instances created. static int getCounter() { return numberOfInstances; } // Increases the total number of instances created. private static void addInstance() { numberOfInstances++; } // The default constructor CounterOfInstances() { CounterOfInstances.addInstance(); } }
public static void main(String[] args) { System.out.println("At the beginning, we have " + CounterOfInstances.getCounter() + " instances of the class."); // We create 10 new instances of the class CounterOfInstances. for (int i = 0; i < 10; i++) new CounterOfInstances(); System.out.println("We have now created " + CounterOfInstances.getCounter() + " instances of the class."); }
public static void main(String[] args) { System.out.println("At the beginning, we have " + CounterOfInstances.getCounter() + " instances of the class."); // We create 10 new instances of the class CounterOfInstances. for (int i = 0; i < 10; i++) new CounterOfInstances(); System.out.println("We have now created " + CounterOfInstances.getCounter() + " instances of the class."); }
public static void main(String[] args) { System.out.println("At the beginning, we have " + CounterOfInstances.getCounter() + " instances of the class."); // We create 10 new instances of the class CounterOfInstances. for (int i = 0; i < 10; i++) new CounterOfInstances(); System.out.println("We have now created " + CounterOfInstances.getCounter() + " instances of the class."); }
public class Car { private String colour; private int year; private int speed; // add an instance variable for the object ID. private int id; // add a class variable for the number of Car // objects instantiated. private static int numberOfCars = 0;
...
}
public class Car { private String colour; private int year; private int speed; // add an instance variable for the object ID. private int id; // add a class variable for the number of Car // objects instantiated. private static int numberOfCars = 0;
...
}
public class Car { private String colour; private int year; private int speed; // add an instance variable for the object ID. private int id; // add a class variable for the number of Car // objects instantiated. private static int numberOfCars = 0;
...
}
public class Car { private String colour; private int year; private int speed; // add an instance variable for the object ID. private int id; // add a class variable for the number of Car // objects instantiated. private static int numberOfCars = 0;
...
}
public class Car { private String colour; private int year; private int speed; // add an instance variable for the object ID. private int id; // add a class variable for the number of Car // objects instantiated. private static int numberOfCars = 0;
...
}
public class Car { private String colour; private int year; private int speed; // add an instance variable for the object ID. private int id; // add a class variable for the number of Car // objects instantiated. private static int numberOfCars = 0;
...
}
public class Car { ... // Constructor public Car(String carColour, int maxSpeed, int startYear) { year = startYear; colour = carColour; speed = maxSpeed; // increment number of Cars // and assign ID number id = ++numberOfCars; }
...
}
public class Car { ... // Constructor public Car(String carColour, int maxSpeed, int startYear) { year = startYear; colour = carColour; speed = maxSpeed; // increment number of Cars // and assign ID number id = ++numberOfCars; }
...
}
public class Car { ... // Constructor public Car(String carColour, int maxSpeed, int startYear) { year = startYear; colour = carColour; speed = maxSpeed; // increment number of Cars // and assign ID number id = ++numberOfCars; }
...
}
public class Car { ... // Constructor public Car(String carColour, int maxSpeed, int startYear) { year = startYear; colour = carColour; speed = maxSpeed; // increment number of Cars // and assign ID number id = ++numberOfCars; }
...
}
public class Car { ... // Constructor public Car(String carColour, int maxSpeed, int startYear) { year = startYear; colour = carColour; speed = maxSpeed; // increment number of Cars // and assign ID number id = ++numberOfCars; }
...
}
You can use the Car constructor to set the id instance variable and increment the numberOfCars class variable.
public class Car { ... // new method (getter) to return the ID instance variable public int getID() { return id; } // method to return total number of Cars public static int getNumberOfCars() { return numberOfCars; } ... }
public class Car { ... // new method (getter) to return the ID instance variable public int getID() { return id; } // method to return total number of Cars public static int getNumberOfCars() { return numberOfCars; } ... }
public class Car { ... // new method (getter) to return the ID instance variable public int getID() { return id; } // method to return total number of Cars public static int getNumberOfCars() { return numberOfCars; } ... }
public class Car { ... // getter method for Car’s year public int getYear() { return year; } ... }
public class Car { ... // getter method for Car’s year public int getYear() { return year; } ... }
public class Car { ... // getter method for Car’s colour public String getColour() { return colour; } // setter method for Car’s colour public void setColour(String newVal) { this.colour = newVal; } ... }
public class Car { ... // getter method for Car’s colour public String getColour() { return colour; } // setter method for Car’s colour public void setColour(String newVal) { this.colour = newVal; } ... }
public class Car { ... // getter method for Car’s colour public String getColour() { return colour; } // setter method for Car’s colour public void setColour(String newVal) { this.colour = newVal; } ... }
public class Car { ... // getter method for Car’s speed public int getSpeed() { return speed; } // method for increasing Car’s speed public void speedUp(int increment) { this.speed += increment; } // method for decreasing Car’s speed public void speedDown(int decrement) { this.speed -= decrement; } ... }
public class Car { ... // getter method for Car’s speed public int getSpeed() { return speed; } // method for increasing Car’s speed public void speedUp(int increment) { this.speed += increment; } // method for decreasing Car’s speed public void speedDown(int decrement) { this.speed -= decrement; } ... }
public class Car { ... // getter method for Car’s speed public int getSpeed() { return speed; } // method for increasing Car’s speed public void speedUp(int increment) { this.speed += increment; } // method for decreasing Car’s speed public void speedDown(int decrement) { this.speed -= decrement; } ... }
public class Car { ... // getter method for Car’s speed public int getSpeed() { return speed; } // method for increasing Car’s speed public void speedUp(int increment) { this.speed += increment; } // method for decreasing Car’s speed public void speedDown(int decrement) { this.speed -= decrement; } ... }
public class Car { ... // Main method public static void main(String[] args) { Car audiCar = new Car("red", 100, 2010); System.out.println(Car.numberOfCars); System.out.println(audiCar.id); audiCar.getNumberOfCars(); // warning! System.out.println(audiCar.numberOfCars); // warning! } }
public class Car { ... // Main method public static void main(String[] args) { Car audiCar = new Car("red", 100, 2010); System.out.println(Car.numberOfCars); System.out.println(audiCar.id); audiCar.getNumberOfCars(); // warning! System.out.println(audiCar.numberOfCars); // warning! } }
public class Car { ... // Main method public static void main(String[] args) { Car audiCar = new Car("red", 100, 2010); System.out.println(Car.numberOfCars); System.out.println(audiCar.id); audiCar.getNumberOfCars(); // warning! System.out.println(audiCar.numberOfCars); // warning! } }
public class Car { ... // Main method public static void main(String[] args) { Car audiCar = new Car("red", 100, 2010); System.out.println(Car.numberOfCars); System.out.println(audiCar.id); audiCar.getNumberOfCars(); // warning! System.out.println(audiCar.numberOfCars); // warning! } }
public class Car { ... // Main method public static void main(String[] args) { Car audiCar = new Car("red", 100, 2010); System.out.println(Car.numberOfCars); System.out.println(audiCar.id); audiCar.getNumberOfCars(); // warning! System.out.println(audiCar.numberOfCars); // warning! } }
You can also refer to static methods with an object reference; discouraged because it does not make it clear that they are class methods.
public class Car { ... // Main method public static void main(String[] args) { Car audiCar = new Car("red", 100, 2010); System.out.println(Car.numberOfCars); System.out.println(audiCar.id); audiCar.getNumberOfCars(); // warning! System.out.println(audiCar.numberOfCars); // warning! } }
You can also refer to static fields with an object reference; discouraged because it does not make it clear that they are class variables.
must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.
final can never be reassigned to refer to an different object.
constant a class variable.
final can never be reassigned to refer to an different object.
constant a class variable.
error if your program tries to do so.
separated by an underscore (_).
static final double PI = 3.141592653589793;
the content of the method.
constructor calls a non-final method, a subclass may redefine that method with surprising or undesirable results.
class ChessPlaying { final String getFirstPlayer() { return "WHITE"; } } class MyChessRules extends ChessPlaying { @Override public final String getFirstPlayer() { return "BLACK"; // compilation error: overridden method is final } }
class ChessPlaying { final String getFirstPlayer() { return "WHITE"; } } class MyChessRules extends ChessPlaying { @Override public final String getFirstPlayer() { return "BLACK"; // compilation error: overridden method is final } }
class ChessPlaying { final String getFirstPlayer() { return "WHITE"; } } class MyChessRules extends ChessPlaying { @Override public final String getFirstPlayer() { return "BLACK"; // compilation error: overridden method is final } }
the content of the method.
constructor calls a non-final method, a subclass may redefine that method with surprising or undesirable results.
class Point1d { int x; } class Point2d extends Point1d { int y; } final class Point3d extends Point2d { int z; } class FinalClassExample { public static void main(String args[]) { Point3d obj = new Point3d();
System.out.println("x = " + obj.x + "\ny = " + obj.y + "\nz = " + obj.z); } }
class Point1d { int x; } class Point2d extends Point1d { int y; } final class Point3d extends Point2d { int z; } class FinalClassExample { public static void main(String args[]) { Point3d obj = new Point3d();
System.out.println("x = " + obj.x + "\ny = " + obj.y + "\nz = " + obj.z); } }
class Point1d { int x; } class Point2d extends Point1d { int y; } final class Point3d extends Point2d { int z; } class FinalClassExample { public static void main(String args[]) { Point3d obj = new Point3d();
System.out.println("x = " + obj.x + "\ny = " + obj.y + "\nz = " + obj.z); } }
class Point1d { int x; } class Point2d extends Point1d { int y; } final class Point3d extends Point2d { int z; } class FinalClassExample { public static void main(String args[]) { Point3d obj = new Point3d();
System.out.println("x = " + obj.x + "\ny = " + obj.y + "\nz = " + obj.z); } }
We would get a compile- time error if we tried to extend this class further.
its sole purpose is to be extended.
Otherwise, a compile error will be thrown.
method’s body (implementation) is provided by the subclass.
methods of the super class, unless the subclass is also an abstract class.
Can abstract methods be final?
public abstract class SuperClass { abstract void m(); // abstract method } class SubClass extends SuperClass { void m() { ... // must implement the abstract method! } }
public abstract class SuperClass { abstract void m(); // abstract method } class SubClass extends SuperClass { void m() { ... // must implement the abstract method! } }
public abstract class Van { private String name; Van(String vanName){ name = vanName; } public abstract void goFast(); // an abstract method public abstract void changeColor(); // another abstract method public void start() { System.out.println("Van " + name + " started"); } }
public abstract class Van { private String name; Van(String vanName){ name = vanName; } public abstract void goFast(); // an abstract method public abstract void changeColor(); // another abstract method public void start() { System.out.println("Van " + name + " started"); } }
public abstract class Van { private String name; Van(String vanName){ name = vanName; } public abstract void goFast(); // an abstract method public abstract void changeColor(); // another abstract method public void start() { System.out.println("Van " + name + " started"); } }
public abstract class Van { private String name; Van(String vanName){ name = vanName; } public abstract void goFast(); // an abstract method public abstract void changeColor(); // another abstract method public void start() { System.out.println("Van " + name + " started"); } }
public class Caravan extends Van { Caravan(String vanName) { super(vanName); } public void goFast() { System.out.println("Caravan going fast!"); } public void changeColor() { ... } public static void main(String[] args) { Caravan c1 = new Caravan("Fury"); c1.start(); } }
public class Caravan extends Van { Caravan(String vanName) { super(vanName); } public void goFast() { System.out.println("Caravan going fast!"); } public void changeColor() { ... } public static void main(String[] args) { Caravan c1 = new Caravan("Fury"); c1.start(); } }
public class Caravan extends Van { Caravan(String vanName) { super(vanName); } public void goFast() { System.out.println("Caravan going fast!"); } public void changeColor() { ... } public static void main(String[] args) { Caravan c1 = new Caravan("Fury"); c1.start(); } }
public class Caravan extends Van { Caravan(String vanName) { super(vanName); } public void goFast() { System.out.println("Caravan going fast!"); } public void changeColor() { ... } public static void main(String[] args) { Caravan c1 = new Caravan("Fury"); c1.start(); } }
public class Caravan extends Van { Caravan(String vanName) { super(vanName); } public void goFast() { System.out.println("Caravan going fast!"); } public void changeColor() { ... } public static void main(String[] args) { Caravan c1 = new Caravan("Fury"); c1.start(); } }
extended.
extended.
public final abstract class ErrorAbstractFinal { abstract void run(); } public class Eleni { public static void main(String[] args) { ErrorAbstractFinal err1 = new ErrorAbstractFinal(); } } Exception in thread "main" java.lang.Error: Unresolved compilation problem: The class ErrorAbstractFinal can be either abstract or final, not both
Compiler output: