Presentation is loading. Please wait.

Presentation is loading. Please wait.

IT 405: KPLBO M ATERI 6 A I NTERAKSI A NTAR O BJEK III Ayi Purbasari, ST., MT. If-Unpas, 2014.

Similar presentations


Presentation on theme: "IT 405: KPLBO M ATERI 6 A I NTERAKSI A NTAR O BJEK III Ayi Purbasari, ST., MT. If-Unpas, 2014."— Presentation transcript:

1 IT 405: KPLBO M ATERI 6 A I NTERAKSI A NTAR O BJEK III Ayi Purbasari, ST., MT. If-Unpas, 2014

2 M ETHOD I NVOCATION Information Hiding / Accessibility Accessor Methods Constructor Constructor Overloading

3 I NFORMATION H IDING / A CCESSIBILITY Public Accessibility public class Student { public String name; // etc. public class MyProgram { public static void main(String[] args) { Student x = new Student(); // Because name is a public attribute of the Student class, we may access // it via dot notation from client code. x.name = "Fred Schnurd"; // assign a value to x's name attribute // or: System.out.println(x.name); // retrieve the value of x's name attribute // etc. }

4 I NFORMATION H IDING / A CCESSIBILITY Private Accessibility public class Student { public String name; private String ssn; // etc. public class MyProgram { public static void main(String[] args) { Student x = new Student(); // Not permitted from client code! ssn is private to the // Student class, and so this will not compile. x.ssn = " "; // etc. The resultant error message would be ssn has private access in Student

5 I NFORMATION H IDING / A CCESSIBILITY The same is true for methods that are declared to be private public class Student { // Attribute details omitted from this example. // Methods. public boolean isHonorsStudent() {... } private void printInfo() {... } // etc. } public class MyProgram { public static void main(String[] args) { Student x = new Student(); // Because printInfo() is a private method, we may not access it // via dot notation from client code; this won't compile: x.printInfo(); // etc. The resultant error message would be printInfo() has private access in Student

6 I NFORMATION H IDING / A CCESSIBILITY

7 M ETHOD H EADERS, R EVISITED A method’s access modifier

8 A CCESSING P RIVATE F EATURES FROM C LIENT C ODE A “set” method is used to pass data into an object. A “get” method is used to retrieve data from an object.

9 A CCESSOR M ETHODS For a “get” method, the formula is as follows: public attribute-type getAttributeName() for example, public String getMajorField() For a “set” method, the formula is as follows: public void setAttributeName(attributeType parameterName) for example, public void setMajorField(String major)

10 C ONTOH P ENGGUNAAN public class Student { private String name; private String ssn; private String major; private Professor advisor; // etc. // Set/get methods provided; details omitted. public void assignMajor(String m, Professor p) { // Not as desirable. this.major = m; this.advisor = p; } // etc.

11 C ONSTRUCTORS

12 Student x = new Student(); It turns out that when we instantiate an object via the new keyword, we’re actually invoking a special type of procedure called a constructor. Invoking a constructor serves as a request to the JVM to construct (instantiate) a brand-new object at run time by allocating enough program memory to house the object’s attributes.

13 D EFAULT C ONSTRUCTOR Constructor: Default Constructor Writing Our Own Explicit Constructors If we don’t explicitly declare any constructors for a class, Java automatically provides a default constructor for that class. The default constructor is parameterless—that is, it takes no arguments— and does the “bare minimum” required to initialize a new object: namely, setting all attributes to their zero- equivalent default values.

14 W RITING O UR O WN E XPLICIT C ONSTRUCTORS A constructor’s name must be exactly the same as the name of the class for which we’re writing the constructor—we have no choice in the matter. A parameter list, enclosed in parentheses, is provided for a constructor header as with method headers. We cannot specify a return type for a constructor; by definition, a constructor returns a reference to a newly created object of the type represented by the class to which the constructor belongs.

15 P ASSING A RGUMENTS TO C ONSTRUCTORS public class Student { // Attributes. private String name; private String ssn; private String major; // etc. // We've declared a constructor that accepts three arguments, to accommodate // passing in three attribute values. public Student(String s, String n, String m) { this.setName(n); this.setSsn(s); this.setMajor(m); } // etc.

16 R EPLACING THE D EFAULT P ARAMETERLESS C ONSTRUCTOR public class Student { // Attributes. private String name; private String major; // etc. // We've explicitly programmed a parameterless constructor, // thus replacing the default version. public Student() { // Perhaps we wish to initialize attribute values to something other than // their zero equivalents. this.setName("?"); this.setMajor("UNDECLARED"); } // etc. // Other methods omitted from this example. }

17 O VERLOADING C ONSTRUCTORS public class Student { private String name; private String ssn; private int age; // etc. // Constructor #1: takes no arguments; supercedes the default constructor. public Student() { } // Assign default values to selected attributes, if desired. // this.setSsn("?"); // Those that aren't explicitly initialized in the constructor // will automatically assume the zero-equivalent value for their respective type.

18 O VERLOADING C ONSTRUCTORS // Constructor #2: takes a single String argument. public Student(String s) { this.setSsn(s); } // Constructor #3: takes two Strings and an int as // arguments. public Student(String s, String n, int i) { this.setSsn(s); this.setName(n); this.setAge(i); } // Other methods omitted from this example. }

19 THE USE OF ALL THREE FORMS OF S TUDENT CONSTRUCTOR // We don't know ANYTHING about our first student, // so we use the parameterless constructor to instantiate s1. Student s1 = new Student(); // We know the ssn (only) for our second student, and // so we use the second form of constructor to // instantiate s2. Student s2 = new Student(" "); // We know the ssn, name, and age of our third // student, and so we use the third form of constructor // // to instantiate s3. Student s3 = new Student(" ", "John Smith", 21);

20 THE USE OF ALL THREE FORMS OF S TUDENT CONSTRUCTOR As with overloaded methods, the compiler is able to unambiguously match up which version of constructor is being invoked in each case based on the argument signatures: (): No arguments tells the compiler that we are invoking constructor #1. (" "): One String argument tells the compiler that we are invoking constructor #2. (" ", "John Smith", 21): Two Strings and an int as arguments tell the compiler that we are invoking constructor #3.

21 R EFERENSI Beginning Java Object: From Concept to Code. Author: JACQUIE BARKER SoftwareEngineering: A Practitioner Approach 7th Edition. Author: Roger S Pressman 21 Author: Hendra Komara

22 T HANK Y OU


Download ppt "IT 405: KPLBO M ATERI 6 A I NTERAKSI A NTAR O BJEK III Ayi Purbasari, ST., MT. If-Unpas, 2014."

Similar presentations


Ads by Google