1.1 Classes and Objects in Java

Embed Size (px)

Citation preview

  • 8/3/2019 1.1 Classes and Objects in Java

    1/15

    6-Feb-12

    Classes and Objects in Java

  • 8/3/2019 1.1 Classes and Objects in Java

    2/15

    2

    Classes and Objects

    A Java program consists of one or more classesA class is an abstract description of objectsHere is an example class:

    class Dog { ...description of a dog goes here... }

    Here are some objects of that class:

  • 8/3/2019 1.1 Classes and Objects in Java

    3/15

    3

    M ore Objects

    Here is another example of a class:class Window { ... }

    Here are some examples of Windows:

  • 8/3/2019 1.1 Classes and Objects in Java

    4/15

    4

    Classes contain data definitions

    Classes describe the data held by each of its objectsExample:

    class Dog {

    String name;int age;...rest of the class...

    }

    Data usually goes first in a class

    A class may describe any number of objectsExamples: "Fido ", 3; "Rover ", 5; "Spot ", 3;A class may describe a single object, or even no objects at all

  • 8/3/2019 1.1 Classes and Objects in Java

    5/15

    5

    Classes contain methods

    A class may contain methods that describe the behavior of objectsExample:

    class Dog {...

    void bark() {System.out.println( "Woof! ");

    }}

    Methods usually go after the data

    When we ask a particular Dog to bark, it says Woof!Only Dog objects can bark; the class Dog cannot bark

  • 8/3/2019 1.1 Classes and Objects in Java

    6/15

    6

    M ethods contain statements

    A statement causes the object to do something(A better word would be commandbut it isnt)

    Example:

    System.out.println( "Woof! ");This causes the particular Dog to print (actually, display onthe screen) the characters Woof!

  • 8/3/2019 1.1 Classes and Objects in Java

    7/15

    7

    M ethods may contain temporary data

    Data described in a class exists in all objects of thatclass

    Example: Every Dog has its own name and age

    A method may contain local temporary data that existsonly until the method finishesExample:

    void wakeTheNeighbors( ) {int i = 50; // i is a temporary variablewhile (i > 0) {

    bark( );i = i 1;

    }}

  • 8/3/2019 1.1 Classes and Objects in Java

    8/15

    8

    Classes always contain constructors

    A constructor is a piece of code that constructs, or creates, anew object of that classIf you dont write a constructor, Java defines one for you (behindthe scenes)You can write your own constructorsExample:

    class Dog {String name;

    int age;Dog(String n , int age) {name = n;this.age = age;

    }}

    (This part is the constructor)

  • 8/3/2019 1.1 Classes and Objects in Java

    9/15

    9

    D iagram of program structure

    A program consists of one or more classesTypically, each class isin a separate .java file

    Program

    File File

    File

    FileClass

    Variables

    Constructors

    Methods

    Variables

    Variables

    Statements

    Statements

  • 8/3/2019 1.1 Classes and Objects in Java

    10/15

    10

    Summary

    A program consists of one or more classesA class is a description of a kind of object

    In most cases, it is the objects that do the actual work

    A class describes data, constructors, and methodsAn objects data is information about that objectAn objects methods describe how the object behavesA constructor is used to create objects of the class

    Methods (and constructors) may contain temporary dataand statements (commands)

  • 8/3/2019 1.1 Classes and Objects in Java

    11/15

    11

    Writing and running programs

    When you w rite a program, you are writing classes and all thethings that go into classesYour program typically contains commands to create objects (thatis, calls to constructors)

    Analogy: A class is like a cookie cutter, objects are like cookies.

    When you run a program, it creates objects, and those objectsinteract with one another and do whatever they do to causesomething to happen

    Analogy: W riting a program is like writing the rules to a game; running a program is like actually playing the game

    You never know how well the rules are going to work until youtry them out

  • 8/3/2019 1.1 Classes and Objects in Java

    12/15

    12

    Getting started

    Question: Where do objects come from?Answer: They are created by other objects.

    Question: Where does the first object come from?Answer: Programs have a special main method, not part of any object, thatis executed in order to get things started

    public static void main (String[ ] args) {Dog fido = new Dog( "Fido ", 5); // creates a Dog

    }The special keyword static says that the main method belongs to

    the class itself , not to objects of the classHence, the main method can be called before any objects are createdUsually, the main method gets things started by creating one or moreobjects and telling them what to do

  • 8/3/2019 1.1 Classes and Objects in Java

    13/15

    1 3

    A bad programpublic 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! ");

    }}

    non-static method bark() cannot be referenced from a static context

    --------- new Dog( "Fido ", 5).bark();

  • 8/3/2019 1.1 Classes and Objects in Java

    14/15

    1 4

    A complete programclass 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 > 0) {

    bark( );i = i 1;

    }}

    public static void main(String[ ] args) {Dog fido = new Dog( "Fido ", 5);fido.wakeTheNeighbors();

    }

    } // ends the class

  • 8/3/2019 1.1 Classes and Objects in Java

    15/15

    1 5

    The End

    I invented the term Object-Oriented, and I can

    tell you I did not have C++ in mind.--A lan Kay, creator of Smalltalk.