A simple object oriented program
1 / 18

A Simple Object Oriented Program - PowerPoint PPT Presentation

  • Uploaded on
  • Presentation posted in: General

A Simple Object Oriented Program. public class Simple { public static void main (String [] args) { System.out.println(“howdy”); } } System.out is an object of class PrintStream, provided by the Java library

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

Download Presentation

A Simple Object Oriented Program

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 Simple Object Oriented Program

public class Simple {

public static void main (String [] args) {




System.out is an object of class PrintStream, provided by the Java library

println is a method of the PrintStream class

Objects and Classes

  • Object: entity that contains data and can performs actions in a program (by invoking methods)

  • Class: describes a type of object, a class must be defined in order to ‘create’ an object

  • So, each object belongs to a class

  • Class determines legal methods for an objectSystem.out.fill(); // Error, no fill methodSystem.out.println(“howdy”); // OK

Creating Objects

The System.out object has already been created, and already exists for your use in a Java program.

Typically, you must create an object before you can use it. In order to create an object, a class must exist which ‘describes’ this type of object . To use this class, you must be familiar with the creation methods (constructors) it provides.

A constructor method always has the same name as the class.

One class which already exists for your use in the Java library is the Rectangle class. Let’s create a Rectangle object………

Two of the constructors available with the Rectangle class:


Rectangle(int x, int y, int width, int height)

www.java.sun.com provides class specs (see API specs)

Declare a rectangle reference variable:

Rectangle myrec;

Construct a rectangle:myrec = new Rectangle(5, 10, 20, 30);

Adjust the size of this Rectangle object by

calling the setsize method of the Rectangle class:


  • Print the Rectangle object

  • System.out.println(myrec); printsjava.awt.Rectangle[x=5,y=10,width=45,height=15]

Attributes of a Rectangle Object

Syntax: Variable Definition

TypeName variableName;

TypeName variableName = expression;

  • Example:

    Rectangle myrec; Rectangle box = new Rectangle();

  • Purpose:

    To define a new variable of a particular type

    and optionally supply an initial value

Syntax: Object Construction

  • new ClassName(parameters)

    • Example:

  • new Rectangle(5, 10, 20, 30)

  • new Car(“red”, “Ford”,”Pinto”, 4.5)

    • Purpose:

  • To construct a new object, initialize it with the construction parameters, and return a reference to the constructed object.

  • Object Variables


    Rectangle crispyCrunchy;

    Declare and initialize:

    Rectangle cerealBox = new Rectangle(5, 10, 20, 30);

    • Once you have an object, you can apply methods:

      cerealBox.translate(15, 25);

      crispyCrunchy.translate(10,5); //error !!

    • Share objects:r = cerealBox;

    Uninitialized and Initialized Variables

    Rectangle cerealBox;

    Rectangle cerealBox = new Rectangle(5,10,20,30);

    These variables are references (like pointers) to objects, SO:

    Rectangle r;

    r = cerealBox;

    is NOT the same as:

    r = new Rectangle(5,10,20,30);

    In the first case, two variables will refer to the SAME object, in the second, an additional object is created and referred to by crispCrunchy.

    Two Object Variables Referring to the Same Object

    Writing a Test Program

    • Invent a new class, say MoveTest

    • Supply a main method (which makes this an application class)

    • Place instructions inside the main method

    • Import any library classes you need by specifying the package and class name:import java.awt.Rectangle;

    • You don't need to import classes in the java.lang package such as String and System

    Syntax : Importing a Class from a Package

    • import packageName.ClassName ;

      • Example:

  • import java.awt.Rectangle;

    • Purpose:

  • To import a class from a package for use in a program

  • File MoveTest.java

    import java.awt.Rectangle;

    public class MoveTest {

    public static void main(String[] args){

    // declare and create the rectangle

    Rectangle cerealBox = new


    // move the rectangle

    cerealBox.translate(15, 25);

    // print the moved rectangle




    Using Class Methods

    In order to call a class method successfully, you must examine it’s signature:

    // the following bold signature indicates how to use this

    // method of the Rectangle class

    public boolean contains (int x, int y) {

    //method body


    Using Class Methods

    • access specifier (such as public)

    • return type (such as String or void)

    • method name (such as contains or


    • list of parameters (number, type and purpose of

      parameters needed in call)

    public boolean contains (int x, int y) {

    //method body


    • This method is ‘callable’, because its access specifier is public

    • This method will return a true or false result, so the call should occur within an expression

    • The method is called using the name ‘contains’

    • the call must provide 2 arguments

      Call: myrec.contains(6,7)

  • Login