Classes and Objects in Java - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Classes and Objects in Java

Description:

A Java program consists of one or more classes. A class is an abstract ... new Dog('Fido', 5).bark(); 7. classes-and-objects.ppt. A complete program. class Dog ... – PowerPoint PPT presentation

Number of Views:83
Avg rating:3.0/5.0
Slides: 21
Provided by: david1668
Category:
Tags: classes | fido | java | objects

less

Transcript and Presenter's Notes

Title: Classes and Objects in Java


1
Classes and Objects in Java
2
Classes and Objects
  • A Java program consists of one or more classes
  • A class is an abstract description of objects
  • Here is an example class
  • class Dog ...description of a dog goes here...
  • Here are some objects of that class

3
Methods may contain temporary data
  • Data described in a class exists in all objects
    of that class
  • Example Every Dog has its own name and age
  • A method may contain local temporary data that
    exists only until the method finishes
  • Example
  • void wakeTheNeighbors( ) int i 50
    // i is a temporary variable while (i gt 0)
    bark( ) i i 1

4
Classes always contain constructors
  • A constructor is a piece of code that
    constructs, or creates, a new object of that
    class
  • If you dont write a constructor, Java defines
    one for you (behind the scenes)
  • You can write your own constructors
  • Example
  • class Dog String name int age
    Dog(String n, int age) name n
    this.age age

5
Diagram of program structure
  • A program consists of one or more classes
  • Typically, each class is in a separate .java file

6
A bad program
  • public class Dog String name int age
    Dog(String name, int age)
    this.name name this.age age
    public static void main(String args)
    bark() void bark()
    System.out.println("Woof!")

--------- new Dog("Fido", 5).bark()
  • non-static method bark() cannot be referenced
    from a static context

7
A complete program
  • class Dog String name int age
    Dog(String n, int age) name n
    this.age age void bark()
    System.out.println("Woof!")
  • void wakeTheNeighbors( ) int i 50
    while (i gt 0) bark( ) i i
    1 public static void main(String
    args) Dog fido new Dog("Fido", 5)
    fido.wakeTheNeighbors()
  • // ends the class

8
Overview
  • In this presentation we will discuss
  • Classes and objects
  • Methods for objects
  • Printing results

9
Classes and objects
  • A class is the type of an object
  • int classSize
  • Color myColor
  • variable classSize has type int
  • variable myColor has type Color
  • classSize 5
  • myColor Color.RED
  • 5 is a literal value of type int
  • Color.RED is a literal value of type Color
  • There are exactly eight primitive types
  • There are thousands of classes, and you can
    create more

10
Declarations
  • You declare variables to hold primitive values
    like this
  • int classSize
  • double area
  • You declare variables to hold objects like this
  • Color uglyBrown
  • String myName

11
Assignment statements
  • An assignment statement has the form
  • variable expression
  • Examples
  • classSize 57
  • area pi radius radius
  • uglyBrown new Color(175, 175, 30)
  • myName "David Matuszek"

12
Methods
  • Primitives have operations, classes have methods
  • You cannot define new operations, but you can
    define new methods for a class

13
Data in classes and objects
  • A class is the type of an object
  • A class describes
  • How to make a new object of that class
  • Example new Color(175, 175, 30)
  • What kind of data is in an object
  • Example a Color object contains three numbers
    representing the amount of red, green, and blue
  • The methods of an object (the actions it can
    perform)
  • Example a Color object can tell you how much red
    it contains
  • int redAmount myColor.getRed()

14
Sending messages to objects
  • We dont perform operations on objects, we talk
    to them
  • This is called sending a message to the object
  • A message looks like this
  • object.method(extra information)
  • The object is the thing we are talking to
  • The method is a name of the action we want the
    object to take
  • The extra information is anything required by the
    method in order to do its job
  • Examples
  • g.setColor(Color.pink)
  • amountOfRed Color.pink.getRed( )

15
Messages to a Graphics
  • If you have a Graphics, and its name is g, here
    are some things you can do with g
  • Tell it to use a particular color
  • g.setColor(Color.ORANGE)
  • Ask it what color it is using
  • Color currentColor g.getColor()
  • Tell it to draw a line
  • g.drawLine(14, 23, 87, 5)

16
Messages to a Color
  • Once you make a Color, you cannot change it you
    can only ask it for information
  • // Make a new purplish colorColor myColor
    new Color(100, 0, 255)
  • // Ask how much blue is in itint amountOfBlue
    myColor.getBlue()
  • // Ask the color for a brighter version of
    itselfColor brightColor myColor.brighter()
  • The last method doesnt change the color it
    makes a new color

17
String
  • A String is an object, but...
  • ...because Strings are used so much, Java gives
    them some special syntax
  • There are String literals "This is a String"
  • There is an operation, concatenation, on Strings
  • Pat" Palmer" gives PatPalmer"
  • In other respects, Strings are just objects

18
String methods
  • If s is the name of the string "Hello", then
  • s.length() tells you the number of characters in
    String s (returns 5)
  • s.toUpperCase() returns the new String "HELLO"(s
    itself is not changed)
  • But you can say s s.toUpperCase()

19
New vocabulary
  • class the type, or description, of an object
  • object an instance, or member, of a class
  • message something that you say to a class,
    either telling it something or asking it for
    information
  • a parameter, or argument, used in calling a
    method of a class or object

20
The End
I invented the term Object-Oriented, and I can
tell you I did not have C in mind.
--Alan Kay, creator of
Smalltalk.
Write a Comment
User Comments (0)
About PowerShow.com