Presentation is loading. Please wait.

Presentation is loading. Please wait.

Written by Paul Pu All Right Reservedwww.torontocollege.com Controlling Access to a Member(Java) public, private, protected (default)

Similar presentations


Presentation on theme: "Written by Paul Pu All Right Reservedwww.torontocollege.com Controlling Access to a Member(Java) public, private, protected (default)"— Presentation transcript:

1 Written by Paul Pu All Right Reservedwww.torontocollege.com Controlling Access to a Member(Java) public, private, protected (default)

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

3 Written by Paul Pu All Right Reservedwww.torontocollege.com Controlling Access to a Member(Java) Make the member public. Then everybody,everywhere, can access it Private :you cans 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

4 Written by Paul Pu All Right Reservedwww.torontocollege.com 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.

5 Written by Paul Pu All Right Reservedwww.torontocollege.com 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 As assembly. This is effectively protected OR internal.( There is no concept of protected AND internal).

6 Written by Paul Pu All Right Reservedwww.torontocollege.com 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);

7 Written by Paul Pu All Right Reservedwww.torontocollege.com 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

8 Written by Paul Pu All Right Reservedwww.torontocollege.com 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.

9 Written by Paul Pu All Right Reservedwww.torontocollege.com 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. class variableinstance variablelocal variable

10 Written by Paul Pu All Right Reservedwww.torontocollege.com 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.field

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

12 Written by Paul Pu All Right Reservedwww.torontocollege.com 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.static field

13 Written by Paul Pu All Right Reservedwww.torontocollege.com 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 class method instance method Any method that is invoked with respect to an instance of a class.

14 Written by Paul Pu All Right Reservedwww.torontocollege.com 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.static method

15 Written by Paul Pu All Right Reservedwww.torontocollege.com 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.

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

17 Written by Paul Pu All Right Reservedwww.torontocollege.com 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); }

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

19 Written by Paul Pu All Right Reservedwww.torontocollege.com 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); }

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

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

22 Written by Paul Pu All Right Reservedwww.torontocollege.com 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.

23 Written by Paul Pu All Right Reservedwww.torontocollege.com 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 :

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

25 Written by Paul Pu All Right Reservedwww.torontocollege.com 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());...

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

27 Written by Paul Pu All Right Reservedwww.torontocollege.com 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; }

28 Written by Paul Pu All Right Reservedwww.torontocollege.com 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; }

29 Written by Paul Pu All Right Reservedwww.torontocollege.com 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());...

30 Written by Paul Pu All Right Reservedwww.torontocollege.com 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?

31 Written by Paul Pu All Right Reservedwww.torontocollege.com 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; }

32 Written by Paul Pu All Right Reservedwww.torontocollege.com 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:

33 Written by Paul Pu All Right Reservedwww.torontocollege.com 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; } }


Download ppt "Written by Paul Pu All Right Reservedwww.torontocollege.com Controlling Access to a Member(Java) public, private, protected (default)"

Similar presentations


Ads by Google