2008-12-05 00:55Learning Java with EclipseOr should that be, “Learning Eclipse with Java”, as I suppose there are people out there who know Java and want to know what using Eclipse is like. There may also be people who are interested in the particular issue of class inheritance and calling objects from one class defined in the superclass, so I don’t want to limit the audience of this post too much. My main motivation for this post, however, is a desire to see Java taught right, which is an ambitious goal, I know, especially as it implies that I and everyone I know was taught incorrectly. Perhaps I should settle for “Java taught differently”, and this can be an experiment to test my hypothesis that people shouldn’t have to scale the learning curve mountain before they can write a line of Java, they should use all the shortcuts at their disposal to get to the summit, and then enjoy the ride down the other side as soon as possible. The main shortcut I can see, apart from just ignoring the formal theory behind the language until you’ve actually seen it in action, is to get Eclipse to tell you what you can do, and settle for knowing which option is right. Preparing a fileEven getting to the point where you can start typing can be quite daunting, and that’s assuming you’ve got Eclipse to run in the first place (possibly using useful advice from a forum). If Eclipse is loaded, though, follow these instructions:
As project creation goes, that probably wasn’t too strenuous, but we aren’t at a stage yet where we can actually type Java code. Continue then:
Adding your codeSo, that involved a bit of weirdness, but you should find that Eclipse has already generated a 16 line file for you, so those clicks were probably worth it. You should also see the Superclass mySuperclass = new Superclass();
mySuperclass.onlyMethod(); and remove the comment, because you’ve done what the This is where things get really exciting. You should see that Eclipse has underlined
The more negative of you might complain that now you have even more code that you don’t understand, but that’s much better than having to write a load of code you don’t understand by hand. Fortunately, the only thing left to do for this file is to replace the but that’s boring to type, so let’s have some more fun with Eclipse:
Not bad for typing just 6 letters and a couple of dots. The other fileThat’s one file (or “class”, as each file contains just one Java class) out of the way, and we only need one more for this to be a clever little Java application. Repeat the steps above again, then, to create a new class, called Subclass, in the same package, and with an initial empty Subclass mySubclass = new Subclass();
mySubclass.onlyMethod(); and get a similar angry red underlining of Now, finally, we have to understand a bit of Java, but don’t worry, it’s one of the good bits. As you might have guessed from the names, we have created a “superclass” and “subclass” as our two classes (in that order), and this sort of relationship in Java allows “child” subclasses to inherit code from their “parent” super classes. Unfortunately, even the genius of Eclipse isn’t able to work out that we want Subclass to be the subclass of Superclass, and Superclass to be the superclass of Subclass, just from the names. If it could, that would actually be annoying as we’d have to remember that special class names mean special things to Java, and Java isn’t so unpredictable. Instead, we have to explicitly declare the relationship, and it is up to the child to decide, which can be done by changing the line: public class Subclass
to: public class Subclass extends Superclass
You should be saving your work as you go (did I say that Eclipse is notoriously and shamefully crashy?) so do that now, saving the two files individually, but you will notice there is still a problem with the Here again is a good time to think about Java and its idea of inheritance. Just as some things that belong to a parent are private and can’t be seen by the child, so a superclass can have private methods which the child class doesn’t know about. One alternative is to make the method on the superclass Run it!This is only a small, example application so it can only really do two things, but let’s do them:
You may be thinking now “Err, great, but what have I just done exactly?” which isn’t really an Eclipse question. To understand, though, I should probably explain what this Finally, if you know what I mean by “arguments”, then perhaps it will make sense when I say that But what does Trackbacks
Trackback specific URI for this entry
No Trackbacks
|
QuicksearchCategoriesSyndicate This BlogBlog Administration |