4. java intro class

480 views
303 views

Published on

Bai Giang Khoa CNTT

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
480
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

4. java intro class

  1. 1. Introduction to Class Object-Oriented Programming
  2. 2. Module Introduction 1. Classes and Objects 2. Instance variables 3. Methods FPT- APTECH 2/28
  3. 3. #1 – Classes and Objects  Class declaration  Constructors  Creating objects FPT- APTECH 3/28
  4. 4. Creating Class  The template or blueprint from which objects are actually made.  Syntax: class ClassName{ //definition of class }  All code that you write in Java is inside a class.  Classes can be built by extending other classes FPT- APTECH 4/28
  5. 5. Constructor - how to instance an object?  Same name as the class.  A class can have more than one constructor.  A constructor can take zero, one, or more parameters.  Has no return value.  Always called with the new operator.  Use for initializing variables and invoking any methods that may be for initialization.  If a class does not have an explicit constructor, a default constructor (no-argument) is provided. FPT- APTECH 5/28
  6. 6. Dissecting the Rectangle class Name of class 1. Use Naming rules Data member 2. Always captain first letter • WhatConstructor data does it need to 3. Use Noun for naming know? • Define the way to instance object 1. Same name as the class name Methods 2. Like function in C but • Whathave no do? value can it return 1. Like function in C 2. Use Verb for naming 3. Always lower first letter FPT- APTECH 6/28
  7. 7. Creating objects (instance object)  An object must be created before using in a program. 1. Declare a variable to store the object reference. 1. Objects can only be manipulated via references 2. Creating an object. 1. Using the new operator in conjunction with a call to a constructor FPT- APTECH 7/28
  8. 8. #2 – Data Member  Instance Variables  Class Variables FPT- APTECH 8/28
  9. 9. Instance variables  The values of those variables define the state of object.  All instances of a class have the same instance variables  May have different values inside them.  Has an “access modifier” associated with it. FPT- APTECH 9/28
  10. 10. Example of Instance variables recA length = 3.0 width = 4.0 FPT- APTECH recB length = 6.4 width = 4.7 10/28
  11. 11. Class Variables (Static Variables)  Variable that is accessed without using an object of a class.  Declare using the static keyword.  Only one copy of a static variable is shared by all the objects of the class.  Change in the value of static variable is reflected by all the objects of the class. FPT- APTECH 11/28
  12. 12. Example of Static Variables FPT- APTECH 12/28
  13. 13. #3 - Methods  Definition  Instance Method  Calling method and Passing Arguments by Value.  Calling method and Passing Arguments by Reference.  Static Methods  Variable Argument Methods FPT- APTECH 13/28
  14. 14. Method  A method is defined as the actual implementation of an operation on an object.  Syntax: access_specifier modifier datatype method_name (parameter_list) { //body of the method }  The method name should begin with a lowercase verb and follow with a proper noun. FPT- APTECH 14/28
  15. 15. Instance Method  A function defined in a class  Invoked by an instance object and can access instance variables  Provide a mechanism for accessing the private data stored in an object FPT- APTECH 15/28
  16. 16. Invoking Methods  Using the '.' operator  Syntax:  <the object reference> ‘.’ <the method to be invoked> FPT- APTECH 16/28
  17. 17. Calling method and Passing Arguments by Value.  Value from calling method is passed as an argument to the called method.  Any change made to that passed value in the called method will not modify the value in the calling method.  Variables of primitive types (int, float ..) are passed by value. FPT- APTECH 17/28
  18. 18. Example of Calling method and Passing Arguments by Value. FPT- APTECH 18/28
  19. 19. Calling method and Passing Arguments by Reference.  Called method to change the value of the parameter passed to if from the calling method.  When references are passed as parameters, the caller can change the values stored but not the reference variables. FPT- APTECH 19/28
  20. 20. Example of Calling method and Passing Arguments by Reference FPT- APTECH 20/28
  21. 21. Static Methods  Methods that do not operate on objects.  Can be called without creating any objects of the class.  Call static method, supply the name of the class.  Declared using the static keyword.  Can directly refer only to static variables and other static methods of the class.  Cannot refer to non-static methods and variables FPT- APTECH 21/28
  22. 22. Example of Static Methods FPT- APTECH 22/28
  23. 23. static methods usage  When a method doesn't need to access the object state  When a method only needs to access static fields of the class. FPT- APTECH 23/28
  24. 24. Variable Argument Methods  Allow calling a method with variable number of arguments. FPT- APTECH 24/28
  25. 25. That’s about all for today!  Classes and Objects  Instance variables  Methods Thank you all for your attention and patient !

×