Docsity

Objectives - Introduction to Programming in Java - Lecture Slides, Slides of Network security

Partial preview of the text

Download Objectives - Introduction to Programming in Java - Lecture Slides and more Slides Network security in PDF only on Docsity!

  • Objectives
    • explain classes and objects
      • introduce fields, constructors, and methods
    • go through two examples

3. Classes and Objects

Topics

1. What are Classes, Objects?

2. The Contents of a Class

3. A Stack Class

4. Creating a Stack Object

5. Understanding a Stack Object

6. A Bad Stack Interface

7. Good vs. Bad Interfaces

8. Creating Two Stack Objects

9. Object Types vs. Primitive Types

10. A Ticket Machine

  • A class consists of data (fields) and methods.
  • Each object gets a copy of the class's data

(fields).

  • An object uses the methods stored in the class
    • the class is a kind of method library for all of its

objects

Very Important Slide

A Class Involves at least 2 People

  • A class is implemented by one person.
  • A class is used by other, different people.
  • The implementor wants to make a class that is

easy to use.

  • A user does not care how a class is

implemented. He only wants to know the

operations that it can carry out (its interface ).

2. The Contents of a Class

public class ClassName { Fields // variables used by all methods

Constructor(s) // method(s) that initialize an object

Methods (functions) }

3 main parts

of a class

Fields

  • Fields are the variables

(data) used by an

object.

  • Also known as

instance variables.

public class Stack { private int store[]; private int max_len; private int top;

// all methods can use fields }

private int top;

visibility type^ variable name

Methods

• Methods are functions very like C/C++

functions/methods.

• The methods that are visible to the user

(public) in a class depend on the interface of

the thing being implemented

– e.g. the stack interface → public methods

that a user can call

3. A Stack Class

// Stack.java // Written by the Stack implementor

public class Stack { private int store[]; // hidden data private int max_len; private int top;

public Stack(int size) // visible method { store = new int[size]; max_len = size-1; top = -1; } :

continued Docsity.com

public boolean pop() { if (top == -1) return false; top--; return true; }

public int topOf() { return store[top]; }

public boolean isEmpty() { return (top == -1); }

} // end of Stack.java

Stack Class Diagram

'-' means private

'+' mean public

class name

fields

methods

constructor

Compilation and Execution

$ javac Stack.java

$ javac TestStack.java

$ java TestStack Top value is 42

Stack.class is in

the same directory as

TestStack.class.

Notes

• The user cannot directly access the

object's data (fields) because it is private.

• The user can call methods because they

are public

– the methods are the object's user interface

continued Docsity.com

Stack Object Diagram

Just after object creation:

The stack object has a copy of the class' fields (data),

but uses the methods in the 'class library'.

private means

the user of

the object

cannot directly

store[].... access the fields.

max_len 9

top -

stk

  • Meaning of a method call:
    • stk1.push(17) means call the push() method stored in the Stack class, but apply it to the stk object
  • Methods work on an object's fields even

though the methods are stored in the class.