1 / 19

Access Control

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

Télécharger la présentation

Access Control

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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.

  3. Top Level Classes • Accessible within package. • If public, accessible by everyone

  4. 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

  5. Member Access • If not specified, it has default package access • Example JHTP ch8 slide 52-59

  6. Expanded method override • public class A { • int i = 1; • int f() {return 1;} • static char g() {return 'A';} • }

  7. public class B extends A { • int i = 2; • int f() {return -1;} • static char g(){return 'B';} • }

  8. 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());

  9. A a = (A) b; • arr[0] = aa; • arr[1] = b; • arr[2] = a;

  10. 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); • } • }

  11. 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

  12. Package Example • package cis368; • public class pkgA { • int i = 1; • int f() {return 1;} • static char g() {return 'A';} • }

  13. package cis368; • public class pkgB extends pkgA { • int i = 2; • int f() {return -1;} • static char g(){return 'B';} • }

  14. ┌────────────────────────────── 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); • │ } • │ }

  15. 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

  16. 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.

  17. Compiling main • A:\>javac -classpath a:\ pkgTest.java • A:\>

  18. 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

  19. 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

More Related