Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

casting class to interface

Thread view: 
Mark - 13 Oct 2006 02:19 GMT
I get an exception ("ClassCastException") when I try to cast Resource
to Comparable2... any idea why?

// Resource.java

class Resource
{

    private String title, firstName, lastName, publisher, dateOfEdition,
ISBN, libraryCallNumber;
    private int numberOfPages, editionNumber;

    Resource(String title,String ISBN,String libraryCallNumber,String
publisher, String firstName,
        String lastName, int editionNumber,String dateOfEdition,int
numberOfPages)
    {
        this.title                 = title;
        this.ISBN                 = ISBN;
        this.libraryCallNumber     = libraryCallNumber;
        this.publisher            = publisher;
        this.firstName            = firstName;
        this.lastName            = lastName;
        this.editionNumber        = editionNumber;
        this.dateOfEdition         = dateOfEdition;
        this.numberOfPages        = numberOfPages;
    }

    public String toStringLong()
    {
        return "    Title:                  " + title + "\n"
            + "    ISBN:                   " + ISBN + "\n"
            + "    Library Call Number:    " + libraryCallNumber + "\n"
            + "    Publisher:              " + publisher + "\n"
            + "    Author:                 " + firstName + " " + lastName +
"\n"
            + "    Edition Number:         " + editionNumber + "\n"
            + "    Date of Edition:        " + dateOfEdition + "\n"
            + "    Number of Pages:        " + numberOfPages + "\n";
    }

    public String toString() {
        return title + " - " + firstName + " " + lastName;
    }

    public String toStringEncoded() {
        final String glue = ";";
        return title + glue + ISBN + glue + libraryCallNumber + glue +
publisher + glue + firstName + glue
            + lastName + glue + editionNumber + glue + dateOfEdition + glue +
numberOfPages + "\n";
    }

    public String toString1() {
        return lastName + firstName + title + editionNumber + publisher;
    }

    public String toString2() {
        return title + editionNumber + publisher + lastName + firstName;
    }

    public int compareTo1(Comparable2 obj) {
        return toString1().compareToIgnoreCase(obj.toString1());
    }

    public int compareTo2(Comparable2 obj) {
        return toString2().compareToIgnoreCase(obj.toString2());
    }
}

// Comparable2.java

public interface Comparable2
{
    public String toString1();
    public String toString2();

    public int compareTo1( Comparable2 obj );
    public int compareTo2( Comparable2 obj );

}
NeoGeoSNK - 13 Oct 2006 02:32 GMT
> I get an exception ("ClassCastException") when I try to cast Resource
> to Comparable2... any idea why?
[quoted text clipped - 77 lines]
>
> }

That is because "Resource is not a Comparable2"
please declare Resource like this:
class Resource implements Comparable2{
}
Arne Vajhøj - 13 Oct 2006 02:33 GMT
> I get an exception ("ClassCastException") when I try to cast Resource
> to Comparable2... any idea why?

> class Resource
> {

The class does not implement Comparable2 !?

Arne
Mark - 13 Oct 2006 05:00 GMT
> > I get an exception ("ClassCastException") when I try to cast Resource
> > to Comparable2... any idea why?
[quoted text clipped - 5 lines]
>
> Arne

lol!! thank you so much. i'm new to this "implements" stuff. i'm more
of a c++ programmer. such an easy fix..thank you!
Stefan Ram - 13 Oct 2006 02:41 GMT
>I get an exception ("ClassCastException") when I try to cast
>Resource to Comparable2... any idea why?
>class Resource

 There is no »implements Comparable2« here.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.