cprg 215 introduction to object-oriented programming with java module 3- introduction to object...
TRANSCRIPT
CPRG 215
Introduction to Object-Oriented Programming with Java
Module 3- Introduction to Object Oriented Programming concepts
Topic 3.6 Access Modifiers, static, and final
Produced by Harvey Peters, 2008Copyright SAIT
Please review the following sections
in your textbookCore Java, Volume I–Fundamentals, Eighth Edition
By Cay S. Horstmann & Gary Cornell
•Chapter 5 - Inheritance•Design Hints for Inheritance
CPRG 215 Module 3.6 - Access Modifiers, static, and finalCopyright SAIT
Class (static) Variables• Are shared among all instances of a class• Can be accessed from outside the class if
marked as public without an instance (object) of the classpublic class Count {
private int serialNumber;
private static int counter = 0;
public Count() {
counter++;
serialNumber = counter;
}
}
This means that itdoesn’t get createdin the object, butstays in the class
It can be accessed from other classes and objects by calling the class and the item name•Example:Count.serialNumber
CPRG 215 Module 3.6 - Access Modifiers, static, and finalCopyright SAIT
Topic 3.6.1
public class Count { private int serialNumber; private static int counter = 0; public Count() { counter++; serialNumber = counter; }}
Class (static) Variables
CPRG 215 Module 3.6 - Access Modifiers, static, and finalCopyright SAIT
Topic 3.6.1
This means that itdoesn’t get createdin the object, butstays in the class
It can be accessed from other classes and objects by calling the class and the item name•Example:Count.serialNumber
Class (static) Methods
• A static method can be invoked without any instance of the class to which it belongs
CPRG 215 Module 3.6 - Access Modifiers, static, and finalCopyright SAIT
Topic 3.6.2
public class GeneralFunction { public static int addUp(int x, int y) { return x + y; }}public class UseGeneral { public void method() { int a = 9; int b = 10; int c = GeneralFunction.addUp(a, b); System.out.println(“addUp() gives “ + c); }}
final• A final class cannot be subclassed
public final class MyClass• A final method cannot be overridden
public final void MyMethod()
• A final variable is a constantpublic final int MAX_ARRAY_SIZE = 25;
– roughly equivalent to “const” in C and C++– Can only be initialized during object construction
CPRG 215 Module 3.6 - Access Modifiers, static, and finalCopyright SAIT
Topic 3.6.3
Access Control• Data hiding
– Cornerstone of Encapsulation– if the data members of a class are not exposed,
the class user is forced to use the methods to alter the member variables -- validity checking can be done in the methods!
– We hide the data by using access modifiers.
CPRG 215 Module 3.6 - Access Modifiers, static, and finalCopyright SAIT
Topic 3.6.4
Access Controlpublic class MyDate { private int day, month, year;
public void tomorrow() { this.day = this.day + 1; // validate day range… }}
public class DateUser { public static void main(String args[ ]) { MyDate mydate = new MyDate(); mydate.day = 21; //illegal!!! }}
CPRG 215 Module 3.6 - Access Modifiers, static, and finalCopyright SAIT
Topic 3.6.4
•Data hiding
Access Control____________________________________________Modifier Same Same Subclass Universe
Class Package____________________________________________ public Yes Yes Yes Yes protected Yes Yes Yes default Yes Yes private Yes____________________________________________
Note: default is not a keyword -- it indicates situations where a modifier is not supplied
CPRG 215 Module 3.6 - Access Modifiers, static, and finalCopyright SAIT
Topic 3.6.4