190 likes | 292 Views
This guide provides an overview of access control mechanisms in Java, detailing the different access levels (Public, Private, Protected, Package) and their implications for class and member accessibility. It elaborates on how these controls affect inheritance and instance interaction, using practical code examples. Access constraints are crucial for software design, and understanding them is essential for object-oriented development. The document also explains error handling related to improper access in Java, contributing to better coding practices.
E N D
Access Control All fields may be used in body of class All methods may be used in body of class Access outside of class controlled by : Public Private Protected Package
Access Control • If not specified, package access is assumed • Package access is an operating system function. You must have read rights to directory containing package and files within it.
Top Level Classes • Accessible within package. • If public, accessible by everyone
Member Access • By default, members are accessible through out package • If public, member accessible anywhere • If private, only available within class • If protected, available to • All classes with package • Within subclasses • Between package and public
Member Access • If not specified, it has default package access • Example JHTP ch8 slide 52-59
Expanded method override • public class A { • int i = 1; • int f() {return 1;} • static char g() {return 'A';} • }
public class B extends A { • int i = 2; • int f() {return -1;} • static char g(){return 'B';} • }
public class Test{ • public static void main(String[] args) { • int i1 = 10; • A aa = new A(); • B b = new B(); • A arr[] = new A[4]; • System.out.println("b.i" + b.i); • System.out.println("b.f() = " + b.f()); • System.out.println("b.g() = " + b.g()); • System.out.println("B.g() = " + B.g());
A a = (A) b; • arr[0] = aa; • arr[1] = b; • arr[2] = a;
System.out.println("aa.f() = " + aa.f()); • System.out.println("a.i" + a.i); • System.out.println("a.f() = " + a.f()); • System.out.println("a.g() = " + a.g()); • System.out.println("A.g() = " + A.g()); • System.out.println("arr[0].f() = " + arr[0].f()); • System.out.println("arr[1].f() = " + arr[1].f()); • System.out.println("arr[2].f() = " + arr[2].f()); • System.out.println("end of class"); • System.exit(0); • } • }
Override/Polymorphism • A:\>java Test • b.i2 • b.f() = -1 • b.g() = B • B.g() = B • aa.f() = 1 • a.i1 • a.f() = -1 • a.g() = A • A.g() = A • arr[0].f() = 1 • arr[1].f() = -1 • arr[2].f() = -1 • end of class
Package Example • package cis368; • public class pkgA { • int i = 1; • int f() {return 1;} • static char g() {return 'A';} • }
package cis368; • public class pkgB extends pkgA { • int i = 2; • int f() {return -1;} • static char g(){return 'B';} • }
┌────────────────────────────── A:\pkgTest.java ────────── • │import cis368.*; • │public class pkgTest{ • │ public static void main(String[] args) { • │ int i1 = 10; • │ cis368.pkgA aa = new cis368.pkgA(); • │ cis368.pkgB b = new cis368.pkgB(); • │ cis368.pkgA arr[] = new cis368.pkgA[4]; • │ System.out.println("b.i" + b.i); • │ System.out.println("b.f() = " + b.f()); • │ System.out.println("b.g() = " + b.g()); • │ System.out.println("pkgB.g() = " + pkgB.g()); • │ pkgA a = (pkgA) b; • │ arr[0] = aa; • │ arr[1] = b; • │ arr[2] = a; • │ System.out.println("aa.f() = " + aa.f()); • │ System.out.println("a.i" + a.i); • │ System.out.println("a.f() = " + a.f()); • │ System.out.println("a.g() = " + a.g()); • │ System.out.println("pkgA.g() = " + pkgA.g()); • │ System.out.println("arr[0].f() = " + arr[0].f()); • │ System.out.println("arr[1].f() = " + arr[1].f()); • │ System.out.println("arr[2].f() = " + arr[2].f()); • │ System.out.println("end of class"); • │ System.exit(0); • │ } • │ }
Errors • A:\>javac pkgTest.java • pkgTest.java:8: i is not public in cis368.pkgB; cannot be accessed from outside • package • System.out.println("b.i" + b.i); • ^ • pkgTest.java:9: f() is not public in cis368.pkgB; cannot be accessed from outsi • e package • System.out.println("b.f() = " + b.f()); • ^ • pkgTest.java:10: g() is not public in cis368.pkgB; cannot be accessed from outs • de package • System.out.println("b.g() = " + b.g()); • ^ • pkgTest.java:11: cannot access pkgB • bad class file: .\pkgB.java • file does not contain class pkgB • Please remove or make sure it appears in the correct subdirectory of the classp • th. • System.out.println("pkgB.g() = " + pkgB.g()); • ^ • 4 errors
A:\>javac pkgA.java • A:\>javac pkgB.java • A:\>copy pkgA.class \cis368\pkgA.class • Overwrite \cis368\pkgA.class? (Yes/No/All): y • 1 file(s) copied. • A:\>copy pkgB.class \cis68\pkgB.class • The system cannot find the path specified. • 0 file(s) copied. • A:\>copy pkgB.class \cis368\pkgB.class • Overwrite \cis368\pkgB.class? (Yes/No/All): y • 1 file(s) copied.
Compiling main • A:\>javac -classpath a:\ pkgTest.java • A:\>
Directory • Directory of A:\ • 02/10/2005 01:35p 97 A.java • 02/10/2005 01:34p 101 B.java • 02/01/2005 03:58p 386 C.java • 02/10/2005 01:35p 312 A.class • 02/10/2005 01:35p 297 B.class • 02/10/2005 01:55p 1,416 Test.class • 02/10/2005 01:54p 859 Test.java • 02/10/2005 01:55p 34 comTest.bat • 02/10/2005 01:56p 11 exeTest.bat • 02/10/2005 02:00p <DIR> cis368 • 02/10/2005 05:34p 143 pkgA.java • 02/10/2005 05:34p 166 pkgB.java • 02/10/2005 05:40p 984 pkgTest.java • 02/10/2005 05:42p 1,448 pkgTest.class • 13 File(s) 6,254 bytes • 1 Dir(s) 1,446,400 bytes free
Running program • A:\>java pkgTest • b.i2 • b.f() = -1 • b.g() = B • pkgB.g() = B • aa.f() = 1 • a.i1 • a.f() = -1 • a.g() = A • pkgA.g() = A • arr[0].f() = 1 • arr[1].f() = -1 • arr[2].f() = -1 • end of class