• Register
0 votes
66 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

Please log in or register to answer this question.

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 164 views
164 views
Problem: Hi there! I have been learning to program for a long time now and I recently learned inheritance in Java. I wrote the following program for practice: class Institutions { public void print_institutions() { System.out.println("This is an institution"); } } class ... , I have no idea what is causing this error. Can anybody here please clarify the cause of the error and its solution? Thanks!
asked Dec 29, 2020 Code Learner 9.2k points
0 votes
1 answer 99 views
99 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, 2020 maddi86 5.4k points
0 votes
1 answer 1 view
1 view
Problem: Any ideas on how to solve this?the relationship between an interface and the class that implements it is
asked 1 day ago ashik 12.9k points
0 votes
1 answer 39 views
39 views
Problem: I just recently started learning inheritance in Java. I am a bit confused between method overloading and method overriding. If two classes have same method with different number of arguments, given that the classes have parent-child relationship, will it be overloading or overriding? Moreover, when does the overloading occurs in inherited classes?
asked Jan 8 Code Learner 9.2k points
0 votes
1 answer 4 views
4 views
Problem: There are two forms. Form2 is derived from Form1. I have an issue with Form2: The method or operation is not implemented Thanks in advance
asked Mar 20 ummesalma 25.2k points
0 votes
1 answer 5 views
5 views
Problem: I want to change the background color according to which color has the class selected. Please help! Thanks
asked Mar 27 ummesalma 25.2k points
0 votes
1 answer 2 views
2 views
Problem: When I encountered this problem, I attempted to find a solution on the internet but was unable to find any useful details. If you read the above query and understand what the solution is, please cooperate with me- Thanks.
asked Mar 30 tuhin1 48.9k points
0 votes
1 answer 11 views
11 views
Problem: Hi there! I have got a task to complete in which I have to write a class named ‘car’. Following is the description of the class that I have to convert into the program: Model: An integer that holds the car’s model. Make: A String object ... days but I am unable to figure out the solution. I only have declared the variables so far. Any help would be appreciated. Thanks!
asked Mar 2 Code Learner 9.2k points
0 votes
2 answers 5 views
5 views
Problem: Please help me … how can I solve it? How to call a static method java?
asked Apr 3 Ifra 24.4k points
0 votes
1 answer 12 views
12 views
Problem i want to call amethod from another class java
asked Feb 7 ahmedadel9090 3.1k points