Sie sind auf Seite 1von 9

3.

1 Abstract Data Type


Abstract data types (ADTs) are an important form of program
abstraction. An ADT consists of some hidden or protected data and a set of
methods to perform actions on the data.When we hide data in a class, we say
that the data have been encapsulated. Encapsulation is illustrated in Figure
3.1. The figure shows a class that defines private data, public methods, and
public constructors. It shows that objects can be instantiated from the class
with the new operator. Finally, it shows that the private data is “hidden” inside
the objects, as indicated by the heavy circle surrounding the data, and that it
can be accessed only through the public instance methods. The
implementation of the data, constructors, and methods is normally hidden from
a programmer who uses the class. The class acts as a boundary surrounding
the constructor, methods, and data.

Figure 3.1 ADTs encapsulation data and methods

3.2 Constructors
Instantiation is made possible by the use of a constructor, which serves several
purposes.
• A constructor is given the same name as the class to allow for the data
type of objects to be declared.
• A constructor is normally used in conjunction with the keyword new,
which allocates memory space from the heap. The heap is an area of
memory set aside for the dynamic allocation of computer memory to
objects during run time.

1
• A constructor provides the storage in memory and the initialization of the
instance variables allocated to the object.
• For each separate invocation of the constructor, a new object will
become instantiated.

SYNTAX
Constructor:
public class-name ( formal-parameter-list )
{
declarations
statements
}

SYNTAX
Instantiation:
object-name = new class-constructor();
object-name = new class-constructor(argument-list);
where argument-list consists of one or more values used by the constructor to
initialize the data of the object.

public final class String ...


{
// constructors
public String();
public String(String value);
..
// instance methods
public String concat(String str);
public int length();
public String replace(char oldChar, char newChar);
public String toUpperCase();
..}

Examples of creating the object alphabet follow.


alphabet = new String();
The above uses the first String constructor with no arguments.
alphabet = new String("abcdefghijklmnopqrstuvwxyz");

2
Figure 2.1 Packages and classes

Figure 2.2 Primitive types stored by value

Figure 2.2 illustrates how three primitive types int, char, and float can be
conceptually represented in the memory of the computer.

In Figure 2.2, the values of the identifiers year, letter, and tax are stored at the
memory locations depicted by the names of the identifiers. Hence, the
primitive data is stored by value.

Figure 2.3 illustrates that when an identifier of the type String is initialized, the
value of the string is not stored at the memory location depicted by the
identifier, but it is stored in a different location pointed at or referenced by the
identifier. The object alphabet is stored by reference.

3
Figure 2.3 An object is stored by reference

Conceptually, an object is a thing you interact with. You send it various


messages and it will react. There are two kinds of messages:
• a command message
• a query message

From the partial listing of the String class, examples of command messages are
concat, replace, and toUpperCase; an example of a query message is length.
These messages are implemented as instance methods.

SYNTAX
Passing a Message to an Object by an Instance Method:
object.method-name();
object.method-name(argument-list);
// program to demonstrate the String class and some of its instance methods

import avi.*;

class Example_1
{
public static void main(String[] args)
{
String oldString = "Have a nice day!";
String newString = oldString.replace('a','-');
String capitalString = oldString.toUpperCase();
int lengthOfString = oldString.length();
Window screen = new Window("Example_1.java","bold","blue",36);
screen.showWindow();
screen.write("\n\tOld string: " + oldString + "\n");
screen.write("\n\tNew string: " + newString + "\n");
screen.write("\n\tOld string in upper case: " + capitalString +"\n");
screen.write("\n\tLength of old string: " + lengthOfString +"
characters\n");
}
}

4
package stringku;

/**
* @author Eto
*/
public class Main {
public static void main(String[] args) {
String oldString = "Have a nice day!";
String newString = oldString.replace('a','-');
String capitalString = oldString.toUpperCase();
int lengthOfString = oldString.length();
String gabung= oldString.concat(" and always Fun");

System.out.println("oldString : "+oldString);
System.out.println("newString : "+newString);
System.out.println("capitalString : "+capitalString);
System.out.println("length Of oldString : "+lengthOfString+" karakter");
System.out.println("gabung : "+gabung);
}
}

run:
oldString : Have a nice day!
newString : H-ve - nice d-y!
capitalString : HAVE A NICE DAY!
length Of oldString : 16 karakter
gabung : Have a nice day! and always Fun

5
3.3 Instance Methods
As you are well aware, instance methods relate to some aspect of the
instantiated object. For example, the length of a string, the conversion of a
string toUpperCase characters, and so on.

SYNTAX
Method:
modifier(s) return-type method-name ( formal-parameter-list )
{
declarations
statements
}

The return-type identifies the type of the value that the method will return with
its return statement.
The syntax of the return statement is:

SYNTAX
Return Statement:
return expression;

3.4 Class Methods


An instance method such as the volumeOfWater method of the SwimmingPool
class is invoked through an object of the class, for example, "volume
=largePool.volumeOfWater();". A class method, also known as a static method,
is one that cannot be invoked through an object. To differentiate an instance
method from a class method, one of the modifiers used in the signature of the
class method is declared as static.

In effect, at this point we have identified two ways of classifying methods:


• A method defined as static is called a class method; a method not
defined as static is called an instance method.
• A private method other than the main method is often called a helper
method; for now, if a method is not private, it is public; later we will learn
more categories of protection for methods.

6
Figure 3.2 A class may define constructors, instance, class, and helper methods

7
8
9

Das könnte Ihnen auch gefallen