• No se han encontrado resultados

Sinterizacion

1.4 CERÁMICOS LAMINADOS: PROPIEDADES MECÁNICAS

1.4.1 TENSIONES REGIDUALES

In this chapter you learn Object-oriented concepts

Encapsulation, information hiding, interface, service, and message passing Java concepts

Class, object, parameter passing, method invocation, method creation, categories of variables, and default constructor

Programming skills

Design, implement, and test a simple Java program in a purely object-oriented way In Chapter 2, you have learned how to create a simple application program. Every program in Chapter 2 has exactly one class and one method, the main, and every variable and executable statement is placed inside the method main. In this chapter, you will learn to create new classes. Recall that a class encapsulates both data and operations. You will also learn to create objects or instances of a class. Th us, in this chapter you learn to design a program in a purely object-oriented way.

In keeping with the pedagogical principle of introducing diffi cult concepts in an incremental fashion, this chapter is intentionally made short and simple. Although vari-ous aspects of object-oriented design are introduced in this chapter, many ideas will be explored in more detail in later chapters. For instance, the method invocation and the method creation discussed in this chapter are limited to methods with at most one formal parameter. Methods having more than one formal parameter are introduced in Chapter 6.

Similarly, we introduce the concept of a constructor through default constructor. Th e gen-eral case is discussed in Chapter 6.

CLASS

Recall that keeping data along with operations is known as encapsulation and is one of the fundamental principles of object-oriented design. Another, diff erent but closely related principle of object-oriented design is that of information hiding and it refers to the fact that

Apago PDF Enhancer

the user need not and should not have access to the internal parts. Th e user is provided with a set of operations and all that a user needs to know is the specifi cations of each of those operations.

Attributes store the data and operations access and manipulate attributes. As you have already seen in Chapter 2, the basic syntax for a class defi nition is

[accessModifier] class ClassName {

[members]

}

where ClassName is an identifi er you wish to give to the class and members consists of attributes and operations of the class. As a general rule, we keep classes public. Th us, we use the following simplifi ed syntax template for user-defi ned classes:

public class ClassName {

[members]

}

Self-Check

1. Consider your television. To use the television, you need not know about its internal parts is an example of .

2. In Java, class is a . Attributes

Attributes of a class are used to store data and they can be classifi ed into two categories:

instance variables and class variables. Certain attributes are such that each object or instance has its own variable and hence the name instance variable. However, certain attributes are shared by all objects or instances of a class. Th ese attributes are known as class variables and will be introduced in Chapter 7.

Th e syntax template of an instance variable is

[accessModifier] dataType identifierOne[[=LOne], ...,

identifierN[=LN]];

Have you ever thought how unwise it is to keep data as public? If you have access to your bank’s data, you can change your account balance to any value. By the same token, if you are running a business, if you allow your customers complete access to their account, they can modify their account balance as they please. In short, we have complete chaos!

Th erefore, adhering to the principle of information hiding, unless there is a very compelling

Apago PDF Enhancer

reason, we keep all instance variables as private. Being private means, only the object alone has access to its instance variables.

Th us, the simplifi ed syntax template for instance variable is private dataType variableName;

Example 3.1

In this example, we begin the creation of a new class Stock to store information about a stock. Assume that you want to keep information about the number of stocks owned, stock symbol, and dividend per year. Th e number of stocks currently owned by a person is an integer value. Th e stock symbol is a string. Th e dividend per year is a real number. Th e instance variables of the Stock class can be declared as follows:

/**

Keep s ticker symbol, number of shares and dividend

*/

class Stock {

private int numberOfShares;

private String tickerSymbol;

private double dividend;

// add code to implement operations }

It is useful to visualize a Stock class as a unit of three slots, each slot having its own label (Figure 3.1).

numberOfShares

tickerSymbol

dividend Stock

FIGURE 3.1 Visual representation of Stock class.

Apago PDF Enhancer

Self-Check

3. Declare an instance variable to keep track of the number of students enrolled in a course.

4. Declare an instance variable to keep track of the maximum grade point average among students enrolled in a course.

Operations

Data being kept as private, it is not directly accessible to other objects. Th e operations are public and operations provide the necessary interface to

Initialize or modify an instance variable

Retrieve the current value of an instance variable

Compute a new value based on current value of an instance variable

Recall that the implementation of an operation is called a method. Before we indulge in creating methods, let us look into the usage or invocation of a method to get some insight.

Introspection

displayTime is an operation of all clocks. Can the displayTime method of an electronic clock be the same as the displayTime method of a mechanical clock?

Eat can be an operation of all mammals. Do humans and dogs eat the same way?

Self-Check 5. As a general rule, keep operations .

6. List an operation of your television other than switch on and switch off .