1 / 5

CH 8 : Designing Classes – Good Time for Review!

CH 8 : Designing Classes – Good Time for Review!. Types – The computer stores Data as either: Pre-Defined Types : Primitives (i.e. int , double, char, etc…) <or> User-Defined Types : aka Abstract Data Types(ADT) Java uses the Class to implement these ADTs

lael
Download Presentation

CH 8 : Designing Classes – Good Time for Review!

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. CH 8 : Designing Classes – Good Time for Review! Types – The computer stores Data as either: Pre-Defined Types: Primitives (i.e. int, double, char, etc…) <or> User-Defined Types: aka Abstract Data Types(ADT) Java uses the Class to implement these ADTs Variables – Identify a storage location to represent either a primitive value or an ADT. A class variable will store a reference. Class – Everything in Java is run through the plumbing of a Class. A Class is an ADT representing a real world entity (aka Object). The class declares and encapsulates the Objects data and behavior. Instance Fields : ADT’s data (state) initialized in constructors. Methods : ADT behavior representing the public interface. Classes should represent a single concept. Class names should be nouns – person, place, or thing. Method names should be verbs – implying action. Encapsulation & Information Hiding – Client code manipulates an ADT’s data through the methods made available by the public interface. The specifics are hidden from the Client code for safety & reusability. Data Abstraction –Specific coding is performed somewhere else.

  2. Scope – Where a variable is defined and has meaning. Local variables – within the block defined. Declared & assigned value within the block. Parameter variables – basically local Declared in method heading. Assigned through method call. A link to Client code. Out of scope when methods exits. Instance Fields–Global to the life of the Object. Shadowing or Name Precedence: When a local variable & instance field have the same name. The local variable has precedence. Use this to distinguish between the two. this refers to the invoking object called the implicit parameter. Pre & Post Conditions: Commenting conditions of a method. Javadoc inserts these before the method. Take a quick glance! Pre: Conditions assumed to be true when method is invoked. Post: Status of something upon completion of method. Invariants - Class or Loop: Some state is true throughout the lifespan of the loop or the Object.

  3. *Not Specifically Tested! * Cohesion– All public interface features of a class (ie methodology) should be closely related to the single concept that the class represents. * Coupling– Refers to the degree in which classes rely on each other. It is a good practice to have a high degree of cohesion and to minimize the coupling between classes. *Accessor & *MutatorMethods: accessor methods don’t change the ADT’s state (acct.getBal( ) ). mutator methods can change the state ( acct.dep(100)). Immutable Class: class with no mutator methods. String class is immutable. None of its methods can actually alter the original string. Copies are made and manipulated. An immutable class is very safe because you cannot modify the state of the implicit parameter. Immutable classes do not allow for side effects.

  4. Parameter Passing – Important!!! Java passes ALL parameters to a method by value. A copy of each parameter’s value from the invoking call is sent to the called method. The formal or explicit parameter in the method heading gets a copy of what was sent. What does that mean? Primitive client parameters cannot be changed - period! The copy is changed in the method, but the values at the invoking level are not! Object references are a bit different. While the actual reference cannot change, the objects state can change. Changing an objects state through parameter passing is referred as a side effect, and it should be avoided at all costs! Ex: int x=3; BankAcct acct = new BankAcct(500); change(x, acct); : public static void change(int a, BankAcct b) { a = 5; // x unchanged b.deposit(200); // acct.balance = 700 b = new BankAcct( ); // acct unchanged //Q - What if switch last 2 lines?

  5. Static Modifiers : Belong to a Class – not an Object. • Static Methods(aka Class Methods) • Belongs to the class – not an Object of the class. • Invoked by the class – not an Object of the class! • Rational : Encapsulate some computation involving only numbers. • num = Math.pow(x,n); • Static Fields – Not unique for every Object of the class! public class BankAccount { public static final double OVERDRAFT_FEE = 5; private static intacctNum= 0; public BankAccount ( ) { acctNum++; } • Static values are not unique (i.e. they’re global & maintain value) • These fields apply to the entire class, not a specific instance. • Notice that they are initialized when declared – not in constructor! • Static constants can be either public or private. This public constant can be accessed anywhere by BankAccount. OVERDRAFT_FEE.

More Related