Download presentation
Presentation is loading. Please wait.
1
COMP 14: decomposition, overloading, relationships June 7, 2000 Nick Vallidis
2
Announcements zMidterm exam is on Friday zReview tomorrow
3
Homework zread 4.1-4.6 zassignment P4 is due next Tuesday! (doing it is a good way to study for the exam…) ze-mail me any questions you'd like me to answer in the review tomorrow (do this before tomorrow morning)
4
Review zWhat is encapsulation? zWhat is an abstraction? zWhat are the visibility modifiers and what do they do? zHow is a method declared?
5
Today zConstructors zObject relationships zMethod overloading zMethod Decomposition
6
Constructors zA constructor is a special method that is used to set up a newly created object zThe programmer does not have to define a constructor for a class yThere is a "default constructor" automatically created for you that does nothing.
7
Constructors zWhen writing a constructor, remember that: yit has the same name as the class yit does not return a value it has no return type, not even void yit often sets the initial values of instance variables
8
Object relationships zAn 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) zAn aggregate object represents a has-a relationship zA bank account has a name
9
Object relationships zSometimes 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
10
Method overloading zMethod overloading is the process of using the same method name for multiple methods zThe signature of each overloaded method must be unique zThe signature includes the number, type, and order of the parameters
11
Method overloading zThe compiler must be able to determine which version of the method is being invoked by analyzing the parameters zThe return type of the method is not part of the signature
12
Method overloading float tryMe (int x) { return x +.375; } Version 1 float tryMe (int x, float y) { return x*y; } Version 2 result = tryMe (25, 4.32)Invocation
13
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);
14
Overloading constructors zConstructors can be overloaded zAn overloaded constructor provides multiple ways to set up a new object
15
Method decomposition zA method should be relatively small, so that it can be readily understood as a single entity zA potentially large method should be decomposed into several smaller methods as needed for clarity zTherefore, a service method of an object may call one or more support methods to accomplish its goal
16
Guideline for decomp. zA general rule is to make your methods small enough so that they can fit on one screen zDefinitely ok to be shorter zcan be longer if decomposing it introduces unnecessary complexity
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.