• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why so many classes?

 
Aadesh Nguyen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, I just stared learning Java few weeks ago. When studying about how to create a JFrame in Swing, I came across the code (1) , in Core Java 1.2 : Volume 1 Fundamentals:

My question is why the author create SimpleFrame class? Why dont just go straight forward like in the code (2)? Thanks so much.



Code (1)

---
Code (2):

 
Dinuka Arsakularatne
Ranch Hand
Posts: 198
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Aadesh,

In most cases we extend JFrame within a class so that you can do all initialization within your constructor. And in a typical application you will have many Internal frames, panels etc. And trying to initialize them all through in the main method is bad design and not maintainable. And for better separation of concern we move to a different class which extends from JFrame to construct new frames.

Hope that helps

Cheers

Dinuka
 
Aadesh Nguyen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does help a lot. Thanks Dinuka.
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what Example 1 is trying to do. The SimpleFrame class is never used.

In general you should only extend a class to provide new functionality. Setting a few properties does not provide new functionalicy. Search the web using "composition vs inheritance" to find all kinds on discussions.

For what its worth you can also look at the Sun tutorial examples, which do NOT extend JFframe.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!