slide1 l.
Skip this Video
Loading SlideShow in 5 Seconds..
A First Look at Objects PowerPoint Presentation
Download Presentation
A First Look at Objects

Loading in 2 Seconds...

play fullscreen
1 / 15

A First Look at Objects - PowerPoint PPT Presentation

  • Uploaded on

A First Look at Objects. Contents . How to use this lecture Primitive types in java Some terminology Classes and objects Accessing methods Packages Static methods and a first program. A First Look at Objects.

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

PowerPoint Slideshow about 'A First Look at Objects' - cana

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

A First Look at Objects


  • How to use this lecture
  • Primitive types in java
  • Some terminology
  • Classes and objects
  • Accessing methods
  • Packages
  • Static methods and a first program

A First Look at Objects

This lecture presents you with an overview of the object-based features of java. In java you will have to deal with objects and the terminology of object-oriented languages immediately. Initially you will be primarily using objects that are part of the standard java library. This overview will acquaint you with issues that you will encounter during the first few weeks of the course. It is best used to serve as a companion for the initial lectures and assignments. It is not easily digested during a first sitting, but by periodically reviewing this material during the initial weeks of the course you should gain a better appreciation of the nature of object-oriented programming in java.

An object-based programmer must be both a librarian and a logician.

A programmer must be able to locate, use, and extend classes contained in a rich collection of previously developed code.

A programmer must be able to write clear, concise, provably correct code of his or her own.


A First Look at Objects

Java is an object-based language. Everything in java is an object except for the primitive types:

entity primitive type


int or long or short


float or double



logical (true or false)


basic unit of memory


Primitive types designate the amount and organization of memory allocated by the system to standalone variables


A First Look at Objects

Primitive Data Types (platform independent):

Type Size Range Default Value


A First Look at Objects

The Terminology of Java

A Java program consists of runtime objects that interact with one another by exchanging messages.

A programmer builds (codes) a class which is a prototype for forming objects.

An object encapsulates data (primitive types and other objects) with the operations that are performed upon the data.


A First Look at Objects

Example -- Consider a Counter (like the dial in an odometer or digital clock)

A counter has two (integer) data members: the current count and the base of the counter – 10 for odometer dials, 24 or 60 for clock dials.

class Counter

Data members

Each Counter object keeps two pieces of data – its base, and its current count value

privateint base;

privateint count;

Data members of a class are usually declared as private or not accessible to other objects.

Operations on the data

Classes also provide public operations called methods that allow other objects to manipulate or view the data members of an object

public Counter (int baseVal);

publicvoid increment( );

publicvoid reset( )

publicint getCount( )


A constructor for creating Counter and initializing objects

Transformations do not return a value

Observations that return the value of a (usually private) data member.

The type of value returned is indicated in the method declaration

A First Look at Objects

Let’s briefly examine the operations for class Counter

There are 4 operations that one need perform on Counters:

There are 3 types of operations in this class

Create a new Counter object with a supplied base value.

public Counter (int baseVal);

publicvoid increment( );

publicvoid reset ( );

publicint viewCount( );

Increment the count by one up to the base value and then roll back to 0 again

Transformations that change the value of one or more of the data members

Reset the count to 0.

View the current value of the count (that is held in the private – inaccessible – variable count.








publicclass Counter {

privateint base;

privateint count;


public Counter(int baseVal)

{//body of the method}

publicvoid increment( ) {…}

publicvoid reset( ) {…}

publicint viewCount( ) {…}


Set by client


Default value

The object carries references to the (common) Counter methods

A First Look at Objects

Counter objects are constructed during run-time from the instructions specified in the class.

Construct a base-10 Counter called units


Each object holds its own unique data

Use the operation new to allocate memory for the object units.

Counter units = new Counter(10);


Name of recipient object

Name of method to execute

Dot operator separates the name of the object from the name of the method

A First Look at Objects

The operations are invoked by a requesting object sending a message to a receiving object (such as units) to execute the indicated operation.

Message is sent to the object units to execute its increment method.

units.increment( );

Message to units requests that it return its current count. The value that is returned is stored in a variable named theCount.

int theCount = units.viewCount( );


Allocates memory for a reference to a Counter

Uses the new operator and a constructor to allocate memory for a Counter object and initializes the base to 10






reset( )

viewCount( )



returns an integer that is printed to the screen

A First Look at Objects

Assume the class Counter that we have just described has been created and placed in a separate file in the same directory as this main program. Write a program to create a base 10 Counter, increment it twice, and display its count to the screen.

publicclass CounterExample {

publicstaticvoid main(String [ ] args) {

Counter baseTen;

baseTen = new Counter(10);

//increment twice

baseTen.increment( );

baseTen.increment( );

//display on screen

System.out.println(baseTen.viewCount( ));



A First Look at Objects


Related java classes may be grouped together into packages.

A program wishing to use one of these classes must supply the “fully qualified name”.


Note! Sometimes packages can also be grouped into larger packages.



A First Look at Objects

The Java Library

The classes used in a Java program come from two sources:

1. The classes created by the programmer

  • The classes contained in the standard java library (that comes
  • with the language)

The first set of programs that you will write will have a single class of your own construction that will have only a main function and will reference operations from classes contained in the standard library.


A First Look at Objects

Heavily used packages (and classes) in the java library

Packages used for interactive dialog


For creating dialog boxes

For accessing input stream classes


For formatting numerical output


For parsing strings to retrieve individual parts

To use classes contained in one of these packages in a program that you are writing, begin your program with an import directive that specifies the full name of the package you are using.

import javax.swing.*; //allows access to all classes in this package

import javax.swing.JOptionPane;

//as an alternative, you may import the single class that is needed


First Look at Objects

Static methods

Methods or data members that are qualified as having staticscope are class methods ( or data members). They can be invoked without instantiating (creating) an object of that class. Two primary examples of static methods that you will encounter very early in your programming efforts are the main function in the classes that you will write, and the functions in the library class Math.

Methods in class Math are all declared as static and accessed without creating an instance of a Math object. They are accessed by qualifying the name of the desired function with the class name Math.


Math.sqrt(real or integer variable name);

Math.abs(real or integer variable name);


A First Look at Objects

The function main( )

In java every function must belong to a class. Every program must have one and only one main function in one of its classes. The first several programs that you write will consist only of a main function, but that must be placed inside a class. This function must be declared static so that it will be run without creating an instance of the class in which it resides.

Construct a class with your own chosen name.

publicclass MyFirstProgram {

publicstaticvoid main(String [ ] args)


System.out.println(“My first program!!”);



Class contains a single function--main

Writes a string of text to the screen