cprg 215 introduction to object-oriented programming with java module 3- introduction to object...

9
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, 2008 Copyright SAIT

Upload: bruno-randall

Post on 13-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.6 Access Modifiers,

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

Page 2: CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.6 Access Modifiers,

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

Page 3: CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.6 Access Modifiers,

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

Page 4: CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.6 Access Modifiers,

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

Page 5: CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.6 Access Modifiers,

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

Page 6: CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.6 Access Modifiers,

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

Page 7: CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.6 Access Modifiers,

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

Page 8: CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.6 Access Modifiers,

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

Page 9: CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.6 Access Modifiers,

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