Classes and Objects in Java Classes and Objects

  • Slides: 14
Download presentation
Classes and Objects in Java

Classes and Objects in Java

Classes and Objects n n n A Java program consists of one or more

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

More Objects n Here is another example of a class: n n class Window

More Objects n Here is another example of a class: n n class Window {. . . } Here are some examples of Windows: 3

Classes contain data definitions n n Classes describe the data held by each of

Classes contain data definitions n n Classes describe the data held by each of its objects Example: Data usually goes first in a class n n A class may describe any number of objects n n class Dog { String name; int age; . . . rest of the class. . . } Examples: "Fido", 3; "Rover", 5; "Spot", 3; A class may describe a single object, or even no objects at all 4

Classes contain methods n n A class may contain methods that describe the behavior

Classes contain methods n n A class may contain methods that describe the behavior of objects Example: Methods usually go after the data n class Dog {. . . void bark() { System. out. println("Woof!"); } } n n When we ask a particular Dog to bark, it says “Woof!” Only Dog objects can bark; the class Dog cannot bark 5

Methods contain statements n A statement causes the object to do something n n

Methods contain statements n A statement causes the object to do something n n (A better word would be “command”—but it isn’t) Example: n n System. out. println("Woof!"); This causes the particular Dog to “print” (actually, display on the screen) the characters Woof! 6

Methods may contain temporary data n Data described in a class exists in all

Methods may contain temporary data n Data described in a class exists in all objects of that class n n n Example: Every Dog has its own name and age A method may contain local temporary data that exists only until the method finishes Example: n void wake. The. Neighbors( ) { int i = 50; // i is a temporary variable while (i > 0) { bark( ); i = i – 1; } } 7

Classes always contain constructors n n A constructor is a piece of code that

Classes always contain constructors n n A constructor is a piece of code that “constructs, ” or creates, a new object of that class If you don’t write a constructor, Java defines one for you (behind the scenes) You can write your own constructors Example: (This part is the constructor) n class Dog { String name; int age; Dog(String n, int age) { name = n; this. age = age; } } 8

Diagram of program structure Program File Class File Variables Constructors Methods Variables File n

Diagram of program structure Program File Class File Variables Constructors Methods Variables File n n A program consists of one or more classes Typically, each class is in a separate. java file 9

Summary n n A program consists of one or more classes A class is

Summary n n A program consists of one or more classes A class is a description of a kind of object n n A class describes data, constructors, and methods n n In most cases, it is the objects that do the actual work An object’s data is information about that object An object’s methods describe how the object behaves A constructor is used to create objects of the class Methods (and constructors) may contain temporary data and statements (commands) 10

Writing and running programs n n When you write a program, you are writing

Writing and running programs n n When you write a program, you are writing classes and all the things that go into classes Your program typically contains commands to create objects (that is, “calls” to constructors) n n When you run a program, it creates objects, and those objects interact with one another and do whatever they do to cause something to happen n n Analogy: A class is like a cookie cutter, objects are like cookies. Analogy: Writing 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 you try them out 11

Getting started n Question: Where do objects come from? n n Question: Where does

Getting started n Question: Where do objects come from? n n Question: Where does the first object come from? n n Answer: They are created by other objects. Answer: Programs have a special main method, not part of any object, that is executed in order to get things started The main method is defined in a class, but it belongs to the class itself, not to any specific objects of that class The special keyword static says that the method belongs to the class, not to objects of the class public static void main(String[ ] args) { Dog fido = new Dog("Fido", 5); // creates a Dog } 12

A complete program class Dog { String name; int age; Dog(String n, int age)

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 wake. The. Neighbors( ) { int i = 50; while (i > 0) { bark( ); i = i – 1; } } public static void main(String[ ] args) { Dog fido = new Dog("Fido", 5); fido. wake. The. Neighbors(); } } // ends the class 13

The End 14

The End 14