Download presentation
Presentation is loading. Please wait.
Published byBruno Randall Modified over 9 years ago
1
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
2
Please review the following sections in your textbook Core 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 final Copyright SAIT
3
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 class public class Count { private int serialNumber; private static int counter = 0; public Count() { counter++; serialNumber = counter; } This means that it doesn’t get created in the object, but stays 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 final Copyright SAIT Topic 3.6.1
4
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 final Copyright SAIT Topic 3.6.1 This means that it doesn’t get created in the object, but stays in the class It can be accessed from other classes and objects by calling the class and the item name Example: Count.serialNumber
5
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 final Copyright 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); }
6
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 constant public 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 final Copyright SAIT Topic 3.6.3
7
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 final Copyright SAIT Topic 3.6.4
8
Access Control public 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 final Copyright SAIT Topic 3.6.4 Data hiding
9
Access Control ____________________________________________ ModifierSameSameSubclassUniverse ClassPackage ____________________________________________ publicYesYesYesYes protectedYesYesYes defaultYesYes privateYes ____________________________________________ Note: default is not a keyword -- it indicates situations where a modifier is not supplied CPRG 215 Module 3.6 - Access Modifiers, static, and final Copyright SAIT Topic 3.6.4
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.