01b Classes, Objects, and Methods (Review)

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Classes, objects, and methods

Creating a class that is a blueprint for


objects
Objects review

If you need to review (or never really understood) objects, the


link below is a good (and extremely thorough) review of object
oriented programming (OOP) basics:
www3.ntu.edu.sg/home/ehchua/programming/java/J3a_OOPBasics.html

Before you continue on to the next slides, take some time to


review the information in this link!

In addition, the PAP / CS1 powerpoints on objects will be


much more comprehensive than these – use them!
Classes / objects / methods

A class is a blueprint; the definition of what something is and


what it should do. A class is a bundle of variables (fields) and
methods, and can also be consider a custom data type.

An object is a particular instance of a class. If a class is a


cookie cutter, then an object is an actual cookie. Every object
has all the variables (fields) and methods of the class, and the
variables have actual values.

A method is a named block of code that does something. A


method is a "behavior" (action) of an object.
Reference vs. primitive types

Data types such as int, boolean, double, char, etc., are


referred to as primitive types. You can't call methods on these
types, they are simple (primitive!) values.

Custom type variables (classes you create or use from


somewhere else, that can be instantiated) are referred to as
reference types.

Reference type variables (aka objects in CS1) store references


to objects. This link has a really excellent explanation of
variable types!
Class example
The following class defines what it means to be a Person, and
is the blueprint for Person objects.

public class Person


{
String name; //instance variables
int age;

//constructor method
public Person(String n, int a) {
name = n;
age = a;
}

//other methods not shown


}
Instance variables
Instance variables are the variables that belong to every
object (instance) of a class. Instance variables (also known
as fields) should be declared (only!) below the class
header, and initialized in the constructor.

Every object will have all the fields and methods in the
class, but each object will have it's own values.

Instance variables are NOT the same as local variables.


Local variables are variables that are declared inside a
method; their scope is only inside the method itself.
Constructor method(s)

A constructor is a special method with no explicit return


type and the same name as the class.

A constructor's job is to initialize instance variables and


return a reference to a new object created in the
computer's memory.

Whenever you see the new keyword (when making


objects), you're calling a class' constructor method.

Put simply, constructors make objects (instances) of


classes!
Constructor method(s) (cont'd)
A class can have any number of constructors with any
number of parameters. A constructor with zero parameters
is referred to as a default constructor. A default
constructor's job is to initialize instance variables to
"default" values.

A constructor with parameters is, unsurprisingly, referred to


as a parameterized constructor.

Creating multiple constructors with different types of


parameters is referred to as overloading.
Constructor method(s) (cont'd)
public class Coin
{
String name; //instance variables
int value;

//default constructor
public Coin() {
name = "";
value = 0;
}

//parameterized constructor
public Coin(String n, int v) {
name = n;
value = v;
}
}
DEFAULT constructor

PARAMETERIZED constructor
Writing methods

Methods that are not declared static (more on this


later) are referred to as instance methods.

Instance methods must be called on objects, and will


run code / return values based on the state of the
object it was called on.

Remember, instance methods should NOT have


the static modifier. We'll learn what the static
modifier means later this year.
Writing methods (cont'd)
Methods must be written outside of other methods (but inside a class). A
method header (the line that declares the method above the block of code
that comprises the method) has the following parts (based on the method
below):

public int getSum(int a, int b)


{
//method's code
}
Code Function
public access modifier (more info later)
int return type
getSum method name
int a, int b method's parameters
Method parameters (review)

What are parameters? Let's look at a simple example:

public int someMethod(int num)


{
return num + 10;
}

A method's parameters go inside the round brackets


(parentheses) at the end of the method header.
Method parameters (cont'd)

int num is the method's one parameter (type is int, variable


name is num). Methods can have any number of parameters
(including none) - this method has one.

A parameter is a variable that stores a value supplied to


the method when it's called. The variable num is a
"placeholder" variable; it stores whatever information the
method needs to complete its job.

The method uses the information supplied, then returns a


result of the type specified (in this case int).
Return type
A method will return a value of the declared type when it's
done running. The value (of the type declared) will be
returned back to wherever the method was called.

public int someMethod(int num)


{
return num + 10;
}

This method has int as the return type. A method can return
any valid type (primitives like int, double, etc., or reference
types like String, Person, etc.).
The void return type
The void return type is commonly misunderstood. A method
that returns void does not return any type of data back to
where it was called.

It does, however, run its code block, then return back to where
it was called.

A void return type method DOES return, it just doesn't


return any type of data!

A void return type method is NOT the same as a


constructor method, which is the only method without an
explicitly declared return type.
The void return type (cont'd)
A method that returns void CAN contain a return
statement, but it doesn't have to! If it doesn't have an explicit
return statement, it will return when it reaches the end of its
code block.

public void method(int a) {


if (a > 10) {
return; //returns (ends the method)
}
System.out.println("something");
}

The method above won't print anything for values of a greater


than 10. Were the if statement omitted, it would always print
"something" and return afterward.
Overloaded methods
Methods can have the same "signature" (same name and
return type), but have different parameters (in number and/or
type).

Methods like this are referred to as being overloaded. The


substring methods are a good example - they have the
same name and return type, but have different parameter lists
(one has a single integer, the other has two).

Methods can have the same signature as long as their


parameter lists are different (in type, number, or both).

Constructors are frequently overloaded, such that objects can


be made for a variety of different situations.

You might also like