You are on page 1of 2

Class design Most importantly, a class should represent a single concept.

Usually classes are named as nouns that describes its concept.Another useful category of classes are actors, for example the SCANNER class and the RANDOM class.They do some work for you.A class with no objects (rarely occurs) but with static methods and constants is called a utility class, example Math class.A common mistake is to turn an action into a class, for example for a program to compute pay checks, we may name the class ComputePayChecks.However it does not represent a noun or an object. Therefore the solution is to make a class paycheck and use a method such as computePayCheck.

classes.**Try to Maximize cohesion Immutable classes These are classes, which only have accessor methods and no mutator methods. This means that no method can change the state of an object of this class. Eg: String class Side effects ---A side effect of a method is any externally observable data modification. Pre con and post cond: A precondition is a requirement that the caller of a method must meet. If a method is called in violation of a precondition, the method is not responsible for computing the correct result. why preconditions?? 1. To restrict the parameters of a method 2. To require that a method is only called when it is in the appropriate state. For example, once a Scanner has run out of input, it is no longer legal to call the next method. Thus, a precondition for the next method is that the hasNext method returns true.4 Using an assertion An assertion is a logical condition in a program that you believe to be true. public double deposit(double amount) { assert amount >= 0; balance = balance + amount; } Assertion checking can be turned on or off. By default they are off. While assertion checking is enabled, if the asserted expression evaluates to true, the statement has no effect, but, if the asserted expression evaluates to false, the program terminates with an assertion error. POST-CONDITIONS if the methods preconditions have not been violated, the method makes a promise that the output will not be incorrect.

IN other words, If a method has been called in accordance with its preconditions, then it must ensure that its postconditions are valid.

STATIC METHODS static methods_ methods that do not belong to objects, but to the class. Eg: Math class. objects are not invoked on objects, but on Math class. This is also because, Math class doesnt have objects, as numbers are primive type. Therefore Math.sqrt(x) has no implicit parameter.(the OBJECT) Why use static methods? For computations involving primitive values. As these values are not objects, we can't invoke INSTANCE METHODS on them.
When defining a new static method: public static RETURNTYPE METHOD_NAME(parameter 1, parameter 2) When we need to call the method. CLASS_NAME.METHOD_NAME( parameter 1, parameter 2)

Cohesion The public interface of a class is cohesive if all of its features are related to the concept that the class represents.As the cashRegister class, had two concepts, we split it into a new class.Therefore as the cashRegister class depend on Coin class, most classes depend on other classes.A class depends on another class if it uses objects of that class.If many classes of a program depend on each other, then we say that the coupling between classes is high.Conversely, if there are few dependecies between classes, then we say that coupling is low. Why Coupling matters? If one class changes in the next release of the program, all the classes that depend on this class may be affected. If the change is drastic, the coupled classes must all be updated(change accordingly).Also, it we need to use a class in another program, we would have to take with it all the classes on which it depends.Therefore, it is a good practice to minimize the coupling(dependencies) between

STATIC FIELDS?? they are not instance fields, means that they do not belong to the new object created(every object does not get its own copy). But they belong to the class itself. but something like account number in Bankaccout, which increases sequentially. So we need each new bank account object created to have an account number 1 greater than the previous account number. private static int lastASsingedNumber = 1000; Then in constructor increment lastAssingedNumber(static field) and assign to bankaccount number(instance field)

STATIC METHODS static methods_ methods that do not belong to objects, but to the class. Eg: Math class. objects are not invoked on objects, but on Math class. This is also because, Math class doesnt have objects, as numbers are primive type. Therefore Math.sqrt(x) has no implicit parameter.(the OBJECT) Why use static methods? For computations involving primitive values. As these values are not objects, we can't invoke INSTANCE METHODS on them. When defining a new static method: public static RETURNTYPE METHOD_NAME(parameter 1, parameter 2) When we need to call the method. CLASS_NAME.METHOD_NA ME(parameter 1, parameter 2)

Scope of local variable extends from where it is declared until the end of the block that encloses it. Therefore two local variable of the same name declared and defined in two different methods will work fine, as the two variables are INDEPENDENT of each other. or their scopes are DISJOINT. We cannot use two different local variables having the same name in the same method. Private and Public fields and members of a class are called its memebers. private members can only be accessed within the same class. To use a public field or method outside its class, we must qualify the name, this is use the class name of that static method.(Math.sqrt()) Instance fields and methods are qualified by the object to which either the instance fieeld or method belongs.eg: harryschecking.getbalance()

STATIC FIELDS?? they are not instance fields, means that they do not belong to the new object created(every object does not get its own copy). But they belong to the class itself. but something like account number in Bankaccout, which increases sequentially. So we need each new bank account object created to have an account number 1 greater than the previous account number. private static int lastASsingedNumber = 1000; Then in constructor increment lastAssingedNumber(static field) and assign to bankaccount number(instance field) However, we should use static fields less often, as in OOP we use objects and instance fields. SCOPE:

You might also like