• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why so many classes?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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):

 
Ranch Hand
Posts: 198
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It does help a lot. Thanks Dinuka.
 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic