160 likes | 256 Views
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.
E N D
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 • The private implementation of a class is the detailed explanation of how the class does its work. • Clients do not know these details.
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.
What is data hiding? • Data hiding is a term used to indicate that a class’s internal state is hidden from its clients.
Delegation • A class should fulfill and restrict itself to one area of responsibility. • Classes should delegate outside responsibilities to other classes.
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
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.
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.
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()
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
What is a constructor? • A constructor is a special method that creates an instance of your class.
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
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
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()
The this Keyword • Is a self-reference to the current object • Provided only in instance methods, not in static methods