Docsity

Libraries, Encapsulation, Packages and This - Object Oriented Program | CMSC 131, Study notes of Computer Science

Toggle sidebar
bg1
1
CMSC 131 Fall 2007
Jan Plane (adapted from Bonnie Dorr)
Lecture Set #7:
Libraries, Encapsulaton,
Packages and “this”
1.
Review of Parameter passing
2.
Libraries
3.
Public vs. private
4.
Package Management
5.
this
CMSC 131 Fall 2007
Jan Plane (adapted from Bonnie Dorr)
1
Parameters and Methods
Recall that methods / constructors can have parameters
public Student (String newName, int IDDesired) {
name = newName;
id = IDDesired;
tokenLevel = 3;
}
What is printed by the following?
String newName = “Joe”;
Student s = new Student(newName + “ Schmoe”, 123456789);
System.out.println (s.name);
System.out.println (newName);
Joe Schmoe
Joe
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Libraries, Encapsulation, Packages and This - Object Oriented Program | CMSC 131 and more Study notes Computer Science in PDF only on Docsity!

CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)

Lecture Set #7:

Libraries, Encapsulaton,

Packages and “this”

1. Review of Parameter passing

2. Libraries

3. Public vs. private

4. Package Management

5. this

CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)

1

Parameters and Methods

 Recall that methods / constructors can have parameters public Student (String newName, int IDDesired) { name = newName; id = IDDesired; tokenLevel = 3; }  What is printed by the following? String newName = “Joe”; Student s = new Student(newName + “ Schmoe”, 123456789); System.out.println (s.name); System.out.println (newName);  Joe Schmoe Joe

CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)

2

How Does Java Evaluate

Method / Constructor Calls?

int newName = “Joe”;

Student s = new Student

(newName + “ Schmoe”, 123456789);

1. Arguments are evaluated using stack in effect at call

site (place where method called)

 newName + “ Schmoe”, evaluates to Joe Schmoe

 123456789 evaluates to 123456789

2. Stack frame (temporary addition to stack) created to

associate method parameters with values

3. Stack frame put into stack

4. Body of method executed in modified stack

5. Stack frame removed from stack

CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)

3

Libraries in Java

 Library: implementation of useful routines that are shared by different programs  Java mechanism for creating libraries: packages

 Package: group of related clases  Example: java.util (contains Scanner class)

 To use a class from a package, you can use a fully qualified name (package name + class name): java.util.Scanner s = new java.util.Scanner(System.in);

 You can also import the class in the beginning of the file

import java.util.Scanner;

 To import class in a package: import java.util.*; (Imports Scanner as well as other classes in package)

CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)

6

String API & Math API

 String implements lots of string functions

 StringExample.java

 Math implements lots of mathematical

functions

 MathExample.java

CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)

7

Public Declarations

 So far all classes / variables / methods have

been public

 Keyword public used in declaration

 Every user of an object can access any public

element

 Sometimes access should be restricted!

 To avoid giving object users unnecessary info

(keep API small)

 To enforce consistency on instance variables

CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)

8

Private Declarations

 Java also allows variables / methods to be

declared private:

private int tokenLevel = 3;

 Private variables / members cannot be

accessed outside the class definition

 Declaring instance variables private means

they can only be modified using public

methods

CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)

9

What Should Be Public /

Private?

 Class interface = API = public variables / methods

 Only make something public if there is a reason to

 Why? Encapsulation

 As long as interface is preserved, class can change without breaking other code  The more limited the interface, the less there is to maintain

 Rule of thumb

 Make instance variables private  Implement set / get methods

 Make auxiliary methods private

CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)

12

Comments (cont.)

 Some programmers hate them …

 … but they are essential for code understanding (it

is called “code” for a reason)

 Beginning with Project #3, you will be graded on the

content of your comments as well as other

readability factors

 Comment:

 Every variable (what is variable for?)  Every method (what does method do?)  Every class (what is class for? who wrote it?)

CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)

13

Testing

 insert set #

CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)

14

Overloading Methods

 Two methods with the same name but

distinguishable parameter types

 Terminology:

 prototype: public static void f(int x, float y)  signature: f(int , float ) // names of variables and the return type are not part of it

 Can’t differ only in return type

 Can’t differ only in types that can be implicitly

promoted between (widening conversion) when

choices are equal in amount of conversion

CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)

15

Implicit Promotions

 On general assignments

 On parameters

 On return value