Presentation is loading. Please wait.

Presentation is loading. Please wait.

Instance methods.

Similar presentations


Presentation on theme: "Instance methods."— Presentation transcript:

1 Instance methods

2 Method We found that it's more appropriate to put these method inside the BankAccount class

3 An ideal class The BankAccount class is ideally suited to contain/store: Information about BankAccount ( instance variables) Operations on BankAccount variables ( method) Remember that program = data + algorithm

4 New notation to invoke a method

5 Terminology A class method Methods defined with the keyword static
An instance method Methods defined without the keyword static

6 Shadowing a instance variable (1)
When the method has: local/parameter variable = class/instance variable The class/instance variable can no longer accessible with the short hand notation in that scope

7 “This” (1) The Java compiler always assume that the type of the variable this is:

8 “This” (2) A variable name used inside a method:
that matches the name of a local/parameter variable refers to the local/parameter variable Otherwise: it refers to a class variable or an instance variable

9 Shadowing a instance variable (2)
Overcome the shadowing problem with instance variables: Use the full name to access the instance variable:

10 Object-oriented programming (OOP)
An object is an entity with some state information and a set of well-defined operations It can be used to represent something in the real world It can be used to organize information used inside a computer program.

11 Implementing an object
Represent the state information of the object accurately  with (instance) variables,  and Represent the operations on the object accurately  with (instance) methods

12 Copying an object (1) Both an object and an array are accessed through a reference variable

13 Copying an object (2)

14 Copying an object (3)

15 Copying an object (4)

16 Classes with private variables (1)
Security/correctness considerations in computer programming

17 Classes with private variables (2)
If there exist complex relationships between different  instance variables, we must maintain the relationship correctly Granting unrestricted access to the instance variables can result in incorrect changes and can give result in data inconsistency

18 Classes with private variables (3)
public = there are no access restrictions to a variable or  method

19 Classes with private variables (4)
private = access is restricted to the same class

20 Classes with private variables (5)


Download ppt "Instance methods."

Similar presentations


Ads by Google