We are about to switch to a new forum software. Until then we have removed the registration on this forum.
i have 2 classes. one is an extends. how can i move the superclass' method "move()" to the subclass
Edit: I have used super but it doesn't transfer the method and sits still.
Answers
Keyword
extends
already transfers all parents class' members to its "heir" subclass.If method move() belongs to the class being extended, it's gonna be accessed by its now subclass too.
@gotoloop im confused. the subclass doesn't need the move method defined?
An important tip: Do not use
/*
. Rather use/**
in order to avoid all green posts! :-&Since you've finally posted your code now, I can see that you've gone beyond simple inheritance, but it's also using polymorphism on both show() & move() methods!
That is, you
@Override
both inherited methods by re-implementing them in subclasses OddballParticle & JumboParticle: http://docs.Oracle.com/javase/8/docs/api/java/lang/Override.htmlSeems like both polymorphed move() methods' implementations are exactly the same as the original inherited version. You can simply erase them from both subclasses, as they're redundant. :-\"
Now both polymorphic method show() have diff. implementations from the original 1.
So you should keep them. But it's a good advise to annotate them w/
@ Override
: O:-)@ Override void show() {}
However I see a probable bug that you're also re-declaring inherited fields! :-SS
They already exist in the original parent class NormalParticle.
You're simply shadowing them inside your subclasses! #-o
That is, w/ such configuration, there are, for example, 2 fields x: this.x & super.x! ~:>
When dealing w/
extends
&implements
we gotta pay attention to what already exists in the whole inheritance chain lest we end up w/ buggy redundant behavior! =;@gotoloop thank you very much for your explanation. however, after erasing both move methods from the subclasses, they no longer move...
Have you also removed your field redeclarations???
What may be happening is that Java doesn't know which 1 to use! 8-}
While methods are overridden, fields are overshadowed instead! L-)
If a method happens to be
static
, it's also overshadowed when re-implemented inside a subclass! 8-Xi just removed them from the subclasses.... what should i have done?
Post your update code so we can see how it is going now...
i removed the move methods and that didn't work. What do I do about the move methods?
@gotoloop what do you want me to do with the move methods exactly?
Your code post is half green! :@) And it seems like you've completely skipped what I had already warned about @: https://forum.Processing.org/two/discussion/comment/60953/#Comment_60953
I suggest you to re-read what you had marked as answer: https://forum.Processing.org/two/discussion/14779/#Comment_60951
well, first of all maybe you don't need a parents class' and "heir" subclass. For smaller projects, it's ok to have just normal classes. When they are similar in some ways, who cares?
Then, you could look at
interface
which is useful when you want to have different classes in oneArrayList
: https://forum.processing.org/two/discussion/4551/arraylist-with-different-object-types#latestAnd then you can look at
extends
- the help is pretty straight forward :https://processing.org/reference/extends.html
So class B builds upon A. Therefore everything that is in A automatically is in B and can be used that way.
Here is an example: A is class Dot, B is DrawDot :
as you can see, although dd1 is of class DrawDot it can use
display()
from class Dot as ifdisplay()
were part of DrawDot. So extends copies all methods from Dot into DrawDot if you want to put it that way.Best, Chrisir ;-)
(i have fixed the comments)
@chrisir @gotoloop I'm still confused. I have tried using the super.move() method in a sub class. it is not working...
for instance,
what I found, after executing, it does not do the forced x,y positions. but the x,y vars are private so are not inherited. what should I change about this to use move() @gotoloop ?
The 3 Java access keywords:
private
,protected
&public
, don't have any effect on inheritance.I've already told you that re-declaration is über bad.
When extending from some class, pay close attention about what's already in there.
For example, if field called xspeed exists in the parent class, why declaring another 1 in the subclass? It's already being inherited after all!
In short, inheritance demands careful planning. :-B
@gotoloop what should I do to improve the above code? I am not understanding.
extends
that automatically inherits all of its members.http://docs.Oracle.com/javase/tutorial/java/concepts/index.html
http://docs.Oracle.com/javase/tutorial/java/javaOO/index.html
this is homework
see lines 137-139 e.g.
maybe just speak to your teacher
@chrisir @gotoloop that is the issue, she doesn't offer help I solved most of it but, I have a new issue a null pointer exception
Please don't post a question in multiple places - it splits the answers up and causes extra work.
Please clear your code of old code lines
Also, /** seems not to work - replace with /*
Which like causes the error? What does it say?
https://forum.Processing.org/two/discussion/15830/code-correction-please-i-get-an-error