Review for exam - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Review for exam

Description:

Review for exam #2. COSC 101. Classes VS Objects. Classes are the blueprints ... Encapsulate the state of an object. bank account's balance. miles on a car's odometer ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 17
Provided by: jaimes1
Category:
Tags: exam | review

less

Transcript and Presenter's Notes

Title: Review for exam


1
Review for exam 2
  • COSC 101

2
Classes VS Objects
  • Classes are the blueprints
  • Objects are the houses
  • Objects are also called instances

3
Instance variables
  • Also called instance fields
  • Encapsulate the state of an object
  • bank accounts balance
  • miles on a cars odometer
  • etc

4
static variables
  • also called static fields
  • Only one copy shared everywhere in the program
  • Good for constant values
  • suit or rank of a card

5
Rules for fields
  • All fields in a class are instance
  • unless specifically declared static

6
Methods
  • You absolutely must know the difference between
    defining a method and using a method

7
3 varieties of methods
  • Constructor
  • instance methods
  • static methods

8
Constructor
  • Special instance method that creates a new
    object/instance
  • also has a method body that executes after the
    new object is created
  • invoked using the Java keyword new

9
instance method
  • Can manipulate instance variables
  • Must be invoked on an instance
  • Can invoke other instance methods inside the same
    class
  • Can create instances of other classes using the
    constructor

10
static methods
  • stateless
  • cannot access instance variables at all
  • most useful for functions that compute their
    result only using their parameters
  • Math.pow(base, exp)
  • Math.max(x, y)

11
Defining methods
  • All methods are instance
  • unless declared static

12
To define a method
  • signature
  • visibility
  • return type
  • method name
  • parameters
  • Then provide a method body

13
Exceptions
  • Constructors NEVER list a return type
  • Constructors NEVER return values

14
Invoking constructors
  • new Point(6, 19)
  • returns a reference to a new instance of a point

15
instance methods
  • Must be invoked on an instance
  • b1.deposit(65.50)
  • pixel.setRed(200)
  • point.moveX(10)
  • Invoking one instance method from another
    instance method that is defined in the same class
    can be done using this
  • this.deposit(50.00)
  • or not
  • deposit(50.00)

16
invoking static methods
  • Invoked on the name of the class
  • Math.max(10, x)
  • Can be invoked without the classname from inside
    the class where its defined
Write a Comment
User Comments (0)
About PowerShow.com