Introduction to Classes and Objects in Java

Everything in Java is either a class, a part of a class, or describes how a class behaves. 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. Programming languages provide a number of simple data types like int, float and String. However very often the data you want to work with may not be simple ints, floats or Strings. Classes let programmers define their own more complicated data types.

All the action in Java programs takes place inside class blocks, in this case the HelloWorld class. In Java almost everything of interest is either a class itself or belongs to a class. Methods are defined inside the classes they belong to. Even basic data primitives like integers often need to be incorporated into classes before you can do many useful things with them. The class is the fundamental unit of Java programs. For instance consider the following Java program:

class HelloWorld {
  public static void main (String args[]) {
    System.out.println("Hello World");
  }
}

Another Example:

class GoodbyeWorld {
  public static void main (String args[]) {
    System.out.println("Goodbye Cruel World!");
  }
}

Save this code in a single file called hellogoodbye.java in your javahtml directory, and compile it with the command javac hellogoodbye.java. Then list the contents of the directory. You will see that the compiler has produced two separate class files, HelloWorld.class and GoodbyeWorld.class. javac hellogoodbye.java

The second class is a completely independent program. Type java GoodbyeWorld and then type java HelloWorld. These programs run and execute independently of each other although they exist in the same source code file. 

Class Syntax:

Use the following syntax to declare a class in Java:
//Contents of SomeClassName.java
[ public ] [ ( abstract | final ) ] class SomeClassName [ extends SomeParentClass ] [ implements SomeInterfaces ]
{
        // 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. Horse is a subclass of Mammal, and it implements the Hoofed interface.

public class Horse extends Mammal implements Hoofed
{
         //Horse's variables and methods go here
}

Lets take one more example of Why use Classes and Objects. For instance let's suppose your program needs to keep a database of web sites. For each site you have a name, a URL, and a description. 

class website
{
     String name;
     String url;
     String description;
}


These variables (name, url and description) are called the members of the class. They tell you what a class is and what its properties are. They are the nouns of the class. members. A class defines what an object is, but it is not itself an object. An object is a specific instance of a class. Thus when we create a new object we say we are instantiating the object. Each class exists only once in a program, but there can be many thousands of objects that are instances of that class.
To instantiate an object in Java we use the new operator. Here's how we'd create a new web site:
website x = new website();
Once we've got a website we want to know something about it. To get at the member variables of the website we can use the . operator. Website has three member variables, name, url and description, so x has three member variables as well, x.name, x.url and x.description. We can use these just like we'd use any other String variables.

For instance:
    website x = new website();   
    x.name = "freehavaguide.com";
    x.url = "http://www.freejavaguide.com";
    x.description = "A Java Programming Website";
    System.out.println(x.name + " at " + x.url + " is " + x.description);

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More