Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Chapter 03

234 views

Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

Chapter 03

  1. 1. Classes and Class Members Chapter 3
  2. 2. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object that calls methods on another object or class. The interface details what the class does, but not how it does it.
  3. 3. 3 Private Implementation The private implementation of a class is the detailed explanation of how the class does its work. Clients do not know these details.
  4. 4. 3 Encapsulation A good contract promises a well- defined area of responsibility. Each class should fully encapsulate (contain) all its responsibilities. What the class does should be clear but the details of how it does it should be hidden.
  5. 5. 3 What is data hiding? Data hiding is a term used to indicate that a class’s internal state is hidden from its clients.
  6. 6. 3 Delegation A class should fulfill and restrict itself to one area of responsibility. Classes should delegate outside responsibilities to other classes.
  7. 7. 3 Class Fields Class fields hold the class data. Each field has a type: Intrinsic Types The Java language defines eight intrinsic types Java Library Types User-Defined Types Classes defined by the programmer
  8. 8. 3 Public vs. Private Fields The keywords public and private are referred to as access modifiers. Use private keyword to hide field from clients. Use public keyword to allow clients access to field.
  9. 9. 3 Methods Methods define the behavior of the class. Methods can return a value: Return type can be an intrinsic type or an object. Use the keyword void if a value is not to be returned by the method. Methods can accept parameters: A parameter is an object you pass in to the method when you call it. Parameters follow the same naming conventions as field names.
  10. 10. 3 Accessorand MutatorMethods Accessor methods return information Typically named to indicate that a value is being returned: getAge() getAccountBalance() Mutator methods modify the state of the object. Typically named to indicate that a value is being changed: setAge() addDeposit()
  11. 11. 3 Why Use Accessors and Mutators? Follows the concept of data hiding Allows the class designer to change how a method is implemented without rewriting clients
  12. 12. 3 What is a constructor? A constructor is a special method that creates an instance of your class.
  13. 13. 3 Constructors Constructors initialize objects with valid values. Constructors have special rules: May accept parameters Never marked with a return type A default constructor: Takes no parameters Is provided by the compiler if you do not provide one
  14. 14. 3 Static Members Static members belong to the class and are shared by all instances of the class. Declared using the static keyword Can be accessed without an instance of the class
  15. 15. 3 Dot Operator The dot operator indicates that a method or member field (right side of dot) belongs to the object or class (left side of dot). ClassName.someStaticMemberField ObjectName.someMethod()
  16. 16. 3 The this Keyword Is a self-reference to the current object Provided only in instance methods, not in static methods

×