Classes, Objects, and Methods

Classes, Objects, and Methods PowerPoint PPT Presentation


  • 124 Views
  • Uploaded on
  • Presentation posted in: General

Classes, Objects, and Methods. Define a class: definition of Java class should be in a file, and the file name should be the same name with extension .javaAfter compiling, the byte code keeps the same name, except with extension .classExample: Myclass is a class defined inside a file called Myclas

Download Presentation

Classes, Objects, and Methods

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


1. Classes, Objects, and Methods Class: a cataloged data package with special methods built-in Data portion describes the properties and method portion actions as the behaviors Object: a variable of a class, an instance of the class not only has data, but also has actions Example of object:

2. Classes, Objects, and Methods Define a class: definition of Java class should be in a file, and the file name should be the same name with extension .java After compiling, the byte code keeps the same name, except with extension .class Example: Myclass is a class defined inside a file called Myclass.java. The compiled byte code is Myclass.class Class is a blueprint, object is an instance of class.

3. Classes, Objects, and Methods Syntax of definition of class

4. Classes, Objects, and Methods Syntax of definition of class

5. Classes, Objects, and Methods Java modifiers

6. Classes, Objects, and Methods Let's introduce and use "public" modifier first Position of "public" modifier matters. no restrict on how to the class is used, if it appears in front of the class definition no restrict on how the instance variables or member data variables are used, if it appears in front of the declaration of members No restrict on how the class method is used, if it appears in front of the class method

7. Classes, Objects, and Methods Let's introduce and use "public" modifier first Position of "public" modifier matters. no restrict on how to the class is used, if it appears in front of the class definition (class name), for example,

8. Classes, Objects, and Methods Position of "public" modifier matters. no restrict on how the instance variables or member data variables are used, if it appears in front of the declaration of members (in data portion), for example,

9. Classes, Objects, and Methods Position of "public" modifier matters. No restrict on how the class method is used, if it appears in front of the class method, for example,

10. Classes, Objects, and Methods Declare objects (instance variables): just like declaration of primitive data Example: Java file is SpeciesFirstTry.java

13. Classes, Objects, and Methods How do we use the class we define before? We can to create another Java file which declares an instance (object) of the class in a special method which system calls first. How to use class members and methods: use member operator "." Example:

16. Classes, Objects, and Methods More about class methods Methods of a class are actions or behaviors, which can be taken by an object of the class To perform the action on the object, we say it is method "invocation". A class method can be invoked or called on objects by another method of the same class or even the methods of another class.

17. Classes, Objects, and Methods Therefore, we often say "the method is called or invoked" or the other method calls the method or invokes the method.

18. Classes, Objects, and Methods return-method: method which returns a value Use return statement May have multiple return statements void method: method without returning a value The return status should match the type of method (object, primitive data type, void)

19. Classes, Objects, and Methods Parameters passing to invoked method Must match to the arguments of the method Syntax of class method

20. Classes, Objects, and Methods Example of passing parameters to invoked methods

25. Classes, Objects, and Methods Two kinds of passing parameters to invoked methods Passing by value (duplicate value and passing over) Passing by reference (make alias variable, especially used in passing parameters of objects or arrays by reference) Example of passing by value

26. Classes, Objects, and Methods The methods inside the class implicitly recognize the members in the same class. If you want to explicitly recognize the class, the class members, or the class methods within the class, you can use keyword "this". For example in readInput() method of SpeciesFirstTry, you can use instead of

27. Classes, Objects, and Methods Question is in what case we do need to use "this"? Whenever or wherever we find the names of class, class members, or class methods conflict with the current class name, class members and class method, we can use this to identify the difference. Sometimes, it is very convenient to use, especially when we use overloading and overriding methods.

28. Classes, Objects, and Methods Variable scope: availability of variables local and global variables local variable is defined inside a curly brackets {} used to to define a block or a method scope of a local variable is only limited to the local block global variable is defined outside any methods scope of a global variable can be extended to any method

29. Classes, Objects, and Methods Example:

31. Classes, Objects, and Methods Information Hiding (abstraction) and Encapsulation design a method which can be used easily without understanding the details of the code inside the method. some information are hidden for programming' concise modulus based programming all we should know is the communication between the calling and called method (way to use)

32. Classes, Objects, and Methods Information Hiding (abstraction) and Encapsulation In order to use or reuse your class and class methods, it is common to comment preconditions and postconditions Preconditions are conditions which must be true, before invoking the method Postcondition describe effects of a method called. It performs action through return value of the method.

33. Classes, Objects, and Methods public and private modifiers "public" modifier makes declaration of objects of the class available publicly, if public modifier is used in front of class definition "public" modifier makes the data members (or instance variables) of the class and the methods of the class accessible, if public modifier is used in front of declaration of members and methods in side the definition of the class Instead, you can use "private" modifier to protect your class, data members, or methods inside class. For example:

37. Classes, Objects, and Methods normally all instance variables or data members are marked "private" and most methods are marked "public". You can use names of instance variables anywhere inside the class definition, no matter the instance variables are preceded "public" or "private". You can use names of instance variables outside the class definition, if instance variables are preceded "public".

38. Classes, Objects, and Methods You can't access of instance variables outside the class definition, if the instance variables are marked "private". If you do want to access the private instance variables outside the class definition, you have to design an access method. Example:

46. Classes, Objects, and Methods Another example:

55. Classes, Objects, and Methods Abstract Data Type (ADT): data type programmers do not need to know how the value, storage, operations implemented ADT in Java can be classified into two parts: interface: all need to know is how to use (including headings of the class methods, and comments) implementation: how the ADT class is realized including private instance variables, and definitions of both private and public methods

56. Classes, Objects, and Methods Example: distinct between interface and implementation How to make your class as ADT? place comments before definition about how to use data and methods all data instance variables are marked "private" public accessor method to read and write data in other object; public methods to input, output and manipulating data how to use the methods with comments before helping methods are marked "private"

57. Classes, Objects, and Methods Benefit of ADT class: Use class and class methods easily without changing programs which use a ADT class, even after modifying implementation of the ADT class efficiently update your class "javadoc" program can be used to generate your java documents by inputting the comment statements from the original your Java code Rules: use /* … */ for interface comments and use //…. For implementation comments

66. Classes, Objects, and Methods Create variable of class, or object has two steps declaration creation

74. Classes, Objects, and Methods Passing class objects as parameter to class method "this" is used to distinct the class instance, invoked object and the object as parameter Example:

83. Classes, Objects, and Methods Homework Assignment: Read chapter 4 of Walter Savitch's textbook Do self-test questions through 1-21 Do Exercises 1, 2, 3, on Page 230 to 231 Due date:

  • Login