It would be nice if we could focus our attention on performing only one action at a time and performing it well, but that is usually difficult to do. The human body performs a great variety of operations in parallel or, as we will say throughout this chapter, concurrently. Respiration, blood circulation, digestion, thinking and walking, for example, can occur concurrently. All the senses—sight, touch, smell, taste and hearing—can be employed
at once. Computers, too, can perform operations concurrently. It is common for personal computers to compile a program, send a file to a printer and receive electronic mail messages over a network concurrently. Only computers that have multiple processors can truly execute multiple instructions concurrently. Operating systems on single-processor computers create the illusion of concurrent execution by rapidly switching between activities, but on such computers only a single instruction can execute at once.
5 Eylül 2010 Pazar
30 Ağustos 2010 Pazartesi
final Methods and Classes
Variables can be declared final to indicate that they cannot be modified after they are initialized—such variables represent constant values. It is also possible to declare methods and classes with the final modifier. A method that is declared final in a superclass cannot be overridden in a subclass. Methods that are declared private are implicitly final, because it is impossible to override them in a subclass. Methods that are declared static are also implicitly final. A final method’s declaration can never change, so all subclasses use the same method implementation, and calls to final methods are resolved at compile time—this is known as static binding. Since the compiler knows that final methods cannot be overridden, it can optimize programs by removing calls to final methods and replacing them with the expanded code of their declarations at each method call location—a technique known as inlining the code.
Case Study: Payroll System Using Polymorphism
We use an abstract method and polymorphism to perform payroll calculations based on the type of employee.We create an enhanced employee hierarchy to solve the following problem:
A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid a fixed weekly salary regardless of the number of hours worked, hourly employees are paid by the hour and receive overtime pay for all hours worked in excess of 40 hours, commission employees are paid a percentage of their sales and salaried- commission employees receive a base salary plus a percentage of their sales. For the current pay period, the company has decided to reward salaried-commission employees by adding 10% to their base salaries. The company wants to implement a Java application that performs its payroll calculations polymorphically.
Abstract Classes and Methods
When we think of a class type, we assume that programs will create objects of that type. In some cases, however, it is useful to declare classes for which the programmer never intends to instantiate objects. Such classes are called abstract classes. Because they are used only as superclasses in inheritance hierarchies, we refer to them as abstract superclasses. These classes cannot be used to instantiate objects, because, as we will soon see, abstract classes are incomplete. Subclasses must declare the “missing pieces.”
An abstract class’s purpose is to provide an appropriate superclass from which other classes can inherit and thus share a common design. In the Shape hierarchy, for example, subclasses inherit the notion of what it means to be a Shape—common attributes such as location, color and borderThickness, and behaviors such as draw, move, resize and changeColor. Classes that can be used to instantiate objects are called concrete classes.Such classes provide implementations of every method they declare (some of the implementations can be inherited). For example, we could derive concrete classes Circle, Square and Triangle from abstract superclass TwoDimensionalShape. Similarly, we could derive concrete classes Sphere, Cube and Tetrahedron from abstract superclass ThreeDimensionalShape. Abstract superclasses are too general to create real objects—they specify only what is common among subclasses. We need to be more specific before we can create objects. For example, if you send the draw message to abstract class TwoDimensionalShape, it knows that two-dimensional shapes should be drawable, but it does not know what specific shape to draw, so it cannot implement a real draw method. Concrete classes provide the specifics that make it reasonable to instantiate objects.
An abstract class’s purpose is to provide an appropriate superclass from which other classes can inherit and thus share a common design. In the Shape hierarchy, for example, subclasses inherit the notion of what it means to be a Shape—common attributes such as location, color and borderThickness, and behaviors such as draw, move, resize and changeColor. Classes that can be used to instantiate objects are called concrete classes.Such classes provide implementations of every method they declare (some of the implementations can be inherited). For example, we could derive concrete classes Circle, Square and Triangle from abstract superclass TwoDimensionalShape. Similarly, we could derive concrete classes Sphere, Cube and Tetrahedron from abstract superclass ThreeDimensionalShape. Abstract superclasses are too general to create real objects—they specify only what is common among subclasses. We need to be more specific before we can create objects. For example, if you send the draw message to abstract class TwoDimensionalShape, it knows that two-dimensional shapes should be drawable, but it does not know what specific shape to draw, so it cannot implement a real draw method. Concrete classes provide the specifics that make it reasonable to instantiate objects.
29 Ağustos 2010 Pazar
Object Oriented Programming: Polymorphism-Introduction
Polymorphism enables us to “program the general” rather than “program in the specific.” In particular, polymorphism enables us to write programs that process objects that share the same superclass in a class hierarchy as if they are all objects of the superclass; this can simplify programming.
Consider the following example of polymorphism. Suppose we create a program that simulates the movement of several types of animals for a biological study. Classes Fish, Frog and Bird represent the three types of animals under investigation. Imagine that each of these classes extends superclass Animal, which contains a method move and maintains an animal’s current location as x-y coordinates. Each subclass implements method move. Our program maintains an array of references to objects of the various Animal subclasses.
To simulate the animals’ movements, the program sends each object the same message once per second - namely, move. However, each specific type of Animal responds to a move message in a unique way—a Fish might swim three feet, a Frog might jump five feet and a Bird might fly ten feet. The program issues the same message (i.e., move) to each animal object generically, but each object knows how to modify its x-y coordinates appropriately for its specific type of movement. Relying on each object to know how to “do the right thing” (i.e., do what is appropriate for that type of object) in response to the same method call is the key concept of polymorphism. The same message (in this case, move) sent to a variety of objects has “many forms” of results—hence the term polymorphism.
Consider the following example of polymorphism. Suppose we create a program that simulates the movement of several types of animals for a biological study. Classes Fish, Frog and Bird represent the three types of animals under investigation. Imagine that each of these classes extends superclass Animal, which contains a method move and maintains an animal’s current location as x-y coordinates. Each subclass implements method move. Our program maintains an array of references to objects of the various Animal subclasses.
To simulate the animals’ movements, the program sends each object the same message once per second - namely, move. However, each specific type of Animal responds to a move message in a unique way—a Fish might swim three feet, a Frog might jump five feet and a Bird might fly ten feet. The program issues the same message (i.e., move) to each animal object generically, but each object knows how to modify its x-y coordinates appropriately for its specific type of movement. Relying on each object to know how to “do the right thing” (i.e., do what is appropriate for that type of object) in response to the same method call is the key concept of polymorphism. The same message (in this case, move) sent to a variety of objects has “many forms” of results—hence the term polymorphism.
Object Class
Method | Description |
---|---|
clone | This protected method, which takes no arguments and returns an Object reference, makes a copy of the object on which it is called. When cloning is required for objects of a class, the class should override method clone as a public method and should implement interface Cloneable (package java.lang). |
equals | This methods compares two objects for equality and returns true if they are equal and false otherwise. The method takes any Object as an argument. It should returns if the argument is null. It should returns true if an object is compared to itself, as in object1.equals (object1). |
finalize | This protected method is called by the garbage collector to perform termination housekeeping on an object just before the garbage collector reclaims the object`s memory. |
getClass | Every object in java knows its own type at execution time. Method getClass returns an object of class Class (package java.lang) that contains information about the object`s type, such as its class name. |
hashCode | A hashtable is a data structure that relates one object, called the key, to another object, called the value. when initially inserting a value into a hashtable, the key`s hashCode method is called. The hashcode value returned is used by the hashtable to determine the location at which to insert the corresponding value. |
notify,wait | Methods notify, notifyAll and the three overloaded version of wait are related to multithreading. |
toString | This methods returns a String representation of an object. |
Software Engineering with Inheritance
When a new class extends an existing class, the new class inherits the non-private members of the existing class. We can customize the new class to meet our needs by including additional members and by overriding superclass members. Doing this does not require the subclass programmer to change the superclass`s source code.
Although inheriting from a class does not require access to the class source code, developers often insist on seeing the source code to understand how the class is implemented. Object oriented programming facilitates software reuse, potentially shortening development time.
Although inheriting from a class does not require access to the class source code, developers often insist on seeing the source code to understand how the class is implemented. Object oriented programming facilitates software reuse, potentially shortening development time.
28 Ağustos 2010 Cumartesi
Relationship between Superclasses and Subclasses
In this essay, we use an inheritance hierarchy containing types types of employees in a company`s payroll application to discuss the relationship between a superclass and its subclass. In this company, commission employees (who will be represented as objects of a superclass) are paid a percentage of their sales, while base-salaried commission employees ( who will be represented as objects of a subclass) receive a base salary plus a percentage of their sales.
The first example declares class CommissionEmployee, which directly inherits from class Object and declares as private instance variables a first name, last name, social security number, commission rate and gross(i.e., total) sales amount.
The first example declares class CommissionEmployee, which directly inherits from class Object and declares as private instance variables a first name, last name, social security number, commission rate and gross(i.e., total) sales amount.
Object Oriented Programming: Inheritance - Superclasses and Subclasses
Often, an object of one class is an object of another class as well. For example, in geometry, a rectangle is a quadrilateral ( as a squares, parallelograms and trapezoids).Thus, in java, class Rectangle can be said to inherit from class Quadrilateral. In this content, class Quadrilateral is a superclass and class Rectangle is a subclass. A rectangle is a specific type of quadrilateral, but it is incorrect to claim that every quadrilateral is a rectangle. The quadrilateral could be a parallelogram or some other shape. Note that superclasses tend to be "more general" and subclasses "more specific".
Object Oriented Programming: Inheritance - Introduction
This essay continues our discussion of object-oriented programming (OOP) by introducing one of its primary features -inheritance, which is a form of software reuse in which a new class is created by absorbing an existing class`s members and embellishing them with new or modified capabilities. With inheritance, programmers save time during program development by reusing proven and debugged high-quality sofware. This is also increases the likehood that system will be implemented effectively.
When creating a class, rather than declaring completely new members, you can designate that the new class should inherit the members of an existing class. The existing class is called the superclass, and the new class is the subclass. ( The C++ programming language refers to the superclass as the base class and the subclass as the derived class. Each subclass can become the superclass for future subclasses.
A subclass normally adds its own fields and methods. Therefore, a subclass is more specific than its superclass and represent a more specialized group of objects. Typically, the subclass exhibits the behaviors of its superclass and additionaly behaviors that are specific to the subclass. This is why inheritance is sometimes referred to as specialization.
When creating a class, rather than declaring completely new members, you can designate that the new class should inherit the members of an existing class. The existing class is called the superclass, and the new class is the subclass. ( The C++ programming language refers to the superclass as the base class and the subclass as the derived class. Each subclass can become the superclass for future subclasses.
A subclass normally adds its own fields and methods. Therefore, a subclass is more specific than its superclass and represent a more specialized group of objects. Typically, the subclass exhibits the behaviors of its superclass and additionaly behaviors that are specific to the subclass. This is why inheritance is sometimes referred to as specialization.
Kaydol:
Kayıtlar (Atom)