Want to laugh? Read my n00b question..

Hi,

Ok, I found out that the a class contained in a Jar file I downloaded contains some methods I almost could use out of the box. I just need to modify one of the methods, and then I am good to go.

So, silly me I thought I could make a class of my own in my own package, that extends the class I found in the Jar. However this does not seem to work, so I guess I .. am not getting it.

I am importing the class that I am trying to extend.

If you havent died laughing of this silly problem, could you give me a hint what I am doing soo wrong and how I can solve it? Is extending classes limited to the same package? Can I only extend abstract classes or interfaces? What is it that I do not get?

/bush

[738 byte] By [PanDemica] at [2007-11-26 14:52:55]
# 1
there are a couple reasons you might not be able to extend a class:1. It is declared final2. It has declared private or protected constructors, but no public constructorsLook at the compiler error message to find out why for sure.- Adam
guitar_man_Fa at 2007-7-8 8:41:11 > top of Java-index,Java Essentials,Java Programming...
# 2

First of all, are you serious or are you kidding?

> So, silly me I thought I could make a class of my own

> in my own package, that extends the class I found in

> the Jar. However this does not seem to work, so I

> guess I .. am not getting it.

Extending the class and overriding the method *is* a way of getting the functionality you desire. I guess you should post some code to show what you are doing. Is there some error you get?

> If you havent died laughing of this silly problem,

Nothing to laugh about so far.

> could you give me a hint what I am doing soo wrong

> and how I can solve it?

> Is extending classes limited to the same package?

No such rule.

> Can I only extend abstract classes or interfaces?

No such rule.

> What is it that I do not get?

http://java.sun.com/docs/books/tutorial/

> /bush

Now that was supposed to make me laugh? ;)

aniseeda at 2007-7-8 8:41:11 > top of Java-index,Java Essentials,Java Programming...
# 3
It is not final and constructors are public. But.. you mentioning the constructors, does that mean I have to trigger the constructor of the class I am extending?
PanDemica at 2007-7-8 8:41:11 > top of Java-index,Java Essentials,Java Programming...
# 4
What is it going to take for you to post some code and the resulting compiler error? Are you holding out for duke dollar :-)?
DrLaszloJamfa at 2007-7-8 8:41:11 > top of Java-index,Java Essentials,Java Programming...
# 5
If it doesn't have a no-argument constructor yes you do, otherwise the no-arg constructor is implicitly called.
kablaira at 2007-7-8 8:41:11 > top of Java-index,Java Essentials,Java Programming...
# 6

> there are a couple reasons you might not be able to

> extend a class:

>

> 1. It is declared final

> 2. It has declared private or protected

> constructors, but no public constructors

>

> Look at the compiler error message to find out why

> for sure.

>

> - Adam

Actually, a better way to answer this question would have been to describe the requirements for you to be ABLE to extend a class:

1. The class must not have been declared final (final is a keyword that explicitly prevents you from subclassing it).

2. The class must be visible to you from the package where you attempt to subclass it from. Therefore, since you are working in a different package, the class must be declared as public.

3. Since a constructor (either explicitly or implicitly) must call a constructor of the super class, there must be at least one visible constructor to call - otherwise you can't subclass it. That one visible constructor CAN be the default constructor, though, if no constructors are actually explicitly defined.

4. Furthermore, you will want to make sure that the methods you wish to override are not final or static, and are visible from the package you are developing your class it. These don't prevent you from subclassing the class in question, but the DO make it pointless to do so.

- Adam

guitar_man_Fa at 2007-7-8 8:41:11 > top of Java-index,Java Essentials,Java Programming...
# 7

> > could you give me a hint what I am doing soo wrong

> > and how I can solve it?

> > Is extending classes limited to the same package?

>

> No such rule.

>

No, BUT, if the class is in a different package, it must have public visibility...

guitar_man_Fa at 2007-7-8 8:41:11 > top of Java-index,Java Essentials,Java Programming...
# 8

> It is not final and constructors are public. But..

> you mentioning the constructors, does that mean I

> have to trigger the constructor of the class I am

> extending?

YES! You must always invoke the constructor of the superclass. It CAN be implicit IF the superclass contains the default constructor, AND it is visible to your class. Otherwise, it MUST be explicit.

- Adam

guitar_man_Fa at 2007-7-8 8:41:11 > top of Java-index,Java Essentials,Java Programming...
# 9
Code http://paste.uni.cc/12668Wow, replies tick in faster then I manage to read. Not holding out on Duke.Catching up now.
PanDemica at 2007-7-8 8:41:11 > top of Java-index,Java Essentials,Java Programming...
# 10

> Code

> http://paste.uni.cc/12668

>

> Wow, replies tick in faster then I manage to read.

> Not holding out on Duke.

>

> Catching up now.

Okay, look, everything looks fine in YOUR code (except the absence of a call to the super constructor, which may or may not be necessary in this case).

What we really need to see are the code, java doc, code stub, or some other form of descriptor of the class you are trying to extend, AND/OR the compiler error description that prints out when you attempt to compile your class.

- Adam

guitar_man_Fa at 2007-7-8 8:41:11 > top of Java-index,Java Essentials,Java Programming...
# 11

> > > could you give me a hint what I am doing soo

> wrong

> > > and how I can solve it?

> > > Is extending classes limited to the same package?

>

> >

> > No such rule.

> >

>

> No, BUT, if the class is in a different package, it

> must have public visibility...

Yes, thanks for elaborating. :)

aniseeda at 2007-7-8 8:41:11 > top of Java-index,Java Essentials,Java Programming...
# 12
All the methods I want to use are public.Constructors are public too, but they all have parameters.Hm, why would I make an instance of the class I want to extend? I mean, triggering the constructor, does not that mean also making an instance of that class?/P
PanDemica at 2007-7-8 8:41:11 > top of Java-index,Java Essentials,Java Programming...
# 13

If by "this does not seem to work" you mean, as the other respondents seem to believe, that you have a compile-time error then post the error.

Myself, I think it means that your new class just isn't being used, and this surprises me not one whit. Unless the application you downloaded has a plugin architecture, it won't know that your class exists, let alone what you want to do with it.

YAT_Archivista at 2007-7-8 8:41:11 > top of Java-index,Java Essentials,Java Programming...
# 14
If all the constructors have parameters, you must explicitly callsuper() with valid parameters as the first line of your constructor.
es5f2000a at 2007-7-8 8:41:11 > top of Java-index,Java Essentials,Java Programming...
# 15
First constructor of the class I want to extend: http://paste.uni.cc/12669/P
PanDemica at 2007-7-21 16:20:43 > top of Java-index,Java Essentials,Java Programming...
# 16

> All the methods I want to use are public.

>

> Constructors are public too, but they all have

> parameters.

>

> Hm, why would I make an instance of the class I want

> to extend? I mean, triggering the constructor, does

> not that mean also making an instance of that class?

>

> /P

Since all of the constructors have parameters, you MUST invoke one of those explicitly in your constructor.

You will need to think it out a little while to find out which one. Pick the most reasonable one, and then invoke it as the VERY FIRST THING YOU DO in your constructor.

That is required by the java language specification.

- Adam

guitar_man_Fa at 2007-7-21 16:20:43 > top of Java-index,Java Essentials,Java Programming...
# 17

> First constructor of the class I want to extend:

> http://paste.uni.cc/12669

>

> /P

okay, wow, those are pretty unique types for the parameters of that constructor...

... the first thing that comes to my mind to ask you is: are you actually going to be using the state of this object in any way... and furthermore, do you actually understand what the state of the object is for, and why those objects are passed as parameters,

... or are you simply just trying to hijack the methods?

- Adam

guitar_man_Fa at 2007-7-21 16:20:43 > top of Java-index,Java Essentials,Java Programming...
# 18
Ok, by using the super() - but including the parameters required?For the compile error: http://paste.uni.cc/12671This error message makes a lot more sense to me now then before, thanks Adam and everyone else.
PanDemica at 2007-7-21 16:20:43 > top of Java-index,Java Essentials,Java Programming...
# 19
> Ok, by using the super() - but including the> parameters required?yes
aniseeda at 2007-7-21 16:20:43 > top of Java-index,Java Essentials,Java Programming...
# 20

> okay, wow, those are pretty unique types for the

> parameters of that constructor...

Ye, it is a very cool library for analyzing graphs.

> ... the first thing that comes to my mind to ask you

> is: are you actually going to be using the state of

> this object in any way... and furthermore, do you

> actually understand what the state of the object is

> for, and why those objects are passed as parameters,

Of course I understand why they are passed.

What do you mean "what the state of the object is for"?

> ... or are you simply just trying to hijack the

> methods?

Hijack a method? Sounds not good. What I am trying to do is change how one of the methods in this class is working. And I felt the best and most clean and honest way was to simply extend the class. Better than copying the code and change it, right?

/P

PanDemica at 2007-7-21 16:20:43 > top of Java-index,Java Essentials,Java Programming...
# 21

> Ok, by using the super() - but including the

> parameters required?

>

> For the compile error:

> http://paste.uni.cc/12671

>

> This error message makes a lot more sense to me now

> then before, thanks Adam and everyone else.

bingo. What that error message tells you is that, since you didn't explicitly invoke a super constructor, it filled in the blanks for you, and attempted to invoke super() with no parameters. Since it can't find that method, it gives you a compile-time error.

- Adam

guitar_man_Fa at 2007-7-21 16:20:43 > top of Java-index,Java Essentials,Java Programming...
# 22

> > okay, wow, those are pretty unique types for the

> > parameters of that constructor...

>

> Ye, it is a very cool library for analyzing graphs.

>

> ... the first thing that comes to my mind to ask

> you

> is: are you actually going to be using the state

> of

> this object in any way... and furthermore, do you

> actually understand what the state of the object is

> for, and why those objects are passed as

> parameters,

>

> Of course I understand why they are passed.

> What do you mean "what the state of the object is

> for"?

>

> > ... or are you simply just trying to hijack the

> > methods?

>

> Hijack a method? Sounds not good. What I am trying to

> do is change how one of the methods in this class is

> working. And I felt the best and most clean and

> honest way was to simply extend the class. Better

> than copying the code and change it, right?

>

> /P

Okay, then it sounds like what you REALLY want to do is make constructors that match the ones in the super class. i.e. make a constructor yourself that takes the same input parameters as the superclass constructor takes, and just pass those parameters back.

Like this:

public MyConstructor(ArchetypeGraph g, NumberEdgeValue nev, boolean cached)

{

super(g, nev, cached);

}

- Adam

guitar_man_Fa at 2007-7-21 16:20:43 > top of Java-index,Java Essentials,Java Programming...
# 23
Suggestion: paste your code in your posts here, like this[code]x = y + z;[/code]Rather than on another page. It makes it much simpler to follow the thread.
DrLaszloJamfa at 2007-7-21 16:20:43 > top of Java-index,Java Essentials,Java Programming...
# 24
And then as a final check; like when I extend an abstract, I expect my class to have the methods in the super class available without them being declared in my class, right?
PanDemica at 2007-7-21 16:20:43 > top of Java-index,Java Essentials,Java Programming...
# 25

> Suggestion: paste your code in your posts here, like

> this

>

> [code]

> x = y + z;

> [/code]

>

> Rather than on another page. It makes it much simpler

> to follow the thread.

Ah, ok. I will from now on. Last time I did that some guy flamed me for being a total idiot and told me to use pastbin (it was not on this forum though).

/P

PanDemica at 2007-7-21 16:20:43 > top of Java-index,Java Essentials,Java Programming...
# 26

> And then as a final check; like when I extend an

> abstract, I expect my class to have the methods in

> the super class available without them being declared

> in my class, right?

Any methods that we NOT declared private or package private will be available to you automatically. Furthermore, any methods that might have been abstract MUST be declared in your class, otherwise you will get a compiler error. However, I don't think that this will be the case today.

But for the most part, yes, the public and protected methods of that super class shoudl be available to you automatically, without needing to write implementations for them.

- Adam

guitar_man_Fa at 2007-7-21 16:20:43 > top of Java-index,Java Essentials,Java Programming...
# 27

> > Suggestion: paste your code in your posts here,

> like

> this

>

> [code]

> x = y + z;

> [/code]

>

> Rather than on another page. It makes it much

> simpler

> to follow the thread.

>

> Ah, ok. I will from now on. Last time I did that some

> guy flamed me for being a total idiot and told me to

> use pastbin (it was not on this forum though).

>

> /P

Did that other forum have syntax highlighting, and preserve code formatting? If not, I could understand why they might have prefered to you us the other site.

here, however, by using the code tags, you can perserve formatting and syntax highlighting, and it's a lot more convenient to not have to move to another website to see your code.

Just remember to use the [ code ] tags (there's a button on the posting screen that will let you do it -> it works a lot like html...

- Adam

guitar_man_Fa at 2007-7-21 16:20:43 > top of Java-index,Java Essentials,Java Programming...
# 28

> Any methods that we NOT declared private or package

> private will be available to you automatically.

> Furthermore, any methods that might have been

> abstract MUST be declared in your class, otherwise

> you will get a compiler error. However, I don't

> think that this will be the case today.

Is it possible to have abstract methods in a public class that itself is not abstract?

> But for the most part, yes, the public and protected

> methods of that super class shoudl be available to

> you automatically, without needing to write

> implementations for them.

>

> - Adam

OK, thanks Adam. Great help. And to everyone else also, thanks for input. Can I split the dukes, or do they all have to go a single person (Adam in this case)?

/P

PanDemica at 2007-7-21 16:20:43 > top of Java-index,Java Essentials,Java Programming...
# 29

> > Any methods that we NOT declared private or

> package

> > private will be available to you automatically.

> > Furthermore, any methods that might have been

> > abstract MUST be declared in your class, otherwise

> > you will get a compiler error. However, I don't

> > think that this will be the case today.

>

> Is it possible to have abstract methods in a public

> class that itself is not abstract?

>

>

> > But for the most part, yes, the public and

> protected

> > methods of that super class shoudl be available to

> > you automatically, without needing to write

> > implementations for them.

> >

> > - Adam

>

> OK, thanks Adam. Great help. And to everyone else

> also, thanks for input. Can I split the dukes, or do

> they all have to go a single person (Adam in this

> case)?

>

> /P

You can split the dukes. When you go to assign the dukes, it will ask you how many you want to assign to that person. The rest remain outstanding on the thread, so you can assign them to another helpful individual.

- Adam

guitar_man_Fa at 2007-7-21 16:20:43 > top of Java-index,Java Essentials,Java Programming...
# 30
Is it possible to have abstract methods in a public class that itself is not abstract?
PanDemica at 2007-7-21 16:20:48 > top of Java-index,Java Essentials,Java Programming...
# 31

> Is it possible to have abstract methods in a public

> class that itself is not abstract?

No. The JLS specifically prohibits this.

The reason is, for a class to be instantiable, it must have all of its methods implemented, and making a method abstract specifically prohibits you from implementing it in the same class.

Basically, at the class level, abstract prohibits instantiation, at the method level, abstract prohibits implementation.

In order for a class to be instantiable, you must implement all of the methods, but the same is NOT true the other way around. A class CAN be uninstantiable even if all of its methods are implemented.

Basically, it is a requirement that a class be abstract to contain abstract methods, but it is not a requirement that a class contain abstract methods in order for it to be, itself, abstract.

- Adam

guitar_man_Fa at 2007-7-21 16:20:48 > top of Java-index,Java Essentials,Java Programming...
# 32
And with that post from Adam, this thread has been resolved and all Dukes rewarded.Thanks everyone,/P
PanDemica at 2007-7-21 16:20:48 > top of Java-index,Java Essentials,Java Programming...
# 33
np.- Adam
guitar_man_Fa at 2007-7-21 16:20:48 > top of Java-index,Java Essentials,Java Programming...
# 34
What? You must be joking. I'm away from the forums for a day, and I see a new thread with 33 responses, and not a single personal attack or Monty Python irrelevancy? Everything on topic and the problem's solved? Good grief.
DrClapa at 2007-7-21 16:20:48 > top of Java-index,Java Essentials,Java Programming...
# 35

> What? You must be joking. I'm away from the forums

> for a day, and I see a new thread with 33 responses,

> and not a single personal attack or Monty Python

> irrelevancy? Everything on topic and the problem's

> solved? Good grief.

I know... it is rather strange...

guitar_man_Fa at 2007-7-21 16:20:48 > top of Java-index,Java Essentials,Java Programming...
# 36

> > What? You must be joking. I'm away from the forums

> > for a day, and I see a new thread with 33

> responses,

> > and not a single personal attack or Monty Python

> > irrelevancy? Everything on topic and the problem's

> > solved? Good grief.

>

> I know... it is rather strange...

What was strange? No personal attacks at all, or attacks not happening in absence of DrClap? ;-)

FUN_ONEa at 2007-7-21 16:20:48 > top of Java-index,Java Essentials,Java Programming...