comp 14 decomposition overloading relationships
Download
Skip this Video
Download Presentation
COMP 14: decomposition, overloading, relationships

Loading in 2 Seconds...

play fullscreen
1 / 16

COMP 14: decomposition, overloading, relationships - PowerPoint PPT Presentation


  • 73 Views
  • Uploaded on

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…)

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'COMP 14: decomposition, overloading, relationships' - nerita


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
announcements
Announcements
  • Midterm exam is on Friday
  • Review tomorrow
homework
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
Review
  • What is encapsulation?
  • What is an abstraction?
  • What are the visibility modifiers and what do they do?
  • How is a method declared?
today
Today
  • Constructors
  • Object relationships
  • Method overloading
  • Method Decomposition
constructors
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.
constructors1
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
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 relationships1
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
  • 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 overloading1
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
method overloading2
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
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
Overloading constructors
  • Constructors can be overloaded
  • An overloaded constructor provides multiple ways to set up a new object
method decomposition
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
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
ad