Jocel L. Garrido,CCNA
Object Oriented Programming 2 (Java) Instructor: Jocel L. Garrido Classes 3 Parts: -Class Name -Attributes -Functions or methods
class Reading_Material public class Reading_Material {
public class Reading_Material { private String Title, Publisher; private int date_published, current_page; public int get_page() { return current_page; }
-Title -Publisher -date_published -current_page
private String Title, Publisher; private int date_published, current_page; public int get_page() { }
return current_page;
+get_page +turn_page_forward +turn_page_backward +get_Publisher +get_date_published +get_Title
Syntax: class { [] [] [] }
Example: public class Person { //attribute declarations private String name; private int age; private Date birthday; // class constructor public Person() { name = "secret"; age = 0; birthday = new Date(7,7); }
//accessor methods - setters public void setName(String X){ name= X; } }
OOP2: Object Oriented Programming 2 (Java)
Jocel L. Garrido,CCNA
• • • •
What is a Method? A method refers to a piece of code referring to behaviors associated either with an object or its class A code found in a class for responding to a message The executable code that implements the logic of a particular message for a class An operation or function that is associated with an object and is allowed to manipulate the object's data A method call is thus considered to be a request to an object to perform some task.
Creating a Method
Steps in declaring a method 1. Set the return type 2. Provide method name 3. Declare formal parameters method signature • consists of the method name and its parameters • must be unique for each method in a class A method with empty parameters
class Number { int multiply(int i, int j) { return i*j; } int divide(int i, int j) { return i/j; } double getPi() { return 3.14159265358979; }
return statement • allows the method