advanced object-oriented programming lecture 5akridel/comp213-fall2016/lecture...

81
COMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static.

Upload: others

Post on 27-May-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

COMP 213Advanced Object-oriented Programming

Lecture 5

Modifiers: final and static.

Page 2: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 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., private or public.

• Java also provides a number of non-access modifiers to achieve many other functionalities:

• The staticmodifier for creating class methods and variables.

• The finalmodifier for finalizing the implementations of classes, methods, and variables.

• The abstractmodifier for creating abstract classes and methods.

• The synchronized and volatilemodifiers, which are used for threads.

Page 3: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 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., private or public.

• Java also provides a number of non-access modifiers to achieve many other functionalities:

• The staticmodifier for creating class methods and variables.

• The finalmodifier for finalizing the implementations of classes, methods, and variables.

• The abstractmodifier for creating abstract classes and methods.

• The synchronized and volatilemodifiers, which are used for threads.

Page 4: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 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., private or public.

• Java also provides a number of non-access modifiers to achieve many other functionalities:

• The staticmodifier for creating class methods and variables.

• The finalmodifier for finalizing the implementations of classes, methods, and variables.

• The abstractmodifier for creating abstract classes and methods.

• The synchronized and volatilemodifiers, which are used for threads.

Page 5: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 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., private or public.

• Java also provides a number of non-access modifiers to achieve many other functionalities:

• The staticmodifier for creating class methods and variables.

• The finalmodifier for finalizing the implementations of classes, methods, and variables.

• The abstractmodifier for creating abstract classes and methods.

• The synchronized and volatilemodifiers, which are used for threads.

Page 6: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 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., private or public.

• Java also provides a number of non-access modifiers to achieve many other functionalities:

• The staticmodifier for creating class methods and variables.

• The finalmodifier for finalizing the implementations of classes, methods, and variables.

• The abstractmodifier for creating abstract classes and methods.

• The synchronized and volatilemodifiers, which are used for threads.

Page 7: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 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., private or public.

• Java also provides a number of non-access modifiers to achieve many other functionalities:

• The staticmodifier for creating class methods and variables.

• The finalmodifier for finalizing the implementations of classes, methods, and variables.

• The abstractmodifier for creating abstract classes and methods.

• The synchronized and volatilemodifiers, which are used for threads.

Page 8: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

The staticmodifier

• static (or class) Variables:

• The static keyword is used to create variables that will exist independently of any instances created for the class. Only one copy of the static variable exists regardless of the number of instances of the class.

• Local variables cannot be declared static : if a variable is declared inside a method in a class, it only has scope "within" the method, i.e., it is local.

Page 9: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

The staticmodifier

• static (or class) Variables:

• The static keyword is used to create variables that will exist independently of any instances created for the class. Only one copy of the static variable exists regardless of the number of instances of the class.

• Local variables cannot be declared static : if a variable is declared inside a method in a class, it only has scope "within" the method, i.e., it is local.

Page 10: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

• static Methods

• The static keyword is used to create methods that will exist independently of any instances created for the class.

• staticmethods do not use any instance variables of any object of the class they are defined in.

• staticmethods take all the data from parameters and compute something from those parameters, with no reference to variables.

• static variables & methods can be accessed using the class name followed by a dot and the name of the variable or method.

The staticmodifier

Page 11: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

• static Methods

• The static keyword is used to create methods that will exist independently of any instances created for the class.

• staticmethods do not use any instance variables of any object of the class they are defined in.

• staticmethods take all the data from parameters and compute something from those parameters, with no reference to variables.

• static variables & methods can be accessed using the class name followed by a dot and the name of the variable or method.

The staticmodifier

Page 12: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

• static Methods

• The static keyword is used to create methods that will exist independently of any instances created for the class.

• staticmethods do not use any instance variables of any object of the class they are defined in.

• staticmethods take all the data from parameters and compute something from those parameters, with no reference to variables.

• static variables & methods can be accessed using the class name followed by a dot and the name of the variable or method.

The staticmodifier

Page 13: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

• static Methods

• The static keyword is used to create methods that will exist independently of any instances created for the class.

• staticmethods do not use any instance variables of any object of the class they are defined in.

• staticmethods take all the data from parameters and compute something from those parameters, with no reference to variables.

• static variables & methods can be accessed using the class name followed by a dot and the name of the variable or method.

The staticmodifier

Page 14: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 1

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 constructorCounterOfInstances() {

CounterOfInstances.addInstance();}

}

Page 15: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 1

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 constructorCounterOfInstances() {

CounterOfInstances.addInstance();}

}

Page 16: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 1

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 constructorCounterOfInstances() {

CounterOfInstances.addInstance();}

}

Page 17: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 1

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 constructorCounterOfInstances() {

CounterOfInstances.addInstance();}

}

Page 18: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 1

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.");

}

Page 19: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 1

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.");

}

Page 20: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 1

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.");

}

Page 21: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

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;

...

}

Page 22: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

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;

...

}

Page 23: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

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;

...

}

Page 24: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

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;

...

}

Page 25: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

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;

...

}

Page 26: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

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;

...

}

Page 27: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// Constructorpublic Car(String carColour, int maxSpeed, int startYear) {year = startYear;colour = carColour;speed = maxSpeed;// increment number of Cars// and assign ID numberid = ++numberOfCars;}

...

}

Page 28: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// Constructorpublic Car(String carColour, int maxSpeed, int startYear) {year = startYear;colour = carColour;speed = maxSpeed;// increment number of Cars// and assign ID numberid = ++numberOfCars;}

...

}

Page 29: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// Constructorpublic Car(String carColour, int maxSpeed, int startYear) {year = startYear;colour = carColour;speed = maxSpeed;// increment number of Cars// and assign ID numberid = ++numberOfCars;}

...

}

Page 30: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// Constructorpublic Car(String carColour, int maxSpeed, int startYear) {year = startYear;colour = carColour;speed = maxSpeed;// increment number of Cars// and assign ID numberid = ++numberOfCars;}

...

}

Page 31: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// Constructorpublic Car(String carColour, int maxSpeed, int startYear) {year = startYear;colour = carColour;speed = maxSpeed;// increment number of Cars// and assign ID numberid = ++numberOfCars;}

...

}You can use the Car

constructor to set the id instance variable and

increment the numberOfCarsclass variable.

Page 32: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// new method (getter) to return the ID instance variablepublic int getID() {return id;}// method to return total number of Carspublic static int getNumberOfCars() {return numberOfCars;}...

}

Page 33: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// new method (getter) to return the ID instance variablepublic int getID() {return id;}// method to return total number of Carspublic static int getNumberOfCars() {return numberOfCars;}...

}

Page 34: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// new method (getter) to return the ID instance variablepublic int getID() {return id;}// method to return total number of Carspublic static int getNumberOfCars() {return numberOfCars;}...

}

Page 35: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// getter method for Car’s yearpublic int getYear() {return year;}...

}

Page 36: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// getter method for Car’s yearpublic int getYear() {return year;}...

}

Page 37: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// getter method for Car’s colourpublic String getColour() {return colour;}// setter method for Car’s colourpublic void setColour(String newVal) {this.colour = newVal;}...

}

Page 38: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// getter method for Car’s colourpublic String getColour() {return colour;}// setter method for Car’s colourpublic void setColour(String newVal) {this.colour = newVal;}...

}

Page 39: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// getter method for Car’s colourpublic String getColour() {return colour;}// setter method for Car’s colourpublic void setColour(String newVal) {this.colour = newVal;}...

}

Page 40: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// getter method for Car’s speedpublic int getSpeed() {return speed;}// method for increasing Car’s speedpublic void speedUp(int increment) {this.speed += increment;}// method for decreasing Car’s speedpublic void speedDown(int decrement) {this.speed -= decrement;}...

}

Page 41: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// getter method for Car’s speedpublic int getSpeed() {return speed;}// method for increasing Car’s speedpublic void speedUp(int increment) {this.speed += increment;}// method for decreasing Car’s speedpublic void speedDown(int decrement) {this.speed -= decrement;}...

}

Page 42: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// getter method for Car’s speedpublic int getSpeed() {return speed;}// method for increasing Car’s speedpublic void speedUp(int increment) {this.speed += increment;}// method for decreasing Car’s speedpublic void speedDown(int decrement) {this.speed -= decrement;}...

}

Page 43: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// getter method for Car’s speedpublic int getSpeed() {return speed;}// method for increasing Car’s speedpublic void speedUp(int increment) {this.speed += increment;}// method for decreasing Car’s speedpublic void speedDown(int decrement) {this.speed -= decrement;}...

}

Page 44: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// Main methodpublic 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!

}}

Page 45: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// Main methodpublic 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!

}}

Page 46: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// Main methodpublic 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!

}}

Page 47: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// Main methodpublic 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!

}}

Page 48: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// Main methodpublic 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.

Page 49: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public class Car {...// Main methodpublic 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.

Page 50: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Notice

• Not all combinations of instance and class variables & methods are allowed:

• Instance methods can access instance variables and instance methods directly.

• Instance methods can access class variables and class methods directly.

• Class methods can access class variables and class methods directly.

• Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.

Page 51: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

The finalmodifier

• finalVariables:

• A final variable can be explicitly initialized only once. A reference variable declared final can never be reassigned to refer to an different object.

• With variables, the finalmodifier often is used together with static to make the constant a class variable.

Page 52: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

The finalmodifier

• finalVariables:

• A final variable can be explicitly initialized only once. A reference variable declared final can never be reassigned to refer to an different object.

• With variables, the finalmodifier often is used together with static to make the constant a class variable.

Page 53: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example

• Constants defined in this way cannot be reassigned; you get a compile-time error if your program tries to do so.

• By convention, the names of constant values are spelled in uppercaseletters. If the name is composed of more than one word, the words are separated by an underscore (_).

static final double PI = 3.141592653589793;

Page 54: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

The finalmodifier

• finalMethods:

• A finalmethod cannot be overridden/modified by any subclasses.

• The main purpose of making a method finalwould be to prevent outsiders changing the content of the method.

• Note: Methods called from constructors should generally be declared final. If a constructor calls a non-final method, a subclass may redefine that method with surprising or undesirable results.

Page 55: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example

class ChessPlaying {final String getFirstPlayer() {

return "WHITE";}

}

class MyChessRules extends ChessPlaying {@Overridepublic final String getFirstPlayer() {return "BLACK"; // compilation error: overridden method is final}

}

Page 56: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example

class ChessPlaying {final String getFirstPlayer() {

return "WHITE";}

}

class MyChessRules extends ChessPlaying {@Overridepublic final String getFirstPlayer() {return "BLACK"; // compilation error: overridden method is final}

}

Page 57: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example

class ChessPlaying {final String getFirstPlayer() {

return "WHITE";}

}

class MyChessRules extends ChessPlaying {@Overridepublic final String getFirstPlayer() {return "BLACK"; // compilation error: overridden method is final}

}

Page 58: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

The finalmodifier

• finalMethods:

• A finalmethod cannot be overridden/modified by any subclasses.

• The main purpose of making a method finalwould be to prevent outsiders changing the content of the method.

• Note: Methods called from constructors should generally be declared final. If a constructor calls a non-final method, a subclass may redefine that method with surprising or undesirable results.

Page 59: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

The finalmodifier

• finalClasses:

• The main purpose of declaring a class final is to prevent it from being subclassed.

• If a class is marked as final, then no class can inherit any feature from it.

Page 60: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example

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();obj.z = 10;obj.y = 1;obj.x = 5;System.out.println("x = " + obj.x + "\ny = " + obj.y + "\nz = " + obj.z);

}}

Page 61: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example

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();obj.z = 10;obj.y = 1;obj.x = 5;System.out.println("x = " + obj.x + "\ny = " + obj.y + "\nz = " + obj.z);

}}

Page 62: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example

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();obj.z = 10;obj.y = 1;obj.x = 5;System.out.println("x = " + obj.x + "\ny = " + obj.y + "\nz = " + obj.z);

}}

Page 63: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example

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();obj.z = 10;obj.y = 1;obj.x = 5;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.

Page 64: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

The abstractmodifier

• abstractClasses:

• An abstract class can never be instantiated. If a class is declared as abstract then its sole purpose is to be extended.

• If a class contains abstractmethods then the class should be declared abstract. Otherwise, a compile error will be thrown.

• An abstract class may contain both abstract methods as well “normal” methods.

Page 65: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

The abstractmodifier

• abstractMethods:

• An abstractmethod is a method declared without any implementation. The method’s body (implementation) is provided by the subclass.

• Any class that extends an abstract class must implement all the abstractmethods of the super class, unless the subclass is also an abstract class.

• If a class contains one or more abstractmethods, then the class must be declared abstract. An abstract class does not need to contain abstract methods.

• An abstractmethod ends with a semicolon, e.g: public abstract run();

Can abstractmethods be final?

Page 66: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 1

public abstract class SuperClass {abstract void m(); // abstract method

}

class SubClass extends SuperClass {void m() {

... // must implement the abstract method!}

}

Page 67: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 1

public abstract class SuperClass {abstract void m(); // abstract method

}

class SubClass extends SuperClass {void m() {

... // must implement the abstract method!}

}

Page 68: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public abstract class Van {

private String name;

Van(String vanName){name = vanName;

}public abstract void goFast(); // an abstract methodpublic abstract void changeColor(); // another abstract methodpublic void start() {

System.out.println("Van " + name + " started");}

}

Page 69: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public abstract class Van {

private String name;

Van(String vanName){name = vanName;

}public abstract void goFast(); // an abstract methodpublic abstract void changeColor(); // another abstract methodpublic void start() {

System.out.println("Van " + name + " started");}

}

Page 70: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public abstract class Van {

private String name;

Van(String vanName){name = vanName;

}public abstract void goFast(); // an abstract methodpublic abstract void changeColor(); // another abstract methodpublic void start() {

System.out.println("Van " + name + " started");}

}

Page 71: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

public abstract class Van {

private String name;

Van(String vanName){name = vanName;

}public abstract void goFast(); // an abstract methodpublic abstract void changeColor(); // another abstract methodpublic void start() {

System.out.println("Van " + name + " started");}

}

Page 72: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

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();

}}

Page 73: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

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();

}}

Page 74: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

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();

}}

Page 75: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

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();

}}

Page 76: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example 2

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();

}}

Page 77: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Question

• Can a class be declared both abstract and final?

Page 78: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Answer

• A class cannot be both abstract and final

• Reason: a final class cannot be extended and an abstract class is only meant to be extended.

Page 79: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Answer

• A class cannot be both abstract and final

• Reason: a final class cannot be extended and an abstract class is only meant to be extended.

Page 80: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Example

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:

Page 81: Advanced Object-oriented Programming Lecture 5akridel/COMP213-Fall2016/Lecture Slides/Lecture5.pdfCOMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static

Summary

• Non-access modifiers: final, static, abstract

• Next: More about modifiers – scope.