Thursday, December 3, 2009

Objects in Java - Introduction


What is a Type?

A type is a category or grouping that describes a piece of data. Every data in Java can be identified by its type. Data of the same type share common characteristics. If a datum is of type int, for example, then the programmer knows that it is a whole number that cannot be larger than 231 because these are characteristics that all ints share.

Java has two general categories of types: primitives and classes. Examples of primitives are int, float, and boolean. Primitive types' characteristics cannot be extended or modified by programmers. They represent simple pieces of data such as numbers, bytes, and characters. Classes are user-defined types and are created by programmers to represent more complex objects when simple primitives are insufficient. Many useful classes have already been defined by Sun engineers as part of the J2SE,J2EE, and J2ME libraries. Classes are frequently extended and modified by programmers.


Classes Describe Objects

Classes are the basic building blocks of Java programs. Classes can be compared to the blueprints of buildings. Instead of specifying the structure of buildings, though, classes describe the structure of "things" in a program. These things are then created as physical software objects in the program. Things worth representing as classes are usually important nouns in the problem domain. A Web-based shopping cart application, for example, would likely have classes that represent customers, products, orders, order lines, credit cards, shipping addresses and shipping providers.

Unlike data structures in other languages which only contain data, Java classes consist of both attributes and behaviors. Attributes represent the data that is unique to an instance of a class, while behaviors are methods that operate on the data to perform useful tasks.

Class Syntax

Use the following syntax to declare a class in Java:


//Contents of SomeClassName.java
[public] [( abstract | final )] class ClassName [extends ParentClass]
[implements Interfaces]
{
// variables and methods are declared within the curly braces
}
  • A class can have public or default (no modifier) visibility.
  • It can be either abstract, final or concrete (no modifier).
  • It must have the class keyword, and class must be followed by a legal identifier.
  • It may optionally extend one parent class. By default, it will extend java.lang.Object.
  • It may optionally implement any number of comma-separated interfaces.
  • The class's variables and methods are declared within a set of curly braces '{}'.
  • Each .java source file may contain only one public class. A source file may contain any number of default visible classes.
  • Finally, the source file name must match the public class name and it must have a .java suffix.

Here is an example of a Horse class. Dog is a subclass of animal, and it implements the food interface.


public class Dog extends Animal implements Food
{
//Dog's variables and methods go here
}

What is an Object?

Objects are the physical instantiations of classes. They are living entities within a program that have independent lifecycles and that are created according to the class that describes them. Just as many buildings can be built from one blueprint, many objects can be instantiated from one class. Many objects of different classes can be created, used, and destroyed in the course of executing a program.

Attributes

Although several buildings may be built from the same blueprint and, hence, have the same attributes, such as twelve windows, exterior paint, and a driveway, each instance of the building may have different values for these attributes. One building may have twelve double-pane windows while another may have twelve single-pane windows. One building may have hunter green exterior paint while another may have white exterior paint. The attributes are identical but the values of the attributes are different. Similarly, a class describes what attributes an object will have but each object will have its own values for those attributes. In our shopping cart example, a customer class may specify that customer objects have a first name, last name, and phone number. Each Customer object, though, will have different values for first name, last name, and phone number.

Attributes are represented by non-local variables. These are variables that are not declared within method bodies.

Behaviors

A building has behaviors in addition to attributes: Toilets are flushed; furnaces fire to maintain temperature; doors open and close. Classes describe all of the behaviors of its objects. In our shopping cart example: A customer may place an order; a credit card may reject a purchase; an order may tally line items to generate a total.

Behaviors are represented by methods. An object may call, or invoke, its own methods, or it may call another object's methods that are visible to it.

Constructors

In order to be used by a program, an object must first be instantiated from its class definition. A special type of method called a constructor is used to define how objects are created. A constructor is called with the new keyword. new tells the JVM to allocate memory, initialize instance variables, and assign the object a reference code that uniquely identifies it within the JVM.

Let's create an instance of a Car:

   Dog myDog = new Dog();

Messages



Objects cooperate and communicate with other objects in a program by
passing messages to one another. When anobject invokes a method on
itself or another object, it passes a message to the object that
contains the target method. The message identifies the method to be
invoked and any required data (known as arguments) that the method
needs. When the method finishes executing, either a return value or
void is passed back to the original invoking object, completing the
message.

Let us create another Car object and pass an accelerate message to it.

   Dog myDog = new Dog();
String hairColor;
hairColor = myDog.getHairColor();

Class Inheritance via extends and implements

As you saw in the Horse example, a class can extend one other class and implement many Java interfaces. Extending and implementing is the Java mechanism for representing class inheritance. Inheritance represents an "is a" relationship between two classes. The dog is a animal. When a child class, or subclass, extends a parent class, or super class, it inherits the parent class's visible variables and methods. The child class can read and write the parent class's visible attributes, it can pass messages to the parent class's visible methods, and it can even override (or re-implement) the parent class's visible methods. We'll cover inheritance in-depth in a future installment of the Java Programming Tutorial.

















0 comments:

Post a Comment

 

JAVA INTRODUCTION Blak Magik is Designed by productive dreams for smashing magazine Bloggerized by Ipiet © 2008