160 likes | 261 Vues
Learn about constructors, overloading, and decomposition in Java programming. Understand object relationships and the importance of method decomposition for code clarity. Get ready for your midterm exam by reviewing key concepts.
E N D
COMP 14: decomposition, overloading, relationships June 7, 2000 Nick Vallidis
Announcements • Midterm exam is on Friday • Review tomorrow
Homework • read 4.1-4.6 • assignment P4 is due next Tuesday! (doing it is a good way to study for the exam…) • e-mail me any questions you'd like me to answer in the review tomorrow (do this before tomorrow morning)
Review • What is encapsulation? • What is an abstraction? • What are the visibility modifiers and what do they do? • How is a method declared?
Today • Constructors • Object relationships • Method overloading • Method Decomposition
Constructors • A constructor is a special method that is used to set up a newly created object • The programmer does not have to define a constructor for a class • There is a "default constructor" automatically created for you that does nothing.
Constructors • When writing a constructor, remember that: • it has the same name as the class • it does not return a value • it has no return type, not even void • it often sets the initial values of instance variables
Object relationships • An aggregate object is an object that contains references to other objects • An Account object is an aggregate object because it contains a reference to a String object (p. 189) • An aggregate object represents a has-a relationship • A bank account has a name
Object relationships • Sometimes an object has to interact with other objects of the same type • For example, we might concatenate two String objects together as follows s3 = s1.concat(s2); • One object (s1) is executing the method and another (s2) is passed as a parameter
Method overloading • Method overloading is the process of using the same method name for multiple methods • The signature of each overloaded method must be unique • The signature includes the number, type, and order of the parameters
Method overloading • The compiler must be able to determine which version of the method is being invoked by analyzing the parameters • The return type of the method is not part of the signature
Version 1 Version 2 float tryMe (int x, float y) { return x*y; } float tryMe (int x) { return x + .375; } Invocation result = tryMe (25, 4.32) Method overloading
Overloaded methods • The println method is overloaded: println (String s) println (int i) println (double d) • The following lines invoke different versions of the println method: System.out.println ("The total is:"); System.out.println (total);
Overloading constructors • Constructors can be overloaded • An overloaded constructor provides multiple ways to set up a new object
Method decomposition • A method should be relatively small, so that it can be readily understood as a single entity • A potentially large method should be decomposed into several smaller methods as needed for clarity • Therefore, a service method of an object may call one or more support methods to accomplish its goal
Guideline for decomp. • A general rule is to make your methods small enough so that they can fit on one screen • Definitely ok to be shorter • can be longer if decomposing it introduces unnecessary complexity