Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. • Class’s Client
  2. o Any class that uses class X is called a client of X
  3. • Public vs. Private
  4. o Public constructors and methods of a class constitute its interface with classes that use it – its clients
  5. o All fields are usually declared private – they are hidden from clients
  6. o Static constants occasionally can be public
  7. o “Helper” methods that are needed only inside the class are declared private
  8. o A private field is accessible anywhere within the class’s source code.
  9. o Any object can access and modify a private field of another object of the same class.
  10. • Accessors and Modifiers
  11. o A programmer often provides methods, called accessors, that return values of private fields; methods that set values of private fields are called modifiers or mutators.
  12. o Accessors’ names often start with get, and modifiers’ names often start with set
  13. o These are not precise categories; the same method can modify several fields or modify a field and also return its old or new value.
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20. • Encapsulation
  21. o Hiding the implementation details of a class (making all fields and helper methods private) is called encapsulation
  22. o Encapsulation helps in program maintenance; a change in one class does not affect other classes
  23. o A client of a class interacts with the class only through well-documented public constructors and methods; this facilitates team development
  24. • Constructors
  25. o A constructor is a procedure for creating objects of the class.
  26. o A constructor often initializes an object’s fields.
  27. o Constructors do not have a return type (not even void) and they do not return a value.
  28. o All constructors in a class have the same name – the name of the class.
  29. o Constructors may take parameters.
  30. o If a class has more than one constructor, they must have different numbers and/or types of parameters
  31. o Programmers often provide a “no-args” constructor that takes no parameters (aka arguments)
  32. o If a programmer does not define any constructors, Java provides one default no-args constructor, which allocates memory and sets fields to default values.
  33. o Constructors of a class can call each other using the keyword this – a good way to avoid duplicating code.
  34.  
  35.  
  36. • Operator new
  37. o Constructors are invoked using the operator new
  38. o Parameters passed to new must match the number, types, and order of parameters expected by one of the constructors.
  39. o You must create an object before you can use it; the new operator is a way to do it.
  40.  i.e ratio = new Fraction (3, 4)
  41. • Methods
  42. o To define a method:
  43.  Decide between public and private (usually private)
  44.  Give it a name
  45.  Specify the types of parameters and give them names
  46.  Specify the method’s return type or choose void
  47.  Write the method’s code
  48. o A method is always defined inside a class
  49. o A method returns a value of the specified type unless it is declared void; the return type can be any primitive data type or a class type
  50. o A method’s parameters can be of any primitive data types or class types
  51. o A method name starts with a lowercase letter
  52. o Method names often sound like verbs
  53. o The name of a method that returns the value of a field often starts with get:
  54.  getWidth, getX
  55. o The name of a method that sets the value of a field often starts with set
  56.  setLocation, setText
  57.  
  58. • Passing Parameters to Constructors and Methods
  59. o Any expression that has an appropriate data type can serve as a parameter
  60. o A “smaller” type can be promoted to a “larger” type (for example, int to long, float to double)
  61. o Int is promoted to double when necessary
  62. o Objects are always passed as references: the reference is copied, not the object.
  63. o A method can change an object passed to it as a parameter because the method gets a reference to the original object.
  64. o Inside a method, this refers to the object for which the method was called. This can be passed to other constructors and methods as a parameter
  65. • Return statement
  66. o A method, unless void, returns a value of the specified type to the calling method
  67. o The return statement is used to immediately quit the method and return a value
  68. o A void method can use the return statement to exit early
  69. o If its return type is a class, the method returns a reference to an object (or null)
  70. o Often the returned object is created in the method using new. For example:
  71.  Return new Fraction(denom, num)
  72. • Overloaded Methods
  73. o Methods of the same class that have the same name but different numbers or types of parameters are called overloaded methods
  74. o Use overloaded methods when they perform tasks
  75. o The compiler treats overloaded methods as completely different methods
  76. o The compiler knows which one to call based on the number and the types of the parameters passed to the method
  77. • Static Fields
  78. o A static field (aka class field or class variable) is shared by all objects of the class
  79. o A non-static field (aka instance field or instance variable) belongs to an individual object
  80. o A static field can hold a constant shared by all objects of the class
  81. o A static field can be used to collect statistics or totals for all objects of the class (for example, total sales for all vending machines)
  82. o Static fields are stored with the class code, separately from instance variables that describe an individual object
  83. o Public static fields, usually global constants, are referred to in other classes using dot notation: ClassName.constName
  84. o Usually static fields are NOT initialized in constructors (they are initialized either in declarations or in public static methods)
  85. o If a class has only static fields, there is no point in creating objects of that class (all of them would be identical)
  86. o Math and System are examples of the above. They have no public constructors and cannot be instantiated.
  87. • Static Methods
  88. o Static methods can access and manipulate class’s static fields
  89. o Static methods cannot access non-static fields or call non-static methods of the class
  90. o Static methods are called using “dot notation”: ClassName.statMethod(…)
  91. o Main is static and therefore cannot access non-static fields or call non-static methods of its class
  92.  
  93. • Non-Static Methods
  94. o A non-static method is called for a particular object using “dot notation”: objName.instMethod
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement