1 / 33

Controlling Access to a Member(Java)

Controlling Access to a Member(Java). public, private, protected (default). Controlling Access to a Member(Java). Specifier class subclass package world private X protected X X public X X X X package X X.

cynara
Download Presentation

Controlling Access to a Member(Java)

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. Controlling Access to a Member(Java) • public, private, protected (default) Written by Paul Pu All Right Reservedwww.torontocollege.com

  2. Controlling Access to a Member(Java) Specifier class subclass package world • private X • protected X X • public X X X X • package X X Written by Paul Pu All Right Reservedwww.torontocollege.com

  3. Controlling Access to a Member(Java) • Make the member public. Then everybody,everywhere, can access it • Private :you can’s touch that. The private keyword that means no one can access that member except that particular class. Other classes in the same package cannot access private member • Package/Friendly:Same Package classes can touch that • protected:Cross package inheritance Written by Paul Pu All Right Reservedwww.torontocollege.com

  4. Controlling Access to a Member(C#) private, protected, and public These three modifiers affect the accessibility of methods, fields and attributes. The private modifier ensures that the method, field or attribute is visible only within the class it is defined. The protected modifier extends this visibility to derived classes, and the public modifier declares the method, field or attribute to be visible to everyone. Written by Paul Pu All Right Reservedwww.torontocollege.com

  5. Controlling Access to a Member(C#) Internal and protected internal Internal: The members in class A that are marked internal are accessible to methods of class A’ assembly. Protected internal: The members in class A that are marked protected internal are accessible to methods of class A, to methods of classes derived from class A, and also to any class in A’s assembly. This is effectively protected OR internal.( There is no concept of protected AND internal). Written by Paul Pu All Right Reservedwww.torontocollege.com

  6. Controlling Access to a Member(C#) extern The extern keyword is basically used just for non-C# function calls, which also requires: using System.Runtime.InteropServices; and is used in conjunction with the DllImport attribute, so that your C# program can call functions in other DLLs. This is a very useful capability of the C# language, because there are cases where you need direct access to the underlying Windows API. Example: [DllImport("User32.dll")] public static extern int MessageBox(int h, string m, string c, int type); MessageBox(0, "Test", "My Message Box", 0); Written by Paul Pu All Right Reservedwww.torontocollege.com

  7. Work with the final keyword(JAVA) • The final keyword has slightly different meanings depending on the context, but in general it says “ This cannot be changed.” The final can be used: for data, methods and for a class Written by Paul Pu All Right Reservedwww.torontocollege.com

  8. Work with the seal keyword(C#) sealed • The sealed modifier ensures that a class cannot be inherited. Therefore, sealed classes cannot be abstract, nor can they have abstract or virtual methods. Written by Paul Pu All Right Reservedwww.torontocollege.com

  9. Important notes to access a class • Variable:An item of data named by an identifier. Each variable has a type, such as int or Object, and a scope. See also class variable, instance variable, local variable. Written by Paul Pu All Right Reservedwww.torontocollege.com

  10. Important notes to access a class • instance variable Any item of data that is associated with a particular object. Each instance of a class has its own copy of the instance variables defined in the class. Also called a field. Written by Paul Pu All Right Reservedwww.torontocollege.com

  11. Important notes to access a class • Static key word: • Class method • Class variable Written by Paul Pu All Right Reservedwww.torontocollege.com

  12. Important notes to access a class • class variable A data item associated with a particular class as a whole--not with particular instances of the class. Class variables are defined in class definitions. Also called a static field. Written by Paul Pu All Right Reservedwww.torontocollege.com

  13. Important notes to access a class • method A function defined in a class. There are instance method, class method. Unless specified otherwise, a method is not static. instance method Any method that is invoked with respect to an instance of a class. Written by Paul Pu All Right Reservedwww.torontocollege.com

  14. Important notes to access a class • class method A method that is invoked without reference to a particular object. Class methods affect the class as a whole, not a particular instance of the class. Also called a static method. Written by Paul Pu All Right Reservedwww.torontocollege.com

  15. Understanding Instance and Class Members • When you declare a member variable such as aFloat in MyClass: class Test { int i=47; } you declare an instance variable. Written by Paul Pu All Right Reservedwww.torontocollege.com

  16. Understanding Instance and Class Members class StaticTest { static int i=47; } Written by Paul Pu All Right Reservedwww.torontocollege.com

  17. Understanding Instance and Class Members //Java Code public class TestDriver { public static void main(String[] args) { Test ts1=new Test(); Test ts2=new Test(); ts1.i=ts1.i+1; System.out.println("object ts1 i ="+ts1.i); System.out.println("object ts2 i="+ts2.i); } } Written by Paul Pu All Right Reservedwww.torontocollege.com

  18. Understanding Instance and Class Members • What is the output? Why? Written by Paul Pu All Right Reservedwww.torontocollege.com

  19. Understanding Instance and Class Members //Java Code public class StDriver { public static void main(String[] args) { StaticTest.i=StaticTest.i+1; StaticTest ts1=new StaticTest(); StaticTest ts2=new StaticTest(); ts1.i=ts1.i+1; System.out.println("object ts i ="+StaticTest.i); System.out.println("object ts1 i ="+ts1.i); System.out.println("object ts2 i="+ts2.i); } } Written by Paul Pu All Right Reservedwww.torontocollege.com

  20. Understanding Instance and Class Members Test ts1=new Test(); Test ts2=new Test(); ts1.i=ts1.i+1; ts1.i 47 48 47 ts2.i Written by Paul Pu All Right Reservedwww.torontocollege.com

  21. Understanding Instance and Class Members StaticTest.i=StaticTest+1; StaticTest ts1=new StaticTest(); StaticTest ts2=new StaticTest(); StaticTest.i 47+1 48 ts1 ts2 Written by Paul Pu All Right Reservedwww.torontocollege.com

  22. Understanding Instance and Class Members • Methods are similar: Your classes can have instance methods and class methods. Instance methods operate on the current object's instance variables but also have access to the class variables. Class methods, on the other hand, cannot access the instance variables declared within the class (unless they create a new object and access them through the object). Also, class methods can be invoked on the class, you don't need an instance to call a class method. Written by Paul Pu All Right Reservedwww.torontocollege.com

  23. Understanding Instance and Class Members • By default, unless otherwise specified, a member declared within a class is an instance member. The class defined below has one instance variable--an integer named x--and two instance methods--x and setX--that let other objects set and query the value of x: Written by Paul Pu All Right Reservedwww.torontocollege.com

  24. Understanding Instance and Class Members class AnInteger { int x; public int x() { return x; } public void setX(int newX) { x = newX; } } Written by Paul Pu All Right Reservedwww.torontocollege.com

  25. Understanding Instance and Class Members . . . AnInteger myX = new AnIntegerX(); AnInteger anotherX = new AnIntegerX(); myX.setX(1); anotherX.x = 2; System.out.println("myX.x = " + myX.x()); System.out.println("anotherX.x = " + anotherX.x()); . . . Written by Paul Pu All Right Reservedwww.torontocollege.com

  26. Understanding Instance and Class Members • What is the output? Written by Paul Pu All Right Reservedwww.torontocollege.com

  27. Understanding Instance and Class Members To specify that a member variable is a class variable, use the static keyword. class AnInX { static int x; public int x() { return x; } public void setX(int newX) { x = newX; } } Written by Paul Pu All Right Reservedwww.torontocollege.com

  28. Understanding Instance and Class Members To specify that a member variable is a class variable, use the static keyword. class AnInX { static int x; public int x() { return x; } public void setX(int newX) { x = newX; } } Written by Paul Pu All Right Reservedwww.torontocollege.com

  29. Understanding Instance and Class Members . . . AnInX myX = new AnInX(); AnInX anotherX = new AnInX(); myX.setX(1); anotherX.x = 2; System.out.println("myX.x = " + myX.x()); System.out.println("anotherX.x = " + anotherX.x()); . . . Written by Paul Pu All Right Reservedwww.torontocollege.com

  30. Understanding Instance and Class Members class AnIntegerNamedX { static int x; public int x() { return x; } public void setX(int newX) { x = newX; } } // Does this program correct? Written by Paul Pu All Right Reservedwww.torontocollege.com

  31. Understanding Instance and Class Members //Fix it class AnIntegerNamedX { static int x; static public int x() { return x; } static public void setX(int newX) { x = newX; } } Written by Paul Pu All Right Reservedwww.torontocollege.com

  32. SingletonPattern //: SingletonPattern.java // The Singleton design pattern: you can // never instantiate more than one. // Since this isn't inherited from a Cloneable // base class and cloneability isn't added, // making it final prevents cloneability from // being added in any derived classes: Written by Paul Pu All Right Reservedwww.torontocollege.com

  33. Singleton Pattern final class Singleton { private static Singleton s = new Singleton(47); private int i; private Singleton(int x) { i = x; } public static Singleton getHandle() { return s; } public int getValue() { return i; } public void setValue(int x) { i = x; } } Written by Paul Pu All Right Reservedwww.torontocollege.com

More Related