Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented...

101
COMP 213 Advanced Object-oriented Programming Lecture 6 Modifiers: scope.

Transcript of Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented...

Page 1: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

COMP 213Advanced Object-oriented Programming

Lecture 6

Modifiers: scope.

Page 2: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Recall: non-access modifiers

• static: variables & methods

• final : variables, methods & classes

• abstract: methods & classes

Page 3: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Recall: Reference Variables

• The only way you can access an object is through a reference variable.

• A reference variable is declared to be of a specific type and that type can never be changed.

• Reference variables can be declared as static variables, instance variables, method parameters, or local variables.

• A reference variable that is declared as final can never be reassigned to refer to a different object– the data within the object can be modified, but the reference variable cannot be changed.

Page 4: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Recall: Reference Variables

• The only way you can access an object is through a reference variable.

• A reference variable is declared to be of a specific type and that type can never be changed.

• Reference variables can be declared as static variables, instance variables, method parameters, or local variables.

• A reference variable that is declared as final can never be reassigned to refer to a different object– the data within the object can be modified, but the reference variable cannot be changed.

Page 5: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Recall: Reference Variables

• The only way you can access an object is through a reference variable.

• A reference variable is declared to be of a specific type and that type can never be changed.

• Reference variables can be declared as static variables, instance variables, method parameters, or local variables.

• A reference variable that is declared as final can never be reassigned to refer to a different object– the data within the object can be modified, but the reference variable cannot be changed.

Page 6: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Recall: Reference Variables

• The only way you can access an object is through a reference variable.

• A reference variable is declared to be of a specific type and that type can never be changed.

• Reference variables can be declared as static variables, instance variables, method parameters, or local variables.

• A reference variable that is declared as final can never be reassigned to refer to a different object– the data within the object can be modified, but the reference variable cannot be changed.

Page 7: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Recall: Reference Variables

• The only way you can access an object is through a reference variable.

• A reference variable is declared to be of a specific type and that type can never be changed.

• Reference variables can be declared as static variables, instance variables, method parameters, or local variables.

• A reference variable that is declared as final can never be reassigned to refer to a different object– the data within the object can be modified, but the reference variable cannot be changed.

Page 8: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example

class Reference {int number;String text;

Reference(String text, int number) {this.text = text;this.number = number;

}

String getText() {return text;

}

int getNumber() {return number;

}}

Page 9: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example

class Reference {int number;String text;

Reference(String text, int number) {this.text = text;this.number = number;

}

String getText() {return text;

}

int getNumber() {return number;

}}

Page 10: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example

class Reference {int number;String text;

Reference(String text, int number) {this.text = text;this.number = number;

}

String getText() {return text;

}

int getNumber() {return number;

}}

Page 11: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example

class Reference {int number;String text;

Reference(String text, int number) {this.text = text;this.number = number;

}

String getText() {return text;

}

int getNumber() {return number;

}}

Page 12: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example

class Reference {int number;String text;

Reference(String text, int number) {this.text = text;this.number = number;

}

String getText() {return text;

}

int getNumber() {return number;

}}

Page 13: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example

class ReferenceDemo {public static void main(String[] args) {// Declaration of Reference variable.Reference ref1, ref2;

// ref3 is declared final; it can't be reassigned or refer to different object.final Reference ref3;

// Assign ref1 with object Reference.ref1 = new Reference("This is the first reference variable", 1);

// Access method getNumber() of object Reference through variable ref1.int number = ref1.getNumber();System.out.println("number= " + number);...

}

Page 14: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example

class ReferenceDemo {public static void main(String[] args) {// Declaration of Reference variable.Reference ref1, ref2;

// ref3 is declared final; it can't be reassigned or refer to different object.final Reference ref3;

// Assign ref1 with object Reference.ref1 = new Reference("This is the first reference variable", 1);

// Access method getNumber() of object Reference through variable ref1.int number = ref1.getNumber();System.out.println("number= " + number);...

}

Page 15: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example

class ReferenceDemo {public static void main(String[] args) {// Declaration of Reference variable.Reference ref1, ref2;

// ref3 is declared final; it can't be reassigned or refer to different object.final Reference ref3;

// Assign ref1 with object Reference.ref1 = new Reference("This is the first reference variable", 1);

// Access method getNumber() of object Reference through variable ref1.int number = ref1.getNumber();System.out.println("number= " + number);...

}

Page 16: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example

class ReferenceDemo {public static void main(String[] args) {// Declaration of Reference variable.Reference ref1, ref2;

// ref3 is declared final; it can't be reassigned or refer to different object.final Reference ref3;

// Assign ref1 with object Reference.ref1 = new Reference("This is the first reference variable", 1);

// Access method getNumber() of object Reference through variable ref1.int number = ref1.getNumber();System.out.println("number= " + number);...

}

Page 17: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example

class ReferenceDemo {public static void main(String[] args) {// Declaration of Reference variable.Reference ref1, ref2;

// ref3 is declared final; it can't be reassigned or refer to different object.final Reference ref3;

// Assign ref1 with object Reference.ref1 = new Reference("This is the first reference variable", 1);

// Access method getNumber() of object Reference through variable ref1.int number = ref1.getNumber();System.out.println("number= " + number);...

}

Page 18: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example

class ReferenceDemo {...// Assign ref2 with object Reference.ref2 = new Reference("This is the second reference variable", 2);

// Passing ref2 as method parameter of printText() method.ReferenceDemo.printText(ref2);

// Assign ref3 with object Reference.ref3 = new Reference("This is the third reference variable", 3);

// try to reassign ref3 will cause a compile-time errorref3 = new Reference("Try to reassign", 3);}...

}

Page 19: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example

class ReferenceDemo {...// Assign ref2 with object Reference.ref2 = new Reference("This is the second reference variable", 2);

// Passing ref2 as method parameter of printText() method.ReferenceDemo.printText(ref2);

// Assign ref3 with object Reference.ref3 = new Reference("This is the third reference variable", 3);

// try to reassign ref3 will cause a compile-time errorref3 = new Reference("Try to reassign", 3);}...

}

Page 20: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example

class ReferenceDemo {...// Assign ref2 with object Reference.ref2 = new Reference("This is the second reference variable", 2);

// Passing ref2 as method parameter of printText() method.ReferenceDemo.printText(ref2);

// Assign ref3 with object Reference.ref3 = new Reference("This is the third reference variable", 3);

// try to reassign ref3 will cause a compile-time errorref3 = new Reference("Try to reassign", 3);}...

}

Page 21: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example

class ReferenceDemo {...// Assign ref2 with object Reference.ref2 = new Reference("This is the second reference variable", 2);

// Passing ref2 as method parameter of printText() method.ReferenceDemo.printText(ref2);

// Assign ref3 with object Reference.ref3 = new Reference("This is the third reference variable", 3);

// try to reassign ref3 will cause a compile-time errorref3 = new Reference("Try to reassign", 3);}...

}

Page 22: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example

class ReferenceDemo {...// Assign ref2 with object Reference.ref2 = new Reference("This is the second reference variable", 2);

// Passing ref2 as method parameter of printText() method.ReferenceDemo.printText(ref2);

// Assign ref3 with object Reference.ref3 = new Reference("This is the third reference variable", 3);

// try to reassign ref3 will cause a compile-time errorref3 = new Reference("Try to reassign", 3);}...

}

Page 23: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example

class ReferenceDemo {...public static void printText(Reference reference) {

String text = reference.getText();System.out.println(text);

}}

Page 24: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Classes

• A class contains declarations of members.Members can be:

• fields (including constants)

• methods

• classes

• Each of these has a name, as do classes themselves.Every name has a scope, which determines where the name may be used.

Page 25: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Classes

• A class contains declarations of members.Members can be:

• fields (including constants)

• methods

• classes

• Each of these has a name, as do classes themselves.Every name has a scope, which determines where the name may be used.

Page 26: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Classes

• A class contains declarations of members.Members can be:

• fields (including constants)

• methods

• classes

• Each of these has a name, as do classes themselves.Every name has a scope, which determines where the name may be used.

Page 27: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Scope of Variables

A variable may be local to a block of code:

if (x > 10) {int i;for (i = x; i >= 10; i--) {

System.out.println(report[i]);}

} else {i = 0; // compile-time error

}

Page 28: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Scope of Variables

A variable may be local to a block of code:

if (x > 10) {int i;for (i = x; i >= 10; i--) {

System.out.println(report[i]);}

} else {i = 0; // compile-time error

}

Page 29: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Scope of Variables

A variable may be local to a block of code:

if (x > 10) {int i;for (i = x; i >= 10; i--) {

System.out.println(report[i]);}

} else {i = 0; // compile-time error

}

Variable 𝑖 is local to this block of code.

Page 30: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Scope of Variables

A variable may be local to a block of code:

if (x > 10) {int i;for (i = x; i >= 10; i--) {

System.out.println(report[i]);}

} else {i = 0; // compile-time error

}

Variable 𝑖 is local to this block of code.

Page 31: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Scope of Variables

A variable may be local to a method:

public void printTo(int n) {int i = 0;while (i < n ) {

System.out.println(report[i++]); }

}

public void sillyMethod() {i = 0 ; // compile-time error}

Note also that 𝑛 is local to the method.

Page 32: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Scope of Variables

A variable may be local to a method:

public void printTo(int n) {int i = 0;while (i < n ) {

System.out.println(report[i++]); }

}

public void sillyMethod() {i = 0 ; // compile-time error}

Note also that 𝑛 is local to the method.

Page 33: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Scope of Variables

A variable may be local to a method:

public void printTo(int n) {int i = 0;while (i < n ) {

System.out.println(report[i++]); }

}

public void sillyMethod() {i = 0 ; // compile-time error}

Variable 𝑖 is local to this method.

Note also that 𝑛 is local to the method.

Page 34: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Scope of Variables

A variable may be local to a method:

public void printTo(int n) {int i = 0;while (i < n ) {

System.out.println(report[i++]); }

}

public void sillyMethod() {i = 0 ; // compile-time error}

Variable 𝑖 is local to this method.

Note also that 𝑛 is local to the method.

Page 35: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Scope of Variables

A variable may be local to a method:

public void printTo(int n) {int i = 0;while (i < n ) {

System.out.println(report[i++]); }

}

public void sillyMethod() {i = 0 ; // compile-time error}

Variable 𝑖 is local to this method.

Note also that 𝑛 is local to the method.

Page 36: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Debugging tip!

A common error:

class Point {int xCoord; // not assigned toPoint(int x, int y) {

int xCoord = x;...

}...

}

Page 37: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Debugging tip!

A common error:

class Point {int xCoord; // not assigned toPoint(int x, int y) {

int xCoord = x;...

}...

}

Page 38: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Debugging tip!

A common error:

class Point {int xCoord; // not assigned toPoint(int x, int y) {

int xCoord = x;...

}...

}

Page 39: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Debugging tip!

A common error:

class Point {int xCoord; // not assigned toPoint(int x, int y) {

int xCoord = x;...

}...

}

Page 40: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Scope of Variables

A variable may be declared in a class:

class Point {int xCoord;...

}

…in which case, it is a field.

Page 41: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

The Scope of Fields

As declared in class Point, the field xCoord is “visible” throughout the class.In fact, it is visible throughout the package that class Point is in.

class SomeOtherClass {...Point p = new Point(2,5);p.xCoord = 10;...

}

The scope of fields, and members in general, can be controlled using the scope modifiers: public, protected, private, and “default” (no scope modifier used).

Page 42: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

The Scope of Fields

As declared in class Point, the field xCoord is “visible” throughout the class.In fact, it is visible throughout the package that class Point is in.

class SomeOtherClass {...Point p = new Point(2,5);p.xCoord = 10;...

}

The scope of fields, and members in general, can be controlled using the scope modifiers: public, protected, private, and “default” (no scope modifier used).

Page 43: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Why restrict Scope?

• The goal of programming is to implement a given functionality. This usually involves choosing some representation for the data used by the program, but the functionality is the important thing.

• If the details of the data representations (implementations) are hidden, then code can be more freely modified– but of course the code has to be modified in such a way that the functionality is preserved.

Page 44: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Why restrict Scope?

• The goal of programming is to implement a given functionality. This usually involves choosing some representation for the data used by the program, but the functionality is the important thing.

• If the details of the data representations (implementations) are hidden, then code can be more freely modified– but of course the code has to be modified in such a way that the functionality is preserved.

Page 45: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Why restrict Scope?

• The goal of programming is to implement a given functionality. This usually involves choosing some representation for the data used by the program, but the functionality is the important thing.

• If the details of the data representations (implementations) are hidden, then code can be more freely modified– but of course the code has to be modified in such a way that the functionality is preserved.

Page 46: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Digression: mistakes are good

• Changes to code are an unfortunate but understandable result of the fact that humans – including programmers – don’t have perfect insight.

• A difficult problem may require a complex solution, and finding an elegant solution may require several false starts.

• These false starts often provide the insight that leads to a better solution...

• ...then after working on that for a while, you start to see how it should have been done in the first place.

• The term refactoring is perhaps an attempt to impose some respectability on this fitful lurching towards elegance.

Page 47: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Digression: mistakes are good

• Changes to code are an unfortunate but understandable result of the fact that humans – including programmers – don’t have perfect insight.

• A difficult problem may require a complex solution, and finding an elegant solution may require several false starts.

• These false starts often provide the insight that leads to a better solution...

• ...then after working on that for a while, you start to see how it should have been done in the first place.

• The term refactoring is perhaps an attempt to impose some respectability on this fitful lurching towards elegance.

Page 48: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Digression: mistakes are good

• Changes to code are an unfortunate but understandable result of the fact that humans – including programmers – don’t have perfect insight.

• A difficult problem may require a complex solution, and finding an elegant solution may require several false starts.

• These false starts often provide the insight that leads to a better solution...

• ...then after working on that for a while, you start to see how it should have been done in the first place.

• The term refactoring is perhaps an attempt to impose some respectability on this fitful lurching towards elegance.

Page 49: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Digression: mistakes are good

• Changes to code are an unfortunate but understandable result of the fact that humans – including programmers – don’t have perfect insight.

• A difficult problem may require a complex solution, and finding an elegant solution may require several false starts.

• These false starts often provide the insight that leads to a better solution...

• ...then after working on that for a while, you start to see how it should have been done in the first place.

• The term refactoring is perhaps an attempt to impose some respectability on this fitful lurching towards elegance.

Page 50: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Digression: mistakes are good

• Changes to code are an unfortunate but understandable result of the fact that humans – including programmers – don’t have perfect insight.

• A difficult problem may require a complex solution, and finding an elegant solution may require several false starts.

• These false starts often provide the insight that leads to a better solution...

• ...then after working on that for a while, you start to see how it should have been done in the first place.

• The term refactoring is perhaps an attempt to impose some respectability on this fitful lurching towards elegance.

Page 51: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Digression: refactoring

Refactoring is:

• A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behaviour. It is a disciplined way to clean up code that minimizes the chances of introducing bugs.

Page 52: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Why restrict Scope?

• Software changes (is “refactored”).

• Very often one piece of software uses another piece of software, which uses another (and so on).How many Java classes have you used that you haven’t written yourself? String, Array, ...hopefully a lot more by the end of this module!

• So one piece of software can depend upon many others.

Page 53: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Why restrict Scope?

• Software changes (is “refactored”).

• Very often one piece of software uses another piece of software, which uses another (and so on).How many Java classes have you used that you haven’t written yourself? String, Array, ...hopefully a lot more by the end of this module!

• So one piece of software can depend upon many others.

Page 54: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Why restrict Scope?

• Software changes (is “refactored”).

• Very often one piece of software uses another piece of software, which uses another (and so on).How many Java classes have you used that you haven’t written yourself? String, Array, ...hopefully a lot more by the end of this module!

• So one piece of software can depend upon many others.

Page 55: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Why restrict Scope?

• Software changes (is “refactored”).

• Very often one piece of software uses another piece of software, which uses another (and so on).How many Java classes have you used that you haven’t written yourself? String, Array, ...hopefully a lot more by the end of this module!

• So one piece of software can depend upon many others.

Page 56: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Why restrict Scope?

• Programs use data: input, output & “intermediate values” are all data, and all data is represented in a particular way.E.g., a sequence of values might be represented by an array.

• But programs also work with this data — this is the functionality.

• It turns out that data representations change more often than functionality.

• We can protect software that uses our software from any changes we may make by hiding the details of data representation. How? — by using scope modifiers (especially private).

Page 57: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Why restrict Scope?

• Programs use data: input, output & “intermediate values” are all data, and all data is represented in a particular way.E.g., a sequence of values might be represented by an array.

• But programs also work with this data — this is the functionality.

• It turns out that data representations change more often than functionality.

• We can protect software that uses our software from any changes we may make by hiding the details of data representation. How? — by using scope modifiers (especially private).

Page 58: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Why restrict Scope?

• Programs use data: input, output & “intermediate values” are all data, and all data is represented in a particular way.E.g., a sequence of values might be represented by an array.

• But programs also work with this data — this is the functionality.

• It turns out that data representations change more often than functionality.

• We can protect software that uses our software from any changes we may make by hiding the details of data representation. How? — by using scope modifiers (especially private).

Page 59: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Why restrict Scope?

• Programs use data: input, output & “intermediate values” are all data, and all data is represented in a particular way.E.g., a sequence of values might be represented by an array.

• But programs also work with this data — this is the functionality.

• It turns out that data representations change more often than functionality.

• We can protect software that uses our software from any changes we may make by hiding the details of data representation. How? — by using scope modifiers (especially private).

Page 60: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Why restrict Scope?

• Programs use data: input, output & “intermediate values” are all data, and all data is represented in a particular way.E.g., a sequence of values might be represented by an array.

• But programs also work with this data — this is the functionality.

• It turns out that data representations change more often than functionality.

• We can protect software that uses our software from any changes we may make by hiding the details of data representation. How? — by using scope modifiers (especially private).

Page 61: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Abstract Data Types

Recall:

• An abstract data type (ADT) consists of:

• a set of abstract data values, and

• some specified operations on those values.

• “Abstract” means no particular data representation (implementation) is given.

Page 62: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example ADT: Stacks

• A Stack is a sequence of values with operations:

• push — add a value onto the top of the stack

• top — get the value at the top of the stack

• pop — remove the last value added to the stack

Page 63: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example ADT: Stacks

• A Stack is a sequence of values with operations:

• push — add a value onto the top of the stack

• top — get the value at the top of the stack

• pop — remove the last value added to the stack

Page 64: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

An implementation of Stacks

public class Stack {private int[] values;private int pointer;public Stack() {

values = new int[100];pointer = -1;

}public int top() {

return values[pointer];}public void pop() {

pointer--;}public void push(int v) {

values[++pointer] = v;}

}

Page 65: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

An implementation of Stacks

public class Stack {private int[] values;private int pointer;public Stack() {

values = new int[100];pointer = -1;

}public int top() {

return values[pointer];}public void pop() {

pointer--;}public void push(int v) {

values[++pointer] = v;}

}

Page 66: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

An implementation of Stacks

public class Stack {private int[] values;private int pointer;public Stack() {

values = new int[100];pointer = -1;

}public int top() {

return values[pointer];}public void pop() {

pointer--;}public void push(int v) {

values[++pointer] = v;}

}

Page 67: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

An implementation of Stacks

public class Stack {private int[] values;private int pointer;public Stack() {

values = new int[100];pointer = -1;

}public int top() {

return values[pointer];}public void pop() {

pointer--;}public void push(int v) {

values[++pointer] = v;}

}

Page 68: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

An implementation of Stacks

public class Stack {private int[] values;private int pointer;public Stack() {

values = new int[100];pointer = -1;

}public int top() {

return values[pointer];}public void pop() {

pointer--;}public void push(int v) {

values[++pointer] = v;}

}

Page 69: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

An implementation of Stacks

public class Stack {private int[] values;private int pointer;public Stack() {

values = new int[100];pointer = -1;

}public int top() {

return values[pointer];}public void pop() {

pointer--;}public void push(int v) {

values[++pointer] = v;}

}

Page 70: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Private is Hidden

Attempting to access a member outside its scope causes a compile-time error:

public class SomeOtherClass {...Stack st = ...;int noWay = st.values[0]; // compiler error...

}

Page 71: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Private is Hidden

Attempting to access a member outside its scope causes a compile-time error:

public class SomeOtherClass {...Stack st = ...;int noWay = st.values[0]; // compiler error...

}

Page 72: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Private is Hidden

Now that the representation details are hidden:

• Classes that use class Stack must follow the Last-In, First-Out restriction on accessing elements of the stack, because they can only use the public methods top, pop, and push.

• The “owner” (programmer) of class Stack is free to change the data representation (e.g., using a linked list instead of an array to store the elements)– as long as the functionality remains the same, client classes that use class Stackwon’t notice any difference.

Page 73: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Scope of Classes

• public – visible inside and outside its package (i.e., it can be imported into another package).

• default – visible only inside its own package (i.e., it cannot be imported into another package).

• private – visible only inside its own compilation unit (file: so it’s not visible in other classes in any other file).

Page 74: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Scope of Classes

• public – visible inside and outside its package (i.e., it can be imported into another package).

• default – visible only inside its own package (i.e., it cannot be imported into another package).

• private – visible only inside its own compilation unit (file: so it’s not visible in other classes in any other file).

Page 75: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Scope of Classes

• public – visible inside and outside its package (i.e., it can be imported into another package).

• default – visible only inside its own package (i.e., it cannot be imported into another package).

• private – visible only inside its own compilation unit (file: so it’s not visible in other classes in any other file).

Page 76: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Scope of Members

• public – visible everywhere the class is visible.

• default – visible only within the package, wherever the class is visible

• private – visible only within its class.

• protected – inside the package, visible wherever the class is; outside the package, visible only in subclasses.

Page 77: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Scope of Members

• public – visible everywhere the class is visible.

• default – visible only within the package, wherever the class is visible

• private – visible only within its class.

• protected – inside the package, visible wherever the class is; outside the package, visible only in subclasses.

Page 78: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Scope of Members

• public – visible everywhere the class is visible.

• default – visible only within the package, wherever the class is visible

• private – visible only within its class.

• protected – inside the package, visible wherever the class is; outside the package, visible only in subclasses.

Page 79: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Scope of Members

• public – visible everywhere the class is visible.

• default – visible only within the package, wherever the class is visible

• private – visible only within its class.

• protected – inside the package, visible wherever the class is; outside the package, visible only in subclasses.

Page 80: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Hiding Data Representation

public class Point {private int xCoord;private int yCoord;public Point(int x, int y) {

xCoord = x;yCoord = y;

}public void move(int dx, int dy) ...

}

Page 81: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Hiding Data Representation

• As a rule of thumb:Members of a class (fields, methods, etc.) should be declared private…unless there is good reason not to!

Page 82: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Hiding Data Representation

public class LabelledPoint extends Point {private String label;public LabelledPoint(int x, int y, String s) {xCoord = x; // compile-time error}

}

Page 83: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Working with hidden information

We could change class Point:

protected int xCoord;

…but why should xCoord be visible throughout the package?This spoils our neat hiding of data representation.

In this case, there is another solution...

Page 84: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Working with hidden information

We could change class Point:

protected int xCoord;

…but why should xCoord be visible throughout the package?This spoils our neat hiding of data representation.

In this case, there is another solution...

Page 85: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Working with hidden information

We could change class Point:

protected int xCoord;

…but why should xCoord be visible throughout the package?This spoils our neat hiding of data representation.

In this case, there is another solution...

Page 86: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Inheritance

• Recall that constructors are not inherited.

• They can, however, be invoked using the super keyword.

• This refers to the constructor of the immediate superclass (in this case, class Point)

Page 87: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Calling super

public class LabelledPoint extends Point {

private String label;

public LabelledPoint(int x, int y, String s) {super(x, y);label = s;

}}

Page 88: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Calling super

public class LabelledPoint extends Point {

private String label;

public LabelledPoint(int x, int y, String s) {super(x, y);label = s;

}}

Page 89: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Calling super

public class LabelledPoint extends Point {

private String label;

public LabelledPoint(int x, int y, String s) {super(x, y);label = s;

}}

Page 90: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Constructors

Constructors are typically used to instantiate objects,

i.e., give initial values to the fields in the newly-created instance.

At the level of the Java Virtual Machine (JVM), invoking a constructor:

• Sets aside memory for the instance and its fields

• Then executes the code in the constructor...

Page 91: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Executing a constructor

• If the code in the constructor body begins with a call of super, then the corresponding constructor of the immediate superclass is executed;

• If not, the compiler adds a call to a default constructor with no arguments: super()– if there is no such constructor, the compiler reports an error.

Page 92: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Executing a constructor

• If the code in the constructor body begins with a call of super, then the corresponding constructor of the immediate superclass is executed;

• If not, the compiler adds a call to a default constructor with no arguments: super()– if there is no such constructor, the compiler reports an error.

Page 93: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Executing a constructor

• If the code in the constructor body begins with a call of super, then the corresponding constructor of the immediate superclass is executed;

• If not, the compiler adds a call to a default constructor with no arguments: super()– if there is no such constructor, the compiler reports an error.

Page 94: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example

class SuperTest {public SuperTest(int i) {}

}

class MySubclass extends SuperTest {public MySubclass() {}

}

Page 95: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example

class SuperTest {public SuperTest(int i) {}

}

class MySubclass extends SuperTest {public MySubclass() {}

}

Page 96: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example

class SuperTest {public SuperTest(int i) {}

}

class MySubclass extends SuperTest {public MySubclass() {}

}

Page 97: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Example

class SuperTest {public SuperTest(int i) {}

}

class MySubclass extends SuperTest {public MySubclass() {}

}

Page 98: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Java: Poor Design?

• Generally, compilers doing things “behind the scenes” can be confusing;

• the compile-time error above is especially confusing since the following is perfectly acceptable:

class Confusion {// no constructorpublic static void main(String[] args) {

Confusion whaaaat = new Confusion();}

}

Page 99: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Java: Poor Design?

• Generally, compilers doing things “behind the scenes” can be confusing;

• the compile-time error above is especially confusing since the following is perfectly acceptable:

class Confusion {// no constructorpublic static void main(String[] args) {

Confusion whaaaat = new Confusion();}

}

Page 100: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Java: Poor Design?

• Generally, compilers doing things “behind the scenes” can be confusing;

• the compile-time error above is especially confusing since the following is perfectly acceptable:

class Confusion {// no constructorpublic static void main(String[] args) {

Confusion whaaaat = new Confusion();}

}

Page 101: Advanced Object-oriented Programming Lecture 6akridel/COMP213-Fall2016...Advanced Object-oriented Programming Lecture 6 Modifiers: scope. Recall: non-access modifiers • static: variables

Summary

• Blocks, methods, classes and packages define scope

• Hide everything that doesn’t need to be visible

• super

• Next: The Queue ADT