Results 1 to 9 of 9
- 03-25-2014, 03:24 PM #1
Member
- Join Date
- Mar 2014
- Posts
- 7
- Rep Power
- 0
Just a simple question about a Java Programm
Ok, I have to write a programm for my school in which I use 3 classes. These classes are:
Class Window
Java Code:public class Window { protected int size; public Window() { size=1; System.out.println("Window size="+size); } public Window(int size) { this.size=size; System.out.println("Window size="+size); } public void setSize(int x) {size += x;} public void printSize() {System.out.println("Size=" + size);} }
Java Code:public class MWindow extends Window { private String message = "No message"; public MWindow(String message) { size = 2; this.message = message; System.out.println ("Window message = " + message); } public MWindow(int size, String message) { super(size); this.message = message; System.out.println ("Window message = " + message); } public void setSize1(int y) {size = y;} public void setSize2(int z) {super.setSize (z);} public void printSize() {System.out.println ("MSize="+size);} public void printSize1() {System.out.println (super.size);} public void printSize2() {super.printSize();} }
Java Code:public class RunWindow { public static void main (String[] args) { MWindow mw1=new MWindow("First MWindow"); MWindow mw2=new MWindow(3, "Second MWindow"); System.out.println(mw1.size); System.out.println(mw2.size); } }
Java Code:Window message = First MWindow Window size=3 Window message = Second MWindow 2 3
Java Code:Window size=1 Window message = First MWindow Window size=3 Window message = Second MWindow 2 3
- 03-25-2014, 03:35 PM #2
Re: Just a simple question about a Java Programm
Subclass constructors always implicitly call super(); which is the default constructor of Window.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 03-25-2014, 03:43 PM #3
Member
- Join Date
- Mar 2014
- Posts
- 7
- Rep Power
- 0
Re: Just a simple question about a Java Programm
Ok, I didnt know that. But why this implicit call of super(); is only happening during the construction of mw1 instance and not during the construction of mw2 instance?
- 03-25-2014, 04:34 PM #4
Re: Just a simple question about a Java Programm
Because you are calling the super(int size) constructor explicitly yourself in line 12 of MWindow.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 03-25-2014, 04:42 PM #5
Re: Just a simple question about a Java Programm
Window is the name of a Java SE class. It will be less confusing if your code used its own name and not a Java SE name.
If you don't understand my response, don't ignore it, ask a question.
- 03-25-2014, 09:49 PM #6
Member
- Join Date
- Mar 2014
- Posts
- 7
- Rep Power
- 0
Re: Just a simple question about a Java Programm
So, every time I call a constructor of a subclass, he also calls his super() automatically, even if I have not called anywhere in my code this default constructor of the "upper" class. But this implicit call of super() is not happening, if I have called somewhere in my subclass constructor super() by myself with or without arguments (e.g super(size) ). Am I right?
I would change the name of this class to something different, but the question dictates me to give that class the name "Window".Last edited by thomason93; 03-25-2014 at 09:53 PM.
- 03-26-2014, 10:43 AM #7
Re: Just a simple question about a Java Programm
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 03-26-2014, 11:01 AM #8
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Just a simple question about a Java Programm
This is dragging on a bit. I'm going to attempt to just explain the rules that are in play here.
If there is one thing that -must- happen, it is that constructors are invoked. If that wouldn't happen then objects wouldn't properly be initialized. So Java does this for you so you can't forget, upon object creation.
That problem becomes slightly harder when you have a hierarchy of classes because then a constructor needs to be invoked in each class in that hierarchy so each individual layer can initialize itself. Again: this MUST happen or you'd end up with half-initialized objects. The question becomes: which constructor must be invoked? Sometimes Java can implicitly make that choice for you, sometimes it cannot.
- if there is a default constructor (or no constructor at all so Java generates a default constructor for you), Java can invoke that for you without any help
- if there is no default constructor but there is at least one constructor which accepts parameters, you need to explicitly invoke that constructor through super(param, param, etc.)
- if there are multiple constructors then you have to make sure that the appropriate one is explicitly invoked by using super with the correct parameters
But the whole point of all this is: a constructor MUST be invoked. And then what is in your care is that the CORRECT constructor is invoked. Hence: the usage of super()."Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 03-26-2014, 02:39 PM #9
Member
- Join Date
- Mar 2014
- Posts
- 7
- Rep Power
- 0
Re: Just a simple question about a Java Programm
Thank you very much all for your helpfull answers. I gave in this set or excercises in time. I think I figoured out approximately the purpose of super() use etc. Though I still have a few more questions that came of these excercises.
If I add the following lines in main in RunWindow so it looks like:
Java Code:public class RunWindow { public static void main (String[] args) { MWindow mw1=new MWindow("First MWindow"); MWindow mw2=new MWindow(3, "Second MWindow"); System.out.println(mw1.size); System.out.println(mw2.size); mw1.setSize1(4); System.out.println(mw1.size); mw1.setSize2(2); System.out.println(mw1.size); mw1.setSize(2); System.out.println(mw1.size); } }
Java Code:Window size=1 Window message = First MWindow Window size=3 Window message = Second MWindow 2 3 4 6 8
Java Code:public class MWindow extends Window { private String message = "No message"; protected int size =7; public MWindow(String message) { size = 2; this.message = message; System.out.println ("Window message = " + message); } public MWindow(int size, String message) { super(size); this.message = message; System.out.println ("Window message = " + message); } public void setSize1(int y) {size = y;} public void setSize2(int z) {super.setSize (z);} public void printSize() {System.out.println ("MSize="+size);} public void printSize1() {System.out.println (super.size);} public void printSize2() {super.printSize();} } }
Java Code:Window size=1 Window message = First MWindow Window size=3 Window message = Second MWindow 2 7 //these 4 //lines 4 //have 4 //changed
Similar Threads
-
Simple question about array in JAVA
By hills2720s in forum New To JavaReplies: 10Last Post: 10-17-2013, 04:14 AM -
Java: simple ArrayList question
By bronzefish in forum New To JavaReplies: 7Last Post: 01-23-2013, 02:47 PM -
Simple Java question
By devinmont in forum New To JavaReplies: 1Last Post: 09-11-2012, 11:15 PM -
Simple java applet zoom question
By Lemmy Winks in forum New To JavaReplies: 4Last Post: 07-02-2012, 04:17 AM -
Simple question on Embedding Java applets
By gasper91 in forum New To JavaReplies: 6Last Post: 06-14-2011, 04:24 AM
Bookmarks