The i-Technology Media!
Register | Log in
   
 
.NET  ·  AJAX  ·  CLOUD  ·  ECLIPSE  ·  FLEX  ·  OPEN WEB  ·  iPHONE  ·  JAVA  ·  LINUX  ·  OPEN SOURCE  ·  ORACLE  ·  PBDJ  ·  SEARCH  ·  SILVERLIGHT  ·  SOA  ·  VIRTUALIZATION  ·  WEB 2.0  ·  WIRELESS  ·  XML
Comments
Drool, Britannia? Is the UK Failing the Cloud?
By Roger Strukhoff
Richard Davies wrote: The UK has a good crop of technology pioneers in cloud computing - for example ElasticHosts, FlexiScale, Flexiant, OnApp - and also some strong government initiatives such as G-Cloud. We will have to see whether this kind of technical leadership converts into swift mass-market adoption or not.
Jan. 8, 2012 11:38 AM EST
read more & respond »
Cloud Expo on Google News
Did you read today's front page stories & breaking news?

Cloud Expo & Virtualization 2011 West
Keynotes
Oracle
Opening Keynote | An Enterprise Cloud for Business-Critical Applications
Abiquo
Day 2 Keynote | The Enterprise Cloud Tightrope - Balancing for Success
Akamai
Day 3 Keynote | The DNA of an Enterprise Cloud
DIAMOND SPONSOR:
Oracle
Many Clouds, Many Choices'Cloud
PLATINUM PLUS SPONSORS:
Abiquo
Enterprise Cloud Best Practices - Town Hall - Join the discussion…
PLATINUM SPONSORS:
Intel
Progressing Toward the Federated, Automated and Client-Aware Cloud
New Relic
How to build an app with Twitter-like throughput
Rackspace
Computing in the Cloud Era
GOLD SPONSORS:
Gale Technologies
Practical Cloud Migration
IBM
Re-think IT. Re-inventing Business.
Intel/McAfee
Identity Driven Security in the Cloud
PerspecSys
Hackers Hackers Everywhere, Is My Public Cloud That Safe?
Red Hat
Unlock the Value of the Cloud
SHI
Mission Critical Applications and the Cloud - Myth or Reality?
SoftLayer
Not Your Grandpa's Cloud
Terremark
Integrating Enterprise Clouds
VMware
Upgrade to a vCloud
POWER PANELS:
Cloud Expo Silicon Valley: CTO Power Panel
Cloud Expo Silicon Valley: CEO Power Panel
Cloud Expo Silicon Valley: Cloud SuperStars Panel
Cloud Expo Silicon Valley: CloudNOW Panel
Click For 2010 West
Event Webcasts
Cloud Expo & Virtualization 2011 East
DIAMOND SPONSOR:
Dell
Dell & VMware Deliver the Enterprise Hybrid Cloud
PLATINUM PLUS SPONSORS:
Abiquo
Are Financial Services Organizations Risking Security by Avoiding Cloud Computing?
Oracle
From Consolidation to Enterprise Private PaaS
PLATINUM SPONSORS:
Intel
Driving the Transformation to Next Generation Cloud Data Centers
Rackspace
The Inevitability of an Open Cloud
GOLD SPONSORS:
CA Technologies
Follow YOUR path to Cloud Computing
Interxion
Who Keeps the Cloud in the Air?
Microsoft
Patterns for Cloud Computing
PerspecSys
War in the Clouds: Are you ready?
ServiceMesh
The Big Win: Stop Playing Small-Ball with Your Cloud Strategy
Terremark
Evaluating Enterprise Clouds
Xiotech
Cloud Storage: Myths and Realities
POWER PANELS:
Cloud Expo New York: CTO Power Panel
Cloud Expo New York: CEO Power Panel
Cloud Expo New York: CMO Power Panel
Cloud Expo New York: Wrap-Up Power Panel
Click For 2010 West
Event Webcasts
Live Google News by SYS-CON!
Top Three Links You Must Click On


Java SE 6
Java Design
Java Design

By: Mike Barlotta
Jun. 1, 2002 12:00 AM

Java classes should be designed to enhance their reusability and flexibility. Coding to an object type rather than an implementation by using interfaces or abstract classes can help us achieve both flexibility and reusability.

What Is an Abstract Class?
Everything in Java is an object, so to write Java programs we need to define classes. A Java class in turn defines the type and the available methods for that type. It also provides the implementation of the methods that it defines. This kind of class is called a concrete class. An abstract class, defined in section 8.1.1.1 of the Java Language Specification (JLS), defines the type and the available methods but is not required to implement any of the methods that it defines.

The following code shows a simple abstract class named Base.

public abstract class Base { public abstract String f1(); public String f2(String s) { return "Base.fs("+s+")"; } }

An abstract class is denoted by the keyword abstract in the class declaration and usually contains one or more abstract methods. An abstract method declaration defines the method name, number and type of parameters, return type, and throws clause, but does not have an implementation. In the declaration an abstract method is denoted by the abstract keyword, as is the case with method f1 in the class Base defined earlier.

Why design with an abstract class instead of a concrete class? Using an abstract class allows you to define the interface of a type and still provide method implementations without having to implement the entire set of methods. The term interface is used here to mean the publicly accessible functions on the class, not the Java interface, which we'll look at next. The abstract class represents a type at a very generic level. Although operations can be defined for the type, the abstract class can't provide all the functionality without becoming too specific.

An abstract class is incomplete and relies on its subclasses to complete the implementation. In the following code sample the class X inherits from the class Base by extending it. The class Base is considered the superclass of class X. The class X is considered the subclass (or subtype) of class Base. For X to avoid being declared abstract, it must implement the abstract method f1 defined in the class Base. The definition of class X is listed below:

public class X extends Base{ public String f1() { return "X.f1()"; } public String f3() { return "X.f3()"; } }

Some important points to remember about abstract classes:

  •    An abstract class cannot be instantiated.
  •    An abstract class can implement zero, one, or more methods.
  •    A class with an abstract method must be declared abstract.
  •    A class can be declared abstract even if it has no abstract methods.
  •    Methods that are static, private, and/or final cannot be abstract.

What Is an Interface?
A Java interface defines a type and the available methods for that type. It does not, and, in fact, cannot provide an implementation for any of the methods it defines. An interface is defined in section 9 of the Java Language Specification.

The following is a simple interface, iBasicType:

public interface iBasicType { public String f1(); public String f4(); }

The interface and all the methods on it are automatically considered abstract and do not require the abstract keyword.

To be of any use a Java interface must be implemented by a Java class. In the following code sample, the class Basic implements the interface iBasicType. The class Basic must provide an implementation for every method in the interface, otherwise it must be declared an abstract class.

public class Basic implements iBasicType { public String f1() { return "Basic.f1()"; } public String f4() { return "Basic.f4()"; } public String f2(String s) { return "Basic.fs("+s+")"; } }

A class implementing an interface can also declare additional methods that aren't part of the interface. This is shown in the Basic class definition by defining method f2, which isn't part of the iBasicType interface.

Some important points to remember:

  •    An interface cannot be instantiated.
  •    An interface has no implementation.
  •    All the methods on an interface are considered public and abstract.
  •    All the fields on an interface are considered public, static, and final.
  •    An interface can extend other interfaces.
  •    A class can implement one or more interfaces.

    The Power of Dealing with Types
    Java is a strongly typed language, so every object in Java has a type. The type associated with any particular object is its class name. For example, when the class Customer is instantiated, the object is said to be of the type Customer. Each instance of an object may have a different state, but all objects of the same type have the same set of methods because they're created from the same class. Abstract classes and interfaces give us more flexibility in our programs because they allow us to look at an object as a different type instead of just using its class name.

    For example, the object Y, shown in Figure 1, can be created from the following line of code:

    Y myYObj = new Y();

    The variable myYObj contains an object reference of type Y that points to a Y object in memory after this code is executed. Through the Y object reference all the methods defined on the Y object and any class it extends can be invoked. Since Y extends the class X, they're related and we can treat the subclass Y as if it were actually any one of its superclasses, in this case X or Base; e.g., the following code is completely valid:

    X myXObj = myYObj;

    The variable myXObj contains an object reference of type X, but the reference points to a Y object in memory after this line of code is executed. This allows us to look at the Y object as if it were really of type X. The compiler can safely place references to subclasses into a variable that's declared to be a superclass of that subclass, since the subclass is guaranteed to contain all the methods and variables that can be called on the superclass. Simply, Y extends X, so the compiler is assured that any method that can be called on X can also be called on Y. However, the reverse is not guaranteed to be true so the following code is not valid:

    Y anotherYObj = new X(); // This is not valid

    Dealing with a subclass object as if it were really one of its superclasses is called upcasting or narrowing. It's important to understand that the object reference, which points to an object in memory, does not determine the type of the object that gets the request to invoke a method, nor does it change the type of the object that was instantiated. The object reference does, however, affect how the object it points to can be operated on. The object reference defines the type through which the calling program sees the object in memory. You can think of the X object reference as a filter that allows only calls to methods that are defined in the X class and any class it extends. Looking at the class diagram in Figure 1, we can call the f1, f2, and f3 methods through the X object reference. But we couldn't call the f4 method even though this is a valid method on the Y object, because it's not defined by the class X or any of its superclasses.

    When we call methods through the X object reference using the myXObj variable, we're actually invoking methods in the object that the reference points to in memory. In this case it's a Y object instance.

    myXObj.f1(); // returns "Y.f1()" myXObj.f3(); // returns "X.f3()"

    These method calls give the results shown in the comment sections based on the following definition of the Y class:

    public class Y extends X implements iBasicType { public String f1() { return "Y.f1()"; } public String f2(String s) { return "Y.fs("+s+")"; } public String f4() { return "Y.f4()"; } }

    Notice that the implementation of the f1 method on the Y class is run even though we're using an X object reference. In this example, the Y class does not define its own f3 method, so the implementation of the f3 method on the X class is executed.

    It's possible to point an object reference to an object that is its subclass because Java performs dynamic binding. Objects communicate by sending messages to each other through references. This is basically a means of calling methods on the object. Dynamic binding allows messages to be sent to an object without knowing the exact type of the object that the message will be sent to when the code is written. When the code is executed and a method is invoked, the JVM determines which method is actually executed based on which object is being pointed to by the object reference at runtime.

    The message to execute a particular method cannot be sent to just any object. Java is a strongly typed language so the compiler makes sure the assignment is type-safe. A variable has a type that never changes, so the myXObj variable will always hold an X object reference. When the variable type is a reference type (arrays, classes, and interfaces), it can point to different objects that are related via inheritance or an interface. Specifically, a reference variable can be assigned a reference to any object whose class is:

    •    Of the same type as that of the reference variable declaration
    •    A subtype of the type that the reference variable is declared to be Or any object that implements:
    •    The same interface that the reference variable is declared to be
    •    An interface that is a subtype of the interface that the reference variable is declared to be

      An Example with an Interface

      We can declare variables to have a type that is defined as an abstract class or an interface even though they can't be instantiated. Given the class hierarchy shown in Figure 1, a variable can be declared as follows:

      iBasicType myBasicObj;

      The variable, myBasicObj, holds an object reference to the interface iBasicType. We can instantiate objects that implement the interface using the code sample below:

      iBasicType myBasicObj = new Basic();

      The variable, myBasicObj, holds an iBasicType reference that points to a Basic object in memory. Since the class Y also implements the interface iBasicType, the following code is also valid:

      iBasicType anotherBasicObj = new Y();

      By implementing an interface, the object is also declaring that it is of that type. For example, the class Y can be said to be of type Y, its class name, as well as of type iBasicType, since it implements that interface.

      Like the variable that holds an object reference to a class type, an object reference to an interface type defines the way the calling program sees the object in memory. So we can call the f1 and f4 methods through the iBasicType object reference. But we could not call the f2 method even though this is a valid method on both the Basic and the Y object, because it's not defined by the interface (see Figure 1).

      Wrapping It Up

      Hopefully, you'll begin to see the potential flexibility we can design into our applications by coding to different types. Applying this in a practical sense will be the focus of our next article, until then, happy coding.

      Published Jun. 1, 2002— Reads 14,840 — Feedback 2
      Copyright © 2002 SYS-CON Media, Inc. — All Rights Reserved.
      Syndicated stories and blog feeds, all rights reserved by the author.
      Related Links
      ▪ Figure 1
      About Mike Barlotta
      Background Information: Michael Barlotta is the Director of Technology at AEGIS.net Inc (www.AEGIS.net). Mike is a Sun Certified Java Programmer and is recognized as an expert on the Sybase application server EAServer (Jaguar CTS), mentoring clients and speaking at conferences on the topic. He is the author of several books including Taming Jaguar and Jaguar Development with PowerBuilder 7 (both by Manning Publications). Be sure to check out Mike’s Web site www.TamingJaguar.com.

Add Your Feedback

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

#2
Richard Nunez commented on 12 Nov 2002

check this out for JUG

#1
K.G.Beech commented on 9 Sep 2002

Please send any references to tutorials on Java for beginners.


Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers

ADS BY GOOGLE

Breaking Java News
U.S. Census Bureau Daily Feature for February 18
U.S. Census Bureau Black History Month Feature for February 18
Following Is a Test Release
President of the Independent Libya Foundation Announces Vision Statement for Libya on the Anniversary of the Libyan Revolution
Former Congressman and Dukes of Hazzard Star Ben Jones Blasts Nascar Decision
CFIA/Amended Allergy Alert: Undeclared Milk in Certain Sweets From the Earth Brand Cakes
Connection Engine Launches Buyer Score to Provide Instant Predictions of Email Subscriber Value Segment Customers by ROI Potential and Identify High Value Customers

ADVERTISE   |   MAGAZINE SUBSCRIPTIONS   |   FREE BREAKING-NEWSLETTERS!   |   SYS-CON.TV   |   BLOG-N-PLAY!   |   WEBCAST   |   EDUCATION   |   RESEARCH

.NET Developer's Journal - .NETDJ   |   ColdFusion Developer's Journal - CFDJ   |   Eclipse Developer's Journal - EDJ   |   Enterprise Open Source Magazine - EOS
Open Web Developer's Journal - OPENWEB   |   iPhone Developer's Journal - iPHONE   |   Virtualization - Virtualization   |   Java Developer's Journal - JDJ   |   Linux.SYS-CON.com
PowerBuilder Developer's Journal - PBDJ   |   SEO / SEM Journal - SJ   |   SOAWorld Magazine - SOAWM   |   IT Solutions Guide - ITSG   |   Symbian Developer's Journal - SDJ
WebLogic Developer's Journal - WLDJ   |   WebSphere Journal - WJ   |   Wireless Business & Technology - WBT   |   XML-Journal - XMLJ   |   Internet Video - iTV
Flex Developer's Journal - Flex   |   AJAXWorld Magazine - AWM   |   Silverlight Developer's Journal - SLDJ   |   PHP.SYS-CON.com   |   Web 2.0 Journal - WEB2
Apache   |   CMS   |   CRM   |   HP   |   Oracle Journal   |   Perl   |   Python   |   Red Hat   |   Ruby on Rails   |   SAP   |   SaaS

SYS-CON MEDIA:   ABOUT US   |   CONTACT US   |   COMPANY NEWS   |   CAREERS   |   SITE MAP
SYS-CON EVENTS:   |  AJAXWorld Conference & Expo  |  iPhone Developer Summit  |  Cloud Computing Conference & Expo  |  SOA World Conference & Expo  |  Virtualization Conference & Expo
INTERNATIONAL SITES:   India  |  U.K.  |  Canada  |  Germany  |  France  |  Australia  |  Italy  |  Spain  |  Netherlands  |  Brazil  |  Belgium
 Terms of Use & Our Privacy Statement     About Newsfeeds / Video Feeds
Copyright ©1994-2008 SYS-CON Publications, Inc. All Rights Reserved. All marks are trademarks of SYS-CON Media.
Reproduction in whole or in part in any form or medium without express written permission of SYS-CON Publications, Inc. is prohibited.
 
close this window