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 / April 2006

Tip: Looking for answers? Try searching our database.

Class Extension que

Thread view: 
as4532@drexel.edu - 16 Apr 2006 21:59 GMT
Hi all,
 I have a newbie question about extending classes in Java. I have 2
files File1.java and File2.java. File1.java is the base class and
File2.java extends it.

//File2.java

public class File2 extends File1 {
....

}

Both files are in the same folder in my C:\. I get the error saying
Cannot find symbol File1 when i try to compile File2.java. What should
i do here to use an existing class in another java program when both
.java files are present in the same folder.

Thanks
A
Mark Thomas - 16 Apr 2006 22:49 GMT
> Hi all,
>   I have a newbie question about extending classes in Java. I have 2
[quoted text clipped - 15 lines]
> Thanks
> A

Has File1.java compiled cleanly?
Note that these are not separate java programs - just two java classes.
 Even calling them Files tends to be misleading.

Mark
as4532@drexel.edu - 16 Apr 2006 22:54 GMT
Hi Mark,
  Yes File1.java compiles. Only File2.java throws an error saying-
"Cannot find the symbol" against File1.

Thanks

> > Hi all,
> >   I have a newbie question about extending classes in Java. I have 2
[quoted text clipped - 21 lines]
>
> Mark
Mark Thomas - 16 Apr 2006 23:26 GMT
> Hi Mark,
>    Yes File1.java compiles. Only File2.java throws an error saying-
[quoted text clipped - 27 lines]
>>
>> Mark

Post the full source - there's not enough information here.

Mark
as4532@drexel.edu - 17 Apr 2006 00:02 GMT
Mark, Here is the source. Customer.java compiles while
CustomerTest.java doesn't. Thanks

//Customer.java

public class Customer{
    private String fname, lname;
    public static void main(String[] args){
    }

    public Customer(String firstname, String lastname){
    this.fname=firstname;
    this.lname=lastname;
    }
}

//CustomerTest.java

public class CustomerTest extends Customer{
    public static void main(String[] args){
    Customer p=new Customer("fname","lname");
    }
}

> > Hi Mark,
> >    Yes File1.java compiles. Only File2.java throws an error saying-
[quoted text clipped - 31 lines]
>
> Mark
Mark Thomas - 17 Apr 2006 12:06 GMT
> Mark, Here is the source. Customer.java compiles while
> CustomerTest.java doesn't. Thanks
[quoted text clipped - 54 lines]
>>
>> Mark

Ah!  You misquoted the error message - it doesn't say "Cannot find
symbol File1 (or Customer)"  rather "cannot find symbol : constructor
Customer()".  Your CustomerTest class doesn't define a constructor -
when that happens, Java provides you with a default no-argument
constructor.  Any constructor always calls the corresponding constructor
in its superclass, in this case that would be a no-argument constructor
in Customer - and you haven't got one!  Hence the error message.  Note
that as soon as you provide a constructor for a class, as you have for
Customer, you lose the default constructor.

Here the is no reason for CustomerTest to extend Customer.  It is not a
subclass (there is no way that a CustomerTest is-a-kind-of Customer),
but a class that uses Customer.  So remove the 'extends Customer' and
all will be well.

Always give the actual error message that is causing you problems -
having read it first!

Mark
as4532@drexel.edu - 17 Apr 2006 12:38 GMT
Thanks for the reply Mark. I removed the word "extends". Now this is
the  error message i am getting:

CustomerTest.java:11: cannot find symbol
symbol:   class Customer
Location:   class CustomerTest

   Customer p=new Customer("fname","lname")

What i need to know is that how can i use the class which is in a
different .java file but same directory as my current .java file.

> > Mark, Here is the source. Customer.java compiles while
> > CustomerTest.java doesn't. Thanks
[quoted text clipped - 74 lines]
>
> Mark
Venkatesh - 17 Apr 2006 14:26 GMT
U need to either set your classpath properly: Include "current
directory" in your classpath by saying
        SET CLASSPATH=%CLASSPATH%;. in windows
or
        CLASSPATH=$CLASSPATH:. in unix machines

Then run javac

Otherwise, u can compile both java files together, use
javac Customer.java CustomerTest.java
as4532@drexel.edu - 17 Apr 2006 14:46 GMT
Hi Venkatesh,
    Thanks a lot for the help. Classpath was the issue.. when i
compiled the 2 classes together it works fine. Thanks again!

A
> U need to either set your classpath properly: Include "current
> directory" in your classpath by saying
[quoted text clipped - 6 lines]
> Otherwise, u can compile both java files together, use
> javac Customer.java CustomerTest.java


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.