1 / 16

Classes and Class Members

Chapter 3. Classes and Class Members. 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. Private Implementation.

tadeo
Download Presentation

Classes and Class Members

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 Classes and Class Members

  2. 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. 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. 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. What is data hiding? • Data hiding is a term used to indicate that a class’s internal state is hidden from its clients.

  6. Delegation • A class should fulfill and restrict itself to one area of responsibility. • Classes should delegate outside responsibilities to other classes.

  7. 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. Public vs. Private Fields • The keywordspublic 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. 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. Accessor and Mutator Methods • 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. 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. What is a constructor? • A constructor is a special method that creates an instance of your class.

  13. 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. 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. 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. The this Keyword • Is a self-reference to the current object • Provided only in instance methods, not in static methods

More Related