a constructor. - Ayi Purbasari

advertisement
IT 405: KPLBO
MATERI 6A INTERAKSI ANTAR
OBJEK III
Ayi Purbasari, ST., MT.
If-Unpas, 2014
METHOD INVOCATION
Information
Hiding /
Accessibility
Accessor
Methods
Constructor
Constructor
Overloading
INFORMATION HIDING / ACCESSIBILITY

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.
}
}
INFORMATION HIDING / ACCESSIBILITY

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 = "123-45-6789";
// etc.

The resultant error message would be
ssn has private access in Student
INFORMATION HIDING / ACCESSIBILITY

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
INFORMATION HIDING / ACCESSIBILITY
METHOD HEADERS, REVISITED
A method’s
access modifier
ACCESSING PRIVATE FEATURES FROM
CLIENT CODE
A “set” method is used to
pass data into an object.
A “get” method is used to retrieve
data from an object.
ACCESSOR METHODS

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)
CONTOH PENGGUNAAN
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.
CONSTRUCTORS
CONSTRUCTORS
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.

DEFAULT CONSTRUCTOR

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 zeroequivalent default values.

WRITING OUR OWN EXPLICIT
CONSTRUCTORS



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.
PASSING ARGUMENTS TO CONSTRUCTORS
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.
REPLACING THE DEFAULT
PARAMETERLESS CONSTRUCTOR
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.
}
OVERLOADING CONSTRUCTORS
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.
OVERLOADING CONSTRUCTORS
// 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.
}
THE USE OF ALL THREE FORMS OF
STUDENT 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("123-45-6789");
// 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("987-65-4321", "John Smith",
21);
THE USE OF ALL THREE FORMS OF
STUDENT 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.
 ("123-45-6789"): One String argument tells the
compiler that we are invoking constructor #2.
 ("987-65-4321", "John Smith", 21): Two Strings and
an int as arguments tell the compiler that we are
invoking constructor #3.

REFERENSI
Beginning Java Object: From Concept to Code.
Author: JACQUIE BARKER
 SoftwareEngineering: A Practitioner Approach
7th Edition. Author: Roger S Pressman

Author: Hendra Komara
21
THANK YOU
Download