Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Objects and Classes.

Similar presentations


Presentation on theme: "Chapter 4 Objects and Classes."— Presentation transcript:

1 Chapter 4 Objects and Classes

2 Assignment 2 Solution In class exercise

3 Object Oriented Programming
OOP Programs made up of objects Functionality exposed to users Implementation hidden from users Objects can be used from library or custom created Using object and their methods can reduce program complexity

4 Classes The template or “blueprint” from which objects are made.
Construct an object from class means to create an “instance” of that class Encapsulation – aka info hiding. The data inside an object is known as its instance fields Function that operate on the data are called methods

5 Class cont’ Notion that classes are cookie cutters and objects are the cookies created from classes The user interacts with the methods of class – you should never deal with how the methods are implemented

6 Classes cont’ Java has the ability to “extend” a class.
A new class has all of the properties and methods of the class you extend You can write new methods and add properties that apply to your new class. This is also referred to as “inheritance”

7 Where to start?? The top down approach doesn’t apply
You don’t start with the void main(String args[]) When thinking about creating your own classes (use nouns for analyzing the problem space) Use verbs to describe methods on the nouns

8 Using Predefined Classes
Take, for example, the Math class. You have seen that you can use methods of the Math class, such as Math.random(), without needing to know how they are implemented— all you need to know is the name and parameters (if any). That is the point of encapsulation and will certainly be true of all classes. But the Math class only encapsulates functionality; it neither needs nor hides data. Because there is no data, you do not need to worry about making objects and initializing their instance fields—there aren’t any Arrays class is another example of this

9 Object Variables To work with objects, you first construct them and specify their initial state. Then you apply methods to the objects. You use constructors to construct new instances. A constructor is a special method whose purpose is to construct and initialize objects. Constructors always have the same name as the class name. To construct an object, you combine the constructor with the new operator. new Date(); There is an important difference between objects and object variables. Date deadline; // deadline doesn't refer to any object The above defines an object variable, deadline, that can refer to objects of type Date

10 Object Variables Cont’
The return value of the new operator is also a reference. A statement such as Date deadline = new Date(); Has two parts. The expression new Date() makes an object of type Date, and its value is a reference to that newly created object. That reference is then stored in the deadline variable.

11 Lets create an Object!!! Bicycle Class Nouns? Verbs? Bike components
Bike (behaviors)

12 Bicycle Class Bike properties State Methods Gear, speed, and cadence
Current gear, speed, and cadence Methods SpeedUp, ChangeGear, Brake, ChangeCadence

13 Bike Class Java Code public class Bicycle { //members (properties of Bike class) int cadence = 0; int speed = 0; int gear = 1;

14 Bike Class Java Code cont’
void changeCadence(int newValue) { cadence = newValue; } void changeGear(int newValue) { gear = newValue; void speedUp(int increment) { speed = speed + increment; void applyBrakes(int decrement) { speed = speed - decrement; void printStates() { System.out.println("cadence:"+cadence+" speed:” +speed+" gear:"+gear); } //end of class

15 Bike Class Java Code cont’
class BicycleDemo { public static void main(String[] args) { // Create Bicycle objects Bicycle bike1 = new Bicycle(); // Invoke methods on those objects bike1.changeCadence(50); bike1.speedUp(10); bike1.changeGear(2); bike1.printStates(); }//end main() }//end BicycleDemo

16 Extending Bicycle class MountainBike extends Bicycle { }
// new fields and methods defining a mountain bike would go here }

17 Relationships Between Classes

18 Constructors

19 Constructors Cont’

20 this

21 Accessor and Mutator and Private
Are types of functions used in your classes Accessor fucntions (get) Return values of instance fields (class properties) Mutator functions (set) Change the value of the instance fields (class properties The private keyword makes sure that the only methods that can access these instance fields No outside method can read or write to these fields.

22 Instance Fields

23 Final Instance Fields

24 Static Fields and Methods

25 Initializing Fields

26 Method Parameters

27 Packages Java allows you to group classes in a collection called a package. Packages are convenient for organizing your work separating your work from code libraries provided by others The main reason for using packages is to guarantee the uniqueness of class names. You wouldn’t want two copies of Bicycle class

28 Packages Cont.’

29 Static Imports

30 Static Imports Cont.’

31 Extending A Class

32 Dynamic Binding

33 super Whenever you want to access a instance field of your super class inside of your sub class you have to use the keyword super See eclipse for example

34 Final Classes

35 Final Methods

36 Assignments 3 & 4 Due Dec 4th – posted to class website


Download ppt "Chapter 4 Objects and Classes."

Similar presentations


Ads by Google