Download presentation
Presentation is loading. Please wait.
1
Object Oriented Programming
Class Methods & array of objects
2
Course class Course { // Data Member private String studentName;
private String courseCode ; } studentName: String courseCode: String public class CourseRegistration { public static void main(String[] args) { Course course1, course2; //Create and assign values to course1 course1 = new Course( ); course1.courseCode= “CSC112“; course1.studentName= “Majed AlKebir“; //Create and assign values to course2 course2 = new Course( ); course2.courseCode= “CSC107“; course2.studentName= “Fahd AlAmri“; System.out.println(course1.studentName + " has the course “+ course1.courseCode); System.out.println(course2.studentName + " has the course “+ course2.courseCode); }
3
Class Common Methods Accessor method (getters): to get information about an object Mutator methods (setters):to mutate (change) an object’s state Printing method: to specify how to print an object Constructor method: to initiate an object by sending its values as parameters
4
Getters The object point of view The user point of view
Are operations performed by the object returning to outsiders data retrieved from the object state. Are services called from outside allowing to retrieve data from the object state. object:X public private Getters :Y (Client) Data Getters are: Public With no parameters With return value
5
Template for Getters public class ClassName {
private dataType1 attribute1; . . . private dataTypen attributen; public dataType1 getAttribute1() { return attribute1; } public dataTypen getAttributen() { return attributen;
6
Getter Example return type access modifier method name parameter list (empty in this case) public int GetAge() { return age; } return statement start and end of method body (block) Because accessor methods have a return type they must have at least one return statement
7
Setters The object point of view The user point of view
Are services used by outsiders allowing to provide to the object the data that should be stored in the object state. Are operations performed by the object allowing to receive and store in the object state the data provided by outsiders. object:X public private Setters :Y (Client) Data Setters are: Public With 1 parameter With no return value
8
Template for Setters public class ClassName {
private dataType1 attribute1; . . . private dataTypen attributen; public void setAttribute1(dataType1 param){ attribute1 = param; } public void setAttributen(dataTypen param) { attributen = param;
9
public class Course { // Attributes private String studentName; private String courseCode ; ... public String getStudentName() { return studentName; } public String getCourseCode() { return courseCode; public void setStudentName(String val) { studentName = val; public void setCourseCode(String val) { courseCode = val;
10
public class CourseRegistration {
public static void main(String[] args) { Course course1, course2; //Create and assign values to course1 course1 = new Course( ); course1.setCourseCode(“CSC112“); course1.setStudentName(“Majed AlKebir“); //Create and assign values to course2 course2 = new Course( ); course2.setCourseCode(“CSC107“); course2.setStudentName(“Fahd AlAmri“); System.out.println(course1.getStudentName() + " has the course “ + course1.getCourseCode()); System.out.println(course2.getStudentName() + " has the course “ + course2.getCourseCode()); }
11
Print Printing method: to specify how to print an object
class Course { // Instance attributes // Attributes private String studentName; private String courseCode ; // Methods public String getStudentName() {return studentName;} public String getCourseCode() {return courseCode;} public void setStudentName(String val) {studentName = val;} public void setCourseCode(String val) {courseCode = val;} // print content public void disply () { System.out.println(getStudentName()+" has the course “ + getCourseCode()); }
12
public class CourseRegistration {
public static void main(String[] args) { Course course1, course2; //Create and assign values to course1 course1 = new Course( ); course1.setCourseCode(“CSC112“); course1.setStudentName(“Majed AlKebir“); //Create and assign values to course2 course2 = new Course( ); course2.setCourseCode(“CSC107“); course2.setStudentName(“Fahd AlAmri“); course1.disply(); }
13
Class Constructors Constructors define the initial states of objects when they are created. ClassName x = new ClassName(); A class contains at least one constructor. A class may contain more than one constructor.
14
Constructors Constructor is a special method that gets invoked “automatically” at the time of object creation. Constructor is normally used for initializing objects with default values unless different values are supplied. Constructor has the same name as the class name. Constructor cannot return values. A class can have more than one constructor as long as they have different parameter (i.e., different input arguments syntax). Constructor is a special method that gets invoked “automatically” at the time of object creation. Constructor is normally used for initializing objects with default values unless different values are supplied. Constructor has the same name as the class name. Constructor cannot return values. A class can have more than one constructor as long as they have different signature (i.e., different input arguments syntax).
15
The Default Class Constructor
If no constructors are defined in the class, the default constructor is added by the compiler at compile time. The default constructor does not accept parameters and creates objects with empty states. ClassName x = new ClassName();
16
Class Constructors Declaration
public <constructor name> ( <parameters> ){ <constructor body> } The constructor name: a constructor has the name of the class . The parameters represent values that will be passed to the constructor for initialize the object state. Constructor declarations look like method declarations except that they use the name of the class and have no return type. Invoking: There is NO explicit invocation statement needed: When the object creation statement is executed, the constructor method will be executed automatically.
17
Constructor with No-Parameter
public class Person { String name; int age; // Constructor public Person(){ name = “ ”; age = 0;} } A. The instance variable is allocated in memory. x Person age “ ” B. The object is created with initial state name C. The reference of the object created in B is assigned to the variable. x A Person P1; B P1 = ; Person age “ ” Object: Kasree bast maquam 1 new Person( ) name C Code State of Memory
18
Class with Multiple Constructors
Public class Person { String name; int age; // Constructor public Person(){ name = “ ”; age = 0;} public Person(String n, int a ) { name = n; age = a; }} A. The constructor declared with no-parameter is used to create the object x Person age “ ” name B. The constructor declared with parameters is used to create the object y Object: Kasree bast maquam 27 Ahmed A Person P1 , P2; P1 = new Person() P2 = new Person(“Ahmed”,27); name B age State of Memory Code
19
Constructor and Static attribute
One of the most common example is to have a variable that could keep a count of how many objects of a class have been created. Note: Java creates only one copy for a static variable which can be used even if the class is never instantiated.
20
Constructor and Static attribute
Public class Person{ string name; int age; public static int numofPerson=0; // Constructor public Person() { name = “ ”; age = 0; numofPerson++} public Person( string n , int a){ name = n; age = a; numofPerson++;} } public static void main(String[] args) { Person P1 = new Person(); Person P2 = new Person(“ahmed”, 27); }
21
Constructor and Static attribute
public static void Main(string[] args) { Person P1 = new Person(); Person P2 = new Person (“ahmed”, 27); } P1 Person age “ ” name numofPerson A 1 2 B P2 Object: Kasree bast maquam 27 Ahmed A Person P1 , P2; P1 = new Person() P2 = new Person(“Ahmed”,27); name B age State of Memory Code
22
Static methods A class can have methods that are defined as static (e.g., main method). Static methods can be accessed without using objects. Also, there is NO need to create objects. They are prefixed with keyword “static” They can only call other static methods. They can only access static data. Static methods are generally used to group related library functions that don’t depend on data members of its class. For example, Math library functions.
23
Arrays of Objects
24
Arrays of Objects Can use arrays to manipulate objects
Example: create array named array1 with N objects of type T T[] array1 = new T[N] Can instantiate array1 as follows: for (int j = 0; j <array1.length; j++) array1[j] = new T();
25
Array of String Objects
String[] nameList = new String[5]; nameList[0] = "Amanda Green"; nameList[1] = "Vijay Arora"; nameList[2] = "Sheila Mann"; nameList[3] = "Rohit Sharma"; nameList[4] = "Mandy Johnson";
26
Array of String Objects (continued)
27
Arrays of Objects (continued)
Clock[] arrivalTimeEmp = new Clock[100];
28
Instantiating Array Objects
for (int j = 0; j < arrivalTimeEmp.length; j++) arrivalTimeEmp[j] = new Clock();
29
Instantiating Array Objects (continued)
arrivalTimeEmp[49].setTime(8, 5, 10);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.