Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Just a simple question about a Java Programm

  1. #1
    thomason93 is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default 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);}
    }
    Class MWindow
    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();}
    }
    Class RunWindow
    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);
    }
    }
    And the question I have is what is gonna print this programm. In my opinion, the printed results should be
    Java Code:
    Window message = First MWindow 
    Window size=3 
    Window message = Second MWindow 
    2 
    3
    but the printed results I get when i run the programm are
    Java Code:
    Window size=1 
    Window message = First MWindow 
    Window size=3 
    Window message = Second MWindow 
    2 
    3
    So, the 1st line which get printed ("Window size=1") is the one I cant explain why is printed. I have figured out that for some reason, this line is printed cause of the 1st constructor of class Window, although I dont call this constructor in my code. Can anyone help me out with this question? My deadline for this excercises set is tomorrow. Thanks in advance.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default 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

  3. #3
    thomason93 is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default 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?

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default 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

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default 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.

  6. #6
    thomason93 is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: Just a simple question about a Java Programm

    Quote Originally Posted by PhHein View Post
    Because you are calling the super(int size) constructor explicitly yourself in line 12 of MWindow.
    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?

    Quote Originally Posted by Norm View Post
    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.
    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.

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default Re: Just a simple question about a Java Programm

    Quote Originally Posted by thomason93 View Post
    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?
    Not really, the first line of a subclass constructor is super(xyz) not somewhere, either because you've written it or the compiler inserts super() automatically. Just for testing remove the Window() constructor and recompile. You'll get a compiler error.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default 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().
    kneitzel likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    thomason93 is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default 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);
    }
    }
    then the printed results I get are:
    Java Code:
    Window size=1
    Window message = First MWindow
    Window size=3
    Window message = Second MWindow
    2
    3
    4
    6
    8
    I understand how these results came out. But if I add the the statement "protected int size =7;" in class MWindow, so now MWindow now looks like:
    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();}
    }
    }
    I get these results:
    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
    I think I also understand how these results came out, but I m not sure. Whose instance's size value is '3' in line 3? Is it something like "mw2.super.size" ? (if this statement is valid). Can someone explain me what is actually happening in this case?

Similar Threads

  1. Simple question about array in JAVA
    By hills2720s in forum New To Java
    Replies: 10
    Last Post: 10-17-2013, 04:14 AM
  2. Java: simple ArrayList question
    By bronzefish in forum New To Java
    Replies: 7
    Last Post: 01-23-2013, 02:47 PM
  3. Simple Java question
    By devinmont in forum New To Java
    Replies: 1
    Last Post: 09-11-2012, 11:15 PM
  4. Simple java applet zoom question
    By Lemmy Winks in forum New To Java
    Replies: 4
    Last Post: 07-02-2012, 04:17 AM
  5. Simple question on Embedding Java applets
    By gasper91 in forum New To Java
    Replies: 6
    Last Post: 06-14-2011, 04:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •