• Register
0 votes
40 views

Problem :

I have following two classes: JDialog and JFrame (see signature below). I want them to have few extra methods, but I also want them both to extend my code, without rewriting the code Is it possible to do that?

java.awt.Window » java.awt.Dialog » javax.swing.JDialog  
java.awt.Window » java.awt.Frame » javax.swing.JFrame

e.g.:
Suppose I am trying to add the function moveWindowToMagicPositionOnScreen(), to move the window to the user-friendly position on users screen.I want the function to apply to both the JDialog and JFrame classes. Using the inheritance, I am trying to write two classes, for example MagicJDialog and MagicJFrame, each of them will implement the method moveWindowToMagicPositionOnScreen() in the identical way. So I will not want to write the code twice. I want only one class to utilize the code of the other class.

6 5 3
7,540 points

1 Answer

0 votes

Solution :

I think the Interface / Delegate / Composition ideas will be the "cleanest". But the best alternative is to make use of the utility class with the static method as follows :

public static moveWindowToMagicPositionOnScreen(Window window) { 
window.setLocation(x, y);  
}

In fact, I would like to put the calculation logic in another function, 

e.g computeMagicPositionOnScreen().

By following above approach you can resolve your logical issues.

9 7 4
38,600 points

Related questions

0 votes
1 answer 50 views
50 views
Problems: Can anyone give the right solution? From the choices below select the correct order to code CSS pseudo-classes? a. link, hover, visited, active b. hover, link, active, visited c. link, visited, hover, active d. link, hover, active, visited
asked Feb 14 maddi86 5.4k points
1 vote
1 answer 37 views
37 views
Problem: Hello all, Please explain to me the error in the following program. class test { String name="name"; public person(String nm){name=nm;} } class emp extends test { String empid="0000"; } public class emptest { public static void main( ... class emp extends test required: String found no arguments reason: actual and formal argument lists differ in length Thanks for the effort. Best regards
asked Mar 10 Gavin 15.3k points
0 votes
1 answer 619 views
619 views
Problem : Getting below eclipse error the class file viewer cannot handle the given input ('org.eclipse.ui.ide.filestoreeditorinput').
asked Nov 12, 2019 peterlaw 6.9k points
1 vote
1 answer 13 views
13 views
Problem: I have a very basic question being a novice. It might be silly to you but I really want to know the fact. Let’s say you have a java program. Can you have two classes in one java file? What is the reason behind it? Thanks for your clearance.
asked Mar 27 Gavin 15.3k points
0 votes
1 answer 10 views