Chapter 7 - Classes and methods III: Static methods and variables. In this chapter we will: describe user-defined classes instance variables, constructors, instance methods; present several examples of classes; discuss the concepts of mutability and visibility; describe method overloading.
Chapter Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N Kamin, D Mickunas, E Reingold Chapter Preview In this chapter we will: • describe user-defined classes – instance variables – constructors – instance methods • present several examples of classes • discuss the concepts of mutability and visibility • describe method overloading Object-Oriented Programming • OOP supports the view that programs are composed of interacting objects • Objects are composed of – values known as attributes or instance variables – operations (actions) that can be performed on these values know as instance methods • Messages requesting an action or value are sent to objects • Objects respond to messages that are in their protocols or interfaces Objects • Encapsulate data values within a single entity • Their behavior is often general enough to allow reuse in a variety of situations • Often form a basis from which other objects can be derived using a mechanism known as inheritance • Are a type container that is stored on the system heap • A client is program that uses an object Client Rights • To declare variables of the class type • To create instances of the class using constructors • To send messages to instances of the class by invoking class instance methods • To know the class public interface – instance method names – parameter number and types – return types • To know the instance methods that alter (mutate) the instance Class Rights • To define the class public interface • To hide all the implementation details from the client • To protect internal data from client access • To change implementation details at any time, provided the public interface remains intact • To change the public interface with client concurrence Revised Class Definition public class name { declarations of instance variables constructor definitions method definitions } • Every class needs one or more constructor definitions Revised Class Definition • Instance variables – local data contained in class • Method definitions – describe the class interface and how it responds to each client message • Constructors definitions – describe how to initialize instance variables in a new object Constructors • Look like regular instance methods • Never have a return type • Always have the same name as the class name • May have parameters • Default constructors have no parameters • Constructors can be overloaded (more than one definition in the same class) Visibility and UML Diagrams • In UML diagrams – private variables and methods are indicated using a leading minus sign as a prefix – public variables and methods are indicates using a leading plus sign as a prefix – a blank prefix means that the variables and methods are not annotated and will have their default visibility Mutation • An object can be changed by assigning a new value to one or more of its instance variables • Example: d = new DrawingBox(); c = new Clock(); c.set(10, 20); c.display(d, 50, 50, 50); c.set(5, 40); Mutability • Transforming an object from one state to another • Only objects can be mutated, primitive values are not (e.g x+4 does not change x) • Objects are only mutable if its interface includes mutating methods Nonmutation • Creating a new object similar to original, but including the desired change • Note the return type is Clock, not void • Example: public Clock set_nonmut (int hour, int Minute) { return new Clock(hour, minute); } What would happen? • Consider the effects on the heap if the following sequence of statements was executed Clock c1 = new Clock(); Clock c2 = c1; c1.set(4, 30); c2.set(5, 40); Heap After Assigning c1 to c2 Class Association • Used to achieve certain desired behavior • Association (or acquaintance) – classes and objects are linked in a manner that allows the object to remain visible to clients – classes can be linked when a client passes an object as a message argument to another object Object Containing Reference to Another Object UML Class Diagram for Clock-DrawingBox Association UML Class Diagram for Clock-DrawingBox Association Class Containment • Composition (or aggregation) – an object of one class is constructed from objects of other classes so that only the composite object is visible to clients – the component objects are internal to the containing object • Aggregation is a weaker form of containment and will not be used in this text UML Class Diagram for Clock-DrawingBox Composition Representation Independence • In OOP the representation of data is encapsulated, this means that the data representation may be changed without affecting the previous class behavior • Clients should not be able to detect that a change has occurred • Sometimes this is know as implementation independence main • Every Java program needs a class containing a static method called main • Static methods (or class methods) are special methods that not require receivers • Static methods cannot refer to class instance variables, but can be invoked when no class instances exist • Static methods will be discussed in more detail in Chapter 10 ... class type • To create instances of the class using constructors • To send messages to instances of the class by invoking class instance methods • To know the class public interface – instance method... objects can be derived using a mechanism known as inheritance • Are a type container that is stored on the system heap • A client is program that uses an object Client Rights • To declare... interface and how it responds to each client message • Constructors definitions – describe how to initialize instance variables in a new object Constructors • Look like regular instance methods