Why do we need to extend the JFrame class when building a Swing application. As far as I know extends is used for inheriting the base class. None o
The first thing to note in your code is this:
super("The title");
This actually calls the JFrame constructor, and passed it "The title" as a title String. This is an explicit example of using the Jframe functionality in your code. This will build the window that appears for you.
Using methods like add are all inherited from the JFrame class. These add Components to the JFrame object.
Why Inheritance?
Well, simply, your class IS a JFrame, with a little more. When you have a Is A operation, you use inheritance. The other advantage of this method is that your class can be referred to as a JFrame. That is:
JFrame tuna = new tuna();
// Note: All classes are meant to start with a capital letter.
Another viewpoint
It's important to note that you don't strictly HAVE TO inherit from a JFrame class. You can use Composition. In this instance you'd have something like:
public class Tuna {
private JFrame parentWindow;
// Rest of class.
}
As mentioned above, the convention is to follow the Is A and Has A approach. If class A Is an example of class B, we tend to use inheritance. If class A has an instance of class B, then you use composition, although in most cases, inheritance is interchangeable with Composition.
Another another Viewpoint
As mentioned in the comments, you should always look for an existing API that offers this kind of functionality, before attempting to implement it yourself.