Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Objects and Classes. 3-2 Object Oriented Programming  An OO program models the application as a world of interacting objects.  A typical Java program.

Similar presentations


Presentation on theme: "Java Objects and Classes. 3-2 Object Oriented Programming  An OO program models the application as a world of interacting objects.  A typical Java program."— Presentation transcript:

1 Java Objects and Classes

2 3-2 Object Oriented Programming  An OO program models the application as a world of interacting objects.  A typical Java program creates many objects, which as you know, interact by invoking methods.  Through these object interactions, a program can carry out various tasks, such as implementing a GUI, running an animation, or sending and receiving information over a network.  Once an object has completed the work for which it was created, its resources are recycled for use by other objects.  An object can create other objects.  An object can call another object’s (and its own) methods  An object has data fields, which hold values that can change while the program is running.

3  A class is a piece of the program’s source code that describes a particular type of objects.  A class is a template or blueprint that describes the behavior/ state of the object.  An object is called an instance of a class. A program can create and use more than one object (instance) of the same class. 3-3

4  A blueprint for objects of a particular type  Defines the structure (number, types) of the attributes  Defines available behaviors of its objects 3-4 Attributes Behaviors

5 3-5 Attributes: String model Color color int numPassengers double amountOfGas Behaviors: Add/remove a passenger Get the tank filled Report when out of gas Attributes: model = "Mustang" color = Color.YELLOW numPassengers = 5 amountOfGas = 16.5 Behaviors:

6 3-6  A piece of the program’s source code  Written by a programmer  An entity in a running program  Created when the program is running (by the main method or a constructor or another method)

7 3-7  Specifies the structure (the number and types) of its objects’ attributes — the same for all of its objects  Specifies the possible behaviors of its objects  Holds specific values of attributes; these values can change while the program is running  Behaves appropriately when called upon

8 3-8  Each class is stored in a separate file  The name of the file must be the same as the name of the class, with the extension.java public class Car {... } Car.java By convention, the name of a class (and its source file) always starts with a capital letter. (In Java, all names are case-sensitive.)

9 3-9  Java programs are usually not written from scratch.  There are hundreds of library classes for all occasions.  Library classes are organized into packages. For example: java.util — miscellaneous utility classes java.awt — windowing and graphics toolkit javax.swing — GUI development package

10 Objects and Classes Programmers implement classes Classes are templates or blueprints for Objects Data and methods are defined within Classes Classes must provide an implementation such that objects created from those classes behave as those defined in the Object model. An Object is the instantiation of a class An object is an Instance of a class The process of creating an object is called instantiation The attributes of an object are called instance variables The methods of an object are called instance methods In Java, Objects are created using the new keyword: Employee e1 = new Employee();

11 Defining Classes A class definition must have the following: The keyword "class" followed by the name of the class The class body Before the keyword "class" is the optional modifier "public" If a class is public, it must be defined within a file which is the same name as the class with a ".java" extension. i.e. Classname.java eg. HelloWorld.java, Account.java, Ledger.java, Transaction.java most classes are declared public The class body contains: Zero or more instance variables Zero or more methods

12 Example Class Definition public class Employee { String name; String dept; int salary; Date startingDate; // more variable definitions... public int getSalary() //accessor method { return salary; } public int computeHourlyRate() //mutator method { // calculate hourly rate from salary } //more method definitions } in Employee.java: Instance Variables: Methods:

13 public class Bicycle { // the Bicycle class has three fields public int cadence; public int gear; public int speed; // the Bicycle class has one constructor public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; }

14 // the Bicycle class has four methods public void setCadence(int newValue) { cadence = newValue; } public void setGear(int newValue) { gear = newValue; } public void applyBrake(int decrement) { speed -= decrement; } public void speedUp(int increment) { speed += increment; } }


Download ppt "Java Objects and Classes. 3-2 Object Oriented Programming  An OO program models the application as a world of interacting objects.  A typical Java program."

Similar presentations


Ads by Google