Professional Documents
Culture Documents
7-1
Classes, Objects, and Methods
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 3
Classes, Objects, and Methods
Objectives
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 4
Classes, Objects, and Methods
Creating a Class Template
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 5
Classes, Objects, and Methods
Creating a Class Template Example
public class Vehicle {
// the Vehicle class has two fields
private String make;
private int milesPerGallon;
//constructor
public Vehicle(){
}
//mutator/setter method
public void setMake(String m){
make = m;
}
//mutator/setter method
public void setMilesPerGallon(int mpg){
milesPerGallon = mpg;
}
//accessor/getter method
public String getMake(){
return make;
}
//accessor/getter method
public int getMilesPerGallon(){
return milesPerGallon;
}
}
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 6
Classes, Objects, and Methods
Creating an Instance of a Class
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7
Classes, Objects, and Methods
Instantiate an Instance
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 8
Classes, Objects, and Methods
Constructors
• Constructors are methods that
allow the user to create instances of
(instantiate) a class.
• Good programming practice
dictates that classes should have a
default constructor.
• Constructors which contain
parameters typically initialize the
private variables of the class to
values passed in by the user.
• Constructors do not have a return
type (void or other).
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 9
Classes, Objects, and Methods
Default Constructor
public Vehicle() {
make = "";
milesPerGallon = 0;
}
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 10
Classes, Objects, and Methods
Constructor with Parameters
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 11
Classes, Objects, and Methods
Instantiate Vehicle Instance
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12
Classes, Objects, and Methods
Defining Methods
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 13
Classes, Objects, and Methods
Components of a Method
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 14
Classes, Objects, and Methods
Components of a Method
• Parameter(s):
– The programmer may choose to include parameters depending on the
purpose and function of the method.
– Parameters can be of any primitive or type of object, but the
parameter type used when calling the method must match the
parameter type specified in the method definition.
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 15
Classes, Objects, and Methods
Method Components Example
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 16
Classes, Objects, and Methods
Class Methods
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 17
Classes, Objects, and Methods
Accessor Methods
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 18
Classes, Objects, and Methods
Mutator Methods
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 19
Classes, Objects, and Methods
Functional Methods
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 20
Classes, Objects, and Methods
Functional Methods
//Compares the miles per gallon of each vehicle passed in, returns 0 if
they are the same, 1 if the first vehicle is larger than the second and -1
if the second vehicle is larger than the first*/
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 21
Classes, Objects, and Methods
Using Constructors and Methods in a
Driver class main method Example
• For the following:
– What functionality does each line have?
– What will the final print statement print to the screen?
Vehicle v;
v=new Vehicle();
v.setMake("Ford");
v.setMilesPerGallon(35);
System.out.print("My "+v.getMake() +
" gets " + v.getMilesPerGallon() + " mpg.");
}
}
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 22
Classes, Objects, and Methods
this Reference
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 23
Classes, Objects, and Methods
this Reference Example
//constructor
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 24
Classes, Objects, and Methods
Card Class Example
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 25
Classes, Objects, and Methods
Reference Object Representation
suit = null
name= null
points = 0
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 26
Classes, Objects, and Methods
Understanding Garbage Collection Example
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 27
Classes, Objects, and Methods
Finalizers
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 28
Classes, Objects, and Methods
Finalizers
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 29
Classes, Objects, and Methods
Finalize Method Example
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 30
Classes, Objects, and Methods
Terminology
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 32
Classes, Objects, and Methods
Summary
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 33
Classes, Objects, and Methods
Summary
JF 7-1 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 34
Classes, Objects, and Methods