1

I just started to learn Java and tried to draw some rectangles.

I have run this example and can't understand one thing: class ShapesDemo2D has a constructor:

public void init() {
    //Initialize drawing colors
    setBackground(bg);
    setForeground(fg);
}

I understand that setBackground is a method.

  • But how can it be called without a class or instance reference?

This is a method of an abstract Component class in Component.java file.

  • Shouldn't it be like Component.setBackground(bg) or componentInstance.setBackground(bg)?

But Component is an abstract class, so it can't be instantiated, and it's methods can not be called.

  • So how it is possible to call this method?

1 Answer 1

3

You're forgetting ShapesDemo2D extends JApplet at the top of the class code which means that the class extends another class, JApplet, and so the code in the method above above is effectively calling super.setBackground(bg); and super.setForeground(fg);. The super class, JApplet has these methods since as per the JApplet API, the class extends from Component which has these methods, meaning that ShapesDemo2D will have inherited the methods as well.

Having said this, please ditch this tutorial since applets are no longer support, are dead technology and have been dead for the longest time, and there is no sense learning a dead technology.


Side note, you state:

class ShapesDemo2D has a constructor:

public void init() {
    //Initialize drawing colors
    setBackground(bg);
    setForeground(fg);
}

But this is in fact not a constructor but rather a method. A constructor for the ShapesDemo2D class would look like:

public ShapesDemo2D() {
    // .....
}

This may seem like a pedantic distinction, but programming is all about being as accurate as possible with your thinking and your code. The Java compiler is strict and unforgiving, and so you must be as well.

Sign up to request clarification or add additional context in comments.

4 Comments

@prupru: you're welcome. Please see addendum
init() made me think it is the constructor. In python double under init works as constructor.
@prupru: I don't know Python all that well, but in Java, constructors must be declared with the same name as the class, capitalization and all, and must not declare a return type. The public void init() method is also inherited from JApplet (from its parent class, Applet) and is required to be called to initiate applets. I know a lot about this dead technology, and am in fact, almost an expert in useless information on this now dead subject.
Right now learning Java feels to me like solving puzzles in an ancient crypt, where you have to push unknown sybmols on the walls in order to deactivate traps. Looks like I have pushed a wrong one with JApplet, but thanks to your help didn't fall into the pit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.