Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » The nested constructor "new A(new B(x))"(How should I make sense of the nested constructor?)
The nested constructor "new A(new B(x))" [message #1759725] Mon, 17 April 2017 15:20 Go to next message
Ricky Robert is currently offline Ricky RobertFriend
Messages: 4
Registered: December 2016
Junior Member
Hello guys...

So I'm currently self-teaching Java and I'm stuck with this tricky technic that my online youtuber "teacher" used, I just can't make any sense of it...

I was doing this programming exercice as the youtuber instructed. It consist of writing a Question Generator program which should display a certain number of "capital game questions", based on a 2-dimensional ArrayList of country names and their capital names, then we formulate a question sentence such as "what's the capital of the following country: France." and the user enters his answer etc etc.

I did it in a very simple manner with only 2 classes. Then I checked how this youtuber did and I am stuck with his version ever since. In his simple version he got 5 classes, he differentiated GUIquiz and ConsoleQuiz in order to display messages in a GUI window or in the console, an abstract class and a QG (QuestionGenerator) class. If I simplify the whole package it looks like this (assume that we want it to display in GUI windows):

Class Test {
____main(...) {
________GUIquiz quiz = new GUIquiz ( new QG(2));
____}
}

Class GUIquiz extends QuizAbs {
____public Guiquiz ( QG g ) {
________super ( g );
____}
}

Class ConsoleQuiz extends QuizAbs {...} //(almost the same as GUIquiz)

Class QuizAbs {
____private QG g;
____public QuizAbs (QG g ) {
________this g = g;
____}
}

Class QG {
____private int nb_q; // stands for number of questions
____public QG ( int nb_q ) {
________this nb_q = nb_q;
____}
}

I can't understand the content in this main() method, I mean if it was just A a = new A( ), no problem, but here...

- So how many objects does the line in the main() generate in fact? at least 3 I guess?
- So the program first instantiates an object of QG and passes the value 2 to the nb_q variable in QG class, then instantiates an object of GUIquiz...? -
- But in GUIquiz its constructor seems to have a variable of the QG class? How's that happening? I mean GUIquiz inherits from QuizAbs but not from QG right ? o_0 !!!
- QG g means that this g variable can have any property and access to any method in QG class right?

I know my description may be long but I'm so disturbed by this exercice I cannot progress if I leave it behind...

Thank you very much in advance for your enlightenments! Have a good day fellows Smile
Re: The nested constructor "new A(new B(x))" [message #1759729 is a reply to message #1759725] Mon, 17 April 2017 18:19 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33136
Registered: July 2009
Senior Member
This forum is specifically for asking questions about Eclipse and Eclipse projects, i.e., it's not a general Java help forum. It seems better to ask questions like this in on stackoverflow.com.

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: The nested constructor "new A(new B(x))" [message #1759785 is a reply to message #1759729] Tue, 18 April 2017 13:51 Go to previous message
Ricky Robert is currently offline Ricky RobertFriend
Messages: 4
Registered: December 2016
Junior Member
OK thanks and sorry for the inconvenience Smile

have a good day!
Previous Topic:Java import statement hints
Next Topic:Where does Eclipse store its settings? (Mac OS)
Goto Forum:
  


Current Time: Fri Apr 19 04:23:26 GMT 2024

Powered by FUDForum. Page generated in 0.02967 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top