Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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: 3253
30
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic