Chapter5_2-Methods

Page 19 dr s gannouni dr a touir introduction to oop

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: declared in the method body (called local variables) are created. • The method body is executed using the parameters and local variables. • When the method finishes: • Parameters and local variables are destroyed. Page 19 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Passing Objects to a Method • As we can pass primitive data type values, we can also pass object references to a method using instance variables. • Pass an instance variable to a method means passing a reference of an object. • It means that the corresponding parameter will be a copy of the reference of this objects. – Because the passing parameter mechanism copies the value of the argument (which is an object reference) into the parameter. • The argument and its corresponding parameter refer to the same object. – The object is not duplicated. – There are two instance variables (the argument and the parameter) referring to the same object. Page 20 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP How Private Attributes could be Accessed • Private attributes are not accessible from outside. • Except from objects of the same class. • They are accessible: • From inside: from the object containing the data itself. • From objects of the same class. • They are accessible from outside using accessor operations. • Getters • Setters Page 21 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP class Course { // Data Member private String studentName; private String courseCode ; } 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...
View Full Document

This document was uploaded on 03/03/2014.

Ask a homework question - tutors are online