• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between extending JFrame and just creating one in the class?  RSS feed

 
Marko Matosevic
Greenhorn
Posts: 12
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between extending JFrame in one class and simply constructing a new JFrame object in that same class? What benefits do I have with each solution, providing I want to use that class to create the GUI. Is it the same or are there differences rather than not having to reference to a new JFrame to be able to use its functions?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The BIG difference is that if your clas is-a JFrame (that is, extends JFrame), any other class can invoke public methods of JFrame on your class. This results in exposing API that doesn't need to be exposed.

If your class has-a JFrame, that situation doesn't arise. YOU decide what methods of your class can or can't be invoked from the outside world.

This may not be important at an early stage of learning, when you alone control the code, but assumes importance when you have no control over how your class is used ... or abused. Good design prevents the latter
 
Marko Matosevic
Greenhorn
Posts: 12
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So they both create the same program in the end, only the second method is safer?
I always read stuff like that in tutorials but never seem to get it. Why do we have to protect the code, like using constant variables or setting methods to private and stuff like that. If I am the only one who writes and modifies the code, why should I be bothered by protecting it so much, unless the user has access to some variables and I need to protect those to prevent program crashes?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I am the only one who writes and modifies the code, why should I be bothered by protecting it so much,

If you write a piece of code that is totally standalone and will only ever be used by yourself and never be reused by you or anyone else at a later time then arguably you don't need to worry. However, given that most code written doesn't fall into that category, you should get into the habit of writing quality code now to make sure you don't write unsafe code when it really matters.

 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I am the only one who writes and modifies the code, why should I be bothered by protecting it so much,


Are you thinking of ever getting a job as a programmer? Then it's a good thing to practice writing safe code. Or maybe you write some software and then you want to share it with the world. Oops, rewrite. If you wrote safely you wouldn't have to change the code.

I think it's a good habit to get into.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!