Objects and Classes, Lecture 2 Definition - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Objects and Classes, Lecture 2 Definition

Description:

Objects and Classes, Lecture 2 Definition an object is a combination of some data (attributes) and some actions (methods) . Hopefully the data and methods are ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 10
Provided by: RodFa3
Category:

less

Transcript and Presenter's Notes

Title: Objects and Classes, Lecture 2 Definition


1
Objects and Classes, Lecture 2Definition
  • an object is a combination of some data
    (attributes) and some actions (methods).
  • Hopefully the data and methods are closely
    related NOT randomly thrown together
  • Creating Software Objects in Java

2
Example of an object
  • A Ball Could have
  • Attributes
    xPosition, yPosition

  • diameter, colour

  • graphics context
  • Constructor
  • Methods display
  • changeXPosition, changeYPosition
  • moveXPosition, moveXPosition,
  • slowMoveXPosition,
  • slowMoveYPosition

(xPosition, yPosition)
diameter
3
Classes
  • Java provides many of its own classes.BUT for
    something like a Ball, a Triangle etc, the
    programmer must define what a ball or triangle
    is ( ie what it looks like)
  • A class is like a template and an instance is
    an object constructed from this template.
  • Constructor a special method used to create or
    construct an object, most often used to
    initialize variables So whats so special
    about it Same name as class, never returns a
    value.

4
  • //shortened version of class
  • public class Ball
  • //attributes
  • private int xPosition
  • private int yPosition
  • private int diameter
  • private Color col
  • private Graphics g
  • //constructor
  • public Ball(int xPosIn, int yPosIn, int diamIn,
  • Color colIn, Graphics gIn)
  • //public method headers or signatures
  • public void display()
  • public void changeXPosition(int xPosIn)
  • public void changeYPosition(int yPosIn)
  • public void moveXPosition(int xMove)
  • public void moveYPosition(int yMove)

5
public or private
  • If private, the programmer has control,
  • that is no-one outside the class can see, ie
    directly change the variables.
  • It is usual to make attributes private and write
    public methods to enable necessary changes
  • This is Encapsulation or information hiding

6
  • The constructor methods are made public so they
    can be seen, ie used from a different class, any
    private methods cannot be used from a different
    class.
  • pinkBall new Ball(50,100,20,Color.pink,g)

diameter
xPosition
yPosition
colour
Graphics object
7
Creating Multiple Instances?
  • // declare
  • Ball orangeBall, pinkBall
  • //construct
  • orangeBall new
  • Ball(20,40,30,Color.orange,g)
  • pinkBall new
  • Ball(50,100,20,Color.pink,g)
  • //as many balls as you want/need all instances
    or objects

8
  • A second class CreateTwoBalls which extends
    Applet, is used to construct, display and
    manipulate the Ball objects by using their
    methods
  • pinkBall.moveXPosition(50)
  • pinkBall.moveYPosition(40)
  • pinkBall.slowMoveXPosition(80)

9
Class CreateTwoBalls (three main objects)
  • 1. User interface object created from the class
    CreateTwoBalls . This is created by the browser
    or AppletViewer.
  • 2. Ball objects, orangeBall and pinkBall, created
    from the class Ball. These are created by the
    user interface object of CreateTwoBalls .
Write a Comment
User Comments (0)
About PowerShow.com