Create new class vs extends a class

J

Jenny

Hi,

Create new class vs extends a class - Could you tell me which way
should I use?

Here are two pieces of code. They do the same thing.

1. The code is from the link
http://www.cadenhead.org/book/java24hours/source/chapter13/ClockFrame.java


import java.awt.*;
import javax.swing.*;

public class ClockFrame extends JFrame {
public ClockFrame() {
super("Clock");
setSize(225, 125);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
FlowLayout flo = new FlowLayout();
pane.setLayout(flo);
ClockPanel time = new ClockPanel();
pane.add(time);
setContentPane(time);
setVisible(true);
}

public static void main(String[] arguments) {
ClockFrame sal = new ClockFrame();
}
}

2. I wrote this code to compare.

import java.awt.*;
import javax.swing.*;
public class Windows {
public static void main(String[] args) {
JFrame f = new JFrame("The Frame");
f.setSize(300, 300);
f.setLocation(100, 100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
Container pane = f.getContentPane();
FlowLayout flo = new FlowLayout();
pane.setLayout(flo);
ClockPanel time = new ClockPanel();
pane.add(time);
f.setContentPane(time);
f.setVisible(true);
}
}

I think the first one is better. But I am new to Java. I'd like to
get your thoughts.

Thank you a lot.

Jenny
 
S

Steve Horsley

Jenny said:
Hi,

Create new class vs extends a class - Could you tell me which way
should I use?

Here are two pieces of code. They do the same thing.

1. The code is from the link
http://www.cadenhead.org/book/java24hours/source/chapter13/ClockFrame.java


import java.awt.*;
import javax.swing.*;

public class ClockFrame extends JFrame {
public ClockFrame() {
super("Clock");
setSize(225, 125);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
FlowLayout flo = new FlowLayout();
pane.setLayout(flo);
ClockPanel time = new ClockPanel();
pane.add(time);
setContentPane(time);
setVisible(true);
}

public static void main(String[] arguments) {
ClockFrame sal = new ClockFrame();
}
}

2. I wrote this code to compare.

import java.awt.*;
import javax.swing.*;
public class Windows {
public static void main(String[] args) {
JFrame f = new JFrame("The Frame");
f.setSize(300, 300);
f.setLocation(100, 100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
Container pane = f.getContentPane();
FlowLayout flo = new FlowLayout();
pane.setLayout(flo);
ClockPanel time = new ClockPanel();
pane.add(time);
f.setContentPane(time);
f.setVisible(true);
}
}

I think the first one is better. But I am new to Java. I'd like to
get your thoughts.

Thank you a lot.

Jenny

I read somewhere that if in doubt, you should use composition rather than
inheritance. In your case, it looks very neat to extend JFrame. So neat in
fact that I might be tempted to use inheritance in this instance. But doing
so goes against OO design in a way, because this extended JFrame is not
suitable for using as a general-purpose JFrame any more, so it's not
necessarily suitable to pass to other objects/methods that expect to be
given a JFrame.

Tough call.

Steve.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top