Download presentation
Presentation is loading. Please wait.
Published byDamian Spencer Modified over 9 years ago
1
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold
2
Chapter Preview In this chapter we will: formally introduce the class construct as it is used in the Java language describe selected class components (instance variables, constructors, instance methods) demonstrate the use of classes to subdivide large programs discuss the concepts of mutability and visibility introduce the concept of operator overloading
3
Objects Composed of –values known as attributes or instance variables –operations known as instance methods that can be performed on these values Encapsulate data values within a single entity Their behavior is often generalized to allow reuse in other programs Inheritance is the OOP mechanism that allows one object to serve as a basis for another
4
Rights of a Client To declare variables of the class type To create instances of the class using class constructors To send messages to class instances by invoking the class instance methods To know the public interface of the class –names of instance methods –instance method parameters (number and types) –instance method return type To know which instance methods alter (or mutate) the class
5
Rights of a Class To define the public interface for the class To hide the class implementation details from the client To protect “internal” data from access by the client To change the implementation details at any time (provided that the public interface remains intact) To change the public interface with the concurrence of the client
6
Building Class Definitions public class name { declarations of instance variables constructor definitions method definitions }
7
Constructors public class Clock { int hour, minute; // constructor public Clock( ) { hour = 12; minute = 0; } // other methods follow … }
8
Using Constructors // c1 set to 12:00 Clock c1 = new Clock(); // c1 set to 8:30 c1.setHour(8); c1.setMinute(30); // c2 set to 12:00 c1 still 8:30 Clock c2 = new Clock();
9
Overloading Constructors Classes can have more than one constructor All constructors have the same name (the class name) Each constructor differs from the others in either the number or types of its arguments new is used when using a constructor to create a new object
10
Overloading Constructors We could add the following to Clock public Clock(int h, int m) { hour = h; minute = m; } A client program could contain Clock c1 = new Clock(8, 20); Which is the same as writing Clock c1 = new Clock( ); c1.setHour(8); c1.setMinute(20);
11
Overloading Methods Methods can also be overloaded This allows different versions of the method in the same class Each method variant must differ from the other by the number or types of its parameters For example OutputBox has several print methods, each taking a different type of parameter (int, double, String, boolean)
12
Methods Calling Methods Sometimes to allow transparent access of a new method by a client overloading can be used For example in Clock we might rewrite the original display method to call a different variant public void display (DrawingBOX d, int r) { display (d, d.getDrawableWidth()/2, d.getDRawableHeight()/2, r); }
13
this - Disambiguating Identifiers “this” can be used to distinguish between references to instance variables and local identifiers or arguments public void set (int hour, int minute) { int totalMinutes = (hour * 60 + minutes); this.minute = totalMinutes % 60; } this.minute refers to the instance variable minute not the method argument
14
this – Passing the Receiver as an Argument “this” can be used to send a message to the current receiver of the message without explicitly naming the receiver public boolean after (Clock c) { return c.priorTo(this); } this is used as if it were a variable referring to the receiver of the message
15
this – Changing Constructors “this” can be used to simplify constructor code by allowing one constructor to call another We can rewrite the clock constructors as: public Clock ( ) { this(12,0); } public Clock (int hour, int minute) { set(hour, minute); }
16
Visibility Qualifiers and Instance Variables By default class instance variables are accessible to some classes but not others Using visibility qualifiers in instance variable declarations allows you to enforce information hiding from the client Examples: public int x; // Any client using object o can // access x by writing o.x private int y; // No client can access y without // using the class methods
17
Visibility Qualifiers and Methods By default class methods are also accessible to some classes but not others Visibility qualifiers should also be used in method declarations Examples: public void f( ) { // Any client using object o // can send it a message // by writing o.f( ) private void g( ) { // No client can send g to // the object except another // method from this class
18
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
19
Mutation Object is changed by assigning new values to one or more the object’s instance variables Example: d = new DrawingBox(); c = new Clock(); c.set(10,20); c.display(d, 50, 50, 50); c.set(5, 40);
21
Non-mutation New object is created that is similar to old object, except for required changes The methods within the class must create new instances of the class Example: public Clock set_nonmut (int hr, int min) { return new Clock(hr, min); } Note: return type is “Clock” not “void”
23
Class Composition 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 Containment (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
24
Need figure 5.6 here
25
Need figure 5.7 here
26
Need figure 5.8 here
27
Need figure 5.9 here
28
main Special methods called static methods or class methods –have no receiver –are not permitted to refer to instance variables –can be invoked when no class instances exist “main” is a static method “main” is the first method invoked by the Java runtime environment when an application begins execution In many applications “main’s” first (and only) action is to create one or more objects and then invoke it’s methods
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.