Transcript
Page 1: 15-Nov-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is the description; while objects

Apr 21, 2023

Classes and Objects in Java

Page 2: 15-Nov-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is the description; while objects

2

Classes and Objects

A Java program consists of one or more classes A class is the description; while objects are

actual examples Here is an example class:

class Dog { ...description of a dog goes here... } Here are some objects of that class:

Page 3: 15-Nov-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is the description; while objects

3

More Objects Here is another example of a class:

class Window { ... } Here are some examples (objects) of Windows:

Page 4: 15-Nov-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is the description; while objects

4

Classes are like blueprintsWhile the house made from the blueprint is the object

Page 5: 15-Nov-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is the description; while objects

5

Classes contain data definitions

Classes describe the data held by each of its objects Example:

class Dog { String name; int age; ...rest of the class...}

Data usually goes first in a class

Page 6: 15-Nov-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is the description; while objects

6

Classes contain methods

A class may contain methods that describe the behavior of objects Example:

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

Page 7: 15-Nov-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is the description; while objects

7

Methods contain statements

A statement causes the object to do something (A better word would be “command”—but it isn’t)

Example: System.out.println("Woof!"); This causes the particular Dog to “print” (actually, display on

the screen) the characters Woof!

Page 8: 15-Nov-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is the description; while objects

8

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 > 0) { bark( ); i = i – 1; }}

Page 9: 15-Nov-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is the description; while objects

9

Classes always contain constructors

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)

The constructor always has the same name as your class. Example:

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

(This part is the constructor)

Page 10: 15-Nov-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is the description; while objects

10

Constructors You can have multiple constructors Example:

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

{ name = n; this.age = age; }

Dog(int age) {

name = “UNKNOWN”; this.age = age; }

Dog() {

name = “UNKNOWN”; age = 0; }

}

There are 3 constructors:

It will construct an object based on the # of arguments.

Page 11: 15-Nov-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is the description; while objects

11

Diagram of program structure

A program consists of one or more classes

Typically, each class is in a separate .java file

Program

File

File

File

FileClas

sVariablesConstructors

Methods

Variables

Variables

Statements

Statements

Page 12: 15-Nov-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is the description; while objects

12

Summary

A program consists of one or more classes A 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 methods

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)

Page 13: 15-Nov-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is the description; while objects

13

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, that is executed in order to get things started public static void main(String[ ] args) {

Dog fido = new Dog("Fido", 5); // creates a Dog}

Page 14: 15-Nov-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is the description; while objects

14

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!"); }

}

public class Kennel{ public static void main(String[ ]

args) {

Dog fido = new Dog("Fido", 5); fido.bark(); }

} // ends the class


Top Related