eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
Resources
 ::   Tutorials
 ::   Presentations
Perl & CGI tutorials
 ::   Intro to Perl/CGI and HTML Forms
 ::   Intro to Windows Perl
 ::   Intro to Perl 5
 ::   Intro to Perl
 ::   Intro to Perl Taint mode
 ::   Sherlock Holmes and the Case of the Broken CGI Script
 ::   Writing COM Components in Perl

Java tutorials
 ::   Intro to Java
 ::   Cross Browser Java

Misc technical tutorials
 ::   Intro to The Web Application Development Environment
 ::   Introduction to XML
 ::   Intro to Web Design
 ::   Intro to Web Security
 ::   Databases for Web Developers
 ::   UNIX for Web Developers
 ::   Intro to Adobe Photoshop
 ::   Web Programming 101
 ::   Introduction to Microsoft DNA

Misc non-technical tutorials
 ::   Misc Technopreneurship Docs
 ::   What is a Webmaster?
 ::   What is the open source business model?
 ::   Technical writing
 ::   Small and mid-sized businesses on the Web

Offsite tutorials
 ::   ISAPI Perl Primer
 ::   Serving up web server basics
 ::   Introduction to Java (Parts 1 and 2) in Slovak

 

Introduction to Web Programming
Creating and Using Classes by Example  
  • So what does a class look like? Well, let's make one. However, since we have not yet gone into the depths of Java, our class will be very small. We will create an Announcer class that is solely responsible for announcing some phrase. Consider the following code (we will explain it in just a minute so don't get too caught up on the syntax or keywords):

    public class Announcer
      {
      private String _announcement;
    
      public Announcer()
        {
        _announcement = "Hello Cyberspace";
        }
    
      public Announcer(String s)
        {
        _announcement = s;
        }
    
      public void printAnnouncement()
        {
        System.out.println(_announcement);
        }
    
      public void setAnnouncement(String s)
        {
        _announcement = s;
        }
    
      public String getAnnouncement()
        {
        return _announcement;
        }
      }
    

  • Okay, so believe it or not, in 23 lines of code we have demonstrated the creation of an object, the creation of a public API, encapsulation, and polymorphism (we'll show an example of inheritance later). See how easy Java makes object-oriented programming!

  • Okay, let's take this code apart and see how we achieved out object-oriented goal.

Access Specifiers

  • So the first thing you see in this class is the word "public". A couple of lines down, you also see the word "private". What do these keywords mean?

  • Well, the words private, public, and protected (which we did not use in the example above), are used to define scope.

  • The scope of a class, method or property defines who is allowed access to it. A "public" scope for example, means that anyone is allowed access. A "private" scope on the other hand means that only the object itself has access. A "protected" scope means that only objects derived from the class can have access. Finally, if you do not define a scope at all (usually called "friendly"), then all classes in the same package will have access.

  • Thus, in our example above, any object in the object space has access to the Announcer class and can use it in order to create Announcer objects. Everyone also has access to use the printAnnouncement() method in order to tell the Announcer object to print out the message. However, only an Announcer object itself has access to the actual content of the announcement that is stored in the _announcement property. Of course, outside objects can request that the announcement be changed using the get and set API provided by the Announcer class as well.

  • The benefit of scope or course, is that it "enforces" encapsulation. Programmers are strongly pushed to hide their objets' data and provide an API such as setAnnouncement().

  • Essentially, the "private" keywords constructs the walls of the black box that are so crucial for object oriented design.

Defining a Class

  • So right after the "public" keyword, we see the "class" keyword. The class keyword is used to specify that the following statement block defines a class. The class can be used to instantiate an object that is defined by the class. In our example case, we are defining a class called "Announcer".

  • By the way, a class is usually defined in a ".java" file that is compiled into bytecode by a compiler into a ".class" file. Typically, your .java and .class filenames will be equivalent to the class name. Thus, the Announcer class would be stored in a file called Announcer.java and be compiled into a file named Announcer.class.

  • Once the class is specified, the class is defined. To define a class you'll remember, you simply define its properties and methods. In our case, we have one property called _announcements and several methods including: Announcer(), Announcer(), setAnnouncement(), getAnnouncement(), and printAnnouncement()

Construction

  • Hey wait a minute...what is the Announcer() method and why are there two of them?

  • Well, the Announcer() method is a very special method called a "Constructor". The constructor method, that always has the same name as the class file, is used to "construct" an actual object out of the class. When an object is instantiated this method is called to initialize the object. Once it is called, it will never be called again however. So you should only out initialization code here

  • So why are there two of them? Well this is an example of polymorphism. The difference between the two versions of Announcer() is that one of them takes no arguments and the other one takes a single String as an argument. In the first case, an Announcer object will be created with the announcement of "Hello Cyberspace!" and in the second case, it will be created with some other phrase determined by the object that instantiated the Announcer!

Developing an API

  • After the definition of the constructors, you'll notice the methods printAnnouncement(), getAnnouncement() and setAnnouncement(). These methods represent the public API of this class and can be called upon by other objects. They allow other objects in the "object space" to work with the Announcer object. In the case of the printAnnouncement and setAnnouncement() outside objects are given the ability to ask the Announcer object to do things. In the case of the getAnnouncement, outside objects are able to ask the Announcer for some piece of data. As you can see, if a method is required to return some value, the value of the return type is specified in the definition of the method and the method is concluded by returning a value of that type. In this case, the getAnnouncement() method returns the string contained in the variable _announcement.

Instantiating and Using an Object

  • So where are these other objects and how is an Announcer object actually "instantiated"?

  • To instantiate an object, Java uses the keyword "new". Consider the following class called Test that instantiates an Announcer and then uses its public API to print the announcement.

    public class Test
      {
      public static void main(String[] args)
        {
        Announcer a = new Announcer();
        a.printAnnouncement();
        }
      }
    

  • We won't yet focus on the syntax of the Test class. Instead we will just focus on how the Test class utilizes the Announcer class. Notice that the Test class use the new keyword to instantiate an Announcer object and then calls the printAnnouncement() method on the Announcer object using the "dot notation".

  • The dot notation is used to access properties or methods in an object. In this case, we have an Announcer object named "a" and we will access the printAnnouncement() method in a.

  • It is crucial that you specify the object that you want to utilize because it is very possible that there may be more than one Announcer object alive at any given time. Thus, you need to specify which Announcer object you want to access the printAnnouncement method on. Consider the following case in which we apply all of what we just learned about classes. In this example we will create three Announcer objects, two using the default constructor and one using the alternate constructor. We will then use the setAnnouncement API method to change the announcement of the second object and then have each Announcer object print out its announcement using the dot notation

    public class Test
    
      {
      public static void main(String[] args)
        {
        Announcer a = new Announcer();
        Announcer b = new Announcer();
        Announcer c = new Announcer("Hello Sol");
    
        b.setAnnouncement("Hello World");
    
        a.printAnnouncement(); // prints Hello
    			   //        Cyberspace
        b.printAnnouncement(); // prints Hello World
        c.printAnnouncement(); // prints Hello Sol
        }
      }
    

Previous Page | Next Page