• Register
0 votes
45 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 16 views
16 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 5.5k points
0 votes
1 answer 70 views
70 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 18 views
18 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 5.5k points
0 votes
1 answer 34 views
34 views
Problem: I am making a program which consists of solving a 2x2 equation, for this I used the Cramer method, the problem is not how to solve it, but it throws an error when executing it, and I suspect that it is because I used methods of the Matrix class Inside the Vector class ... " using namespace std; int main () { cout << "As long as the program compiles well it is enough" << endl; return 0; }
asked Nov 5, 2020 sasha 5.3k points
0 votes
1 answer 16 views
16 views
Problem: Hello Programmers! I just gave an interview for a programming job. The interviewer asked me a question regarding inheritance in Java. The question was: Can subclass access the private members of a superclass? I answered: No!. But, I am still confused about whether I answered correctly or not. Can any of you tell me whether my answer was correct or not? Also, explain why and why not?
asked Dec 17, 2020 Code Learner 5.5k points
0 votes
1 answer 13 views
13 views
Problem: a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. Which overriding method has the same name, number and type of parameters, and return type as the method that it overrides?
asked Dec 5, 2020 Mashhoodch 5.9k points
1 vote
1 answer 114 views
114 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, 2020 Gavin 15.3k points
0 votes
1 answer 1K views
1K 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 18 views
18 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&rsquo;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, 2020 Gavin 15.3k points
0 votes
1 answer 13 views
13 views
Hi, I have a small php project. I need a php class that will enable me to retrieve the information from a database so that I can display in a table as shown in this html code: <!DOCTYPE html> <html> <head> <title>List of Cars</title> </head> <body> <table> <tr> <th>Year</th> <th>Brand</ ... >AUDI Q2 2020</td> </tr> <tr> <td>2020</td> <td>Volvo</td> <td>Xc60 2020</td> </tr> </table> </body> </html>
asked Oct 11, 2020 azriel 590 points