[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

Extending classes and there constructors

I guess this is an easy one, but my Java-book just doesn't mention it, so here it goes:

There's a class called ByteArrayInputStream and I have a subclass myByteArrayOutputStream. I did that to get the protected member variable count from ByteArrayOutputStream (I had another way of doing it, but I like to experiment, so that's why I tried this rather cumbersome method). The class looks as follows:

class myByteArrayOutputStream extends ByteArrayOutputStream
{
    //Extended class to get the protected member variable count from ByteArrayOutputStream
    public int getByteCount()
    {
        return count;
    }
}


The way this class is defined and built, it only supports the default constructor, without arguments. I know to make more overloaded constructors, but I want them to be inherited from ByteArrayOutputStream. Let's say, myByteArrayOutputStream(int size) should be the same as ByteArrayOutputStream(int size).

I guess this is easy, so, 100pts for anyone with a good answer :)

Thanks in advance,
Abel
0
abel
Asked:
abel
  • 12
  • 11
1 Solution
 
OviCommented:
use in your constructor : super() or super(int size)
0
 
OviCommented:
public myByteArrayOutputStream() {
     super();
..........................................//your code
}

public myByteArrayOutputStream(int size) {
     super(size);
..........................................//your code
}

public myByteArrayOutputStream() {
     super(1000);
..........................................//your code
}
0
 
dhymesCommented:
The class DataOutputStream is dirived from ByteArrayOutputStream, and has a method called written that returns to you the number of bytes that have been written to the output stream. Why not just use this subclass?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
abelAuthor Commented:
Thanks, but I don't know how... :( please elaborate a bit.

Can you cut 'n' paste my sample code with working declarations of the overriden constructors?
0
 
OviCommented:
Or even :

public myByteArrayOutputStream(int size) {
                          super(size);
                     ..........................................//your code
                     }
public myByteArrayOutputStream() {
                          this(1000);
                     ..........................................//your code
                     }

, or :

public myByteArrayOutputStream() {
                          super(1000);
                     ..........................................//your code
                     }

public myByteArrayOutputStream(int size) {
                          this();
                     ..........................................//your code
                     }


 
0
 
abelAuthor Commented:
Oops, you guys are fast! You already did it.

dhymes, I might indeed use that one (thanks for the tip, I'll look into it), but I will probably run into this problem sooner or later, so that's why I used this as a sample.
0
 
abelAuthor Commented:
Thanks a lot, please forgive my ignorance here, but what are the differences between all these calls? Does it call the constructor of the parent class and next I (can) extend that constructor?
0
 
OviCommented:
The above code match your problem ?
0
 
OviCommented:
By using the super() method automatically you make a call to the supercalss matching constructor. Using this(1000) in your constructor give to you a little more flexibility and generality (depending on the case) in the implementation of your constructors.

public myByteArrayOutputStream(int size) {
     super(size);
...........................................//your code
}
public myByteArrayOutputStream() {
    this(1000);
...........................................//your code
}



For example, see this :

import java.applet.Applet;
import java.awt.Font;
import java.awt.Image;
import java.awt.Color;
import java.net.MalformedURLException;
import java.net.URL;

class Element
{
    public String m_text;
    public int m_level;
    boolean m_visible;
    public String m_url;
    public int m_imageNum;
    public Image m_image;
    public Image m_image2;
    public int m_textWidth;
    public String m_target;
    public int m_isNew;
    public boolean m_failedMerge;
    public boolean m_fIsVolume;
    public Color nodeColor;
   
    Element()
    {
        nodeColor = TreeColors.nodeDefaultColor[0];
        m_text = null;
        m_level = 0;
        m_visible = false;
        m_url = "";
        m_imageNum = -1;
        m_textWidth = 0;
        m_target = "";
        m_isNew = 0;
        m_failedMerge = false;
        m_fIsVolume = false;
    }

    Element(Element element)
    {
        m_text = null;
        m_level = 0;
        m_visible = false;
        m_url = "";
        m_imageNum = -1;
        m_textWidth = 0;
        m_target = "";
        m_isNew = 0;
        m_failedMerge = false;
        m_fIsVolume = false;
        nodeColor = element.nodeColor;
        m_text = element.m_text;
        m_textWidth = element.m_textWidth;
        m_level = element.m_level;
        m_imageNum = element.m_imageNum;
        m_image = element.m_image;
        m_image2 = element.m_image2;
        m_isNew = element.m_isNew;
        m_target = element.m_target;
        m_url = element.m_url;
        m_visible = element.m_visible;
    }

    Element(String s, int i, Applet applet)
    {
        this(s, i, null, "", applet);
    }

    Element(String s, int i, Image image, Applet applet)
    {
        this(s, i, image, "", applet);
    }

    Element(String s, int i, Image image, String s1, Applet applet)
    {
        m_text = null;
        m_level = 0;
        m_visible = false;
        m_url = "";
        m_imageNum = -1;
        m_textWidth = 0;
        m_target = "";
        m_isNew = 0;
        m_failedMerge = false;
        m_fIsVolume = false;
        boolean sem = true;
        int ind = 0;
        try {
                   ind = Integer.parseInt(s.substring(0, 1));
        }
        catch(NumberFormatException e) {
              sem = false;
        }
                        if(sem)
                              m_text = s.substring(1);
                        else
              m_text = s;
                        nodeColor = TreeColors.nodeDefaultColor[ind];
        m_level = i;
        m_url = s1;
        if(i < 2)
            m_visible = true;
        if(image != null)
        {
            m_image = image;
            return;
        }
        try
        {
            m_image = applet.getImage(new URL(applet.getDocumentBase(), "default.jpg"));
            return;
        }
        catch(MalformedURLException _ex)
        {
            return;
        }
    }

    Element(String s, int i, Image image, String s1, Applet applet, int j)
    {
        this(s, i, image, s1, "", applet, j);
    }

    Element(String s, int i, Image image, String s1, String s2, Applet applet, int j)
    {
        this(s, i, image, s1, applet);
        if(i < j)
            m_visible = true;
        else
            m_visible = false;
        m_target = s2;
    }

//...................................................................................//
}
0
 
abelAuthor Commented:
Yes. I've tried your code, and while working with it I think I got the picture. If I understand it correctly, this is what happens:

class myByteArrayOutputStream extends ByteArrayOutputStream
{
    //Extended class to get the protected member variable count from ByteArrayOutputStream
    public myByteArrayOutputStream()
    {
        super();
    }
    public myByteArrayOutputStream(int size)
    {
        super(size);
    }
    public int getByteCount()
    {
        return count;
    }
}


The keyword super, in the first constructor, calls the constructor of the superclass (hence "super"). The keyword this, I don't know yet... and I don't yet use it. After I've called the constructor, I can (logically, I see that now) put more code to extend the construction of the class. Next, the second constructor, does the same, but now with the other constructor of the superclass. If I want, I can, as you put in you samplecode, call anything I want, which makes a lot of sence, using super, as long as I obey the constructors-definitions of the superclass.

Is there a same kind of possibility for even higher level superclasses? Or is that something that I probably never need, or something that doesn't exist in the language?

BTW, I owe you already a lot of thanks, because my code does now what I'd asked for.

Greetz,
Abel
0
 
abelAuthor Commented:
And again, our postings crossed :)
0
 
OviCommented:
Yep, you have understand the problem, and yes, you can do this with all public constructors in all high level classes (exception : abstract classes wich cannot be instantiated). With protected constructors, you can call them only if you "subclass the superclass" (your case).
This refer to the CURENT OBJECT. CALLING THIS(VARIABLES), YOU CALL AN ALLREADY DEFINED CONSTRUCTOR IN THIS CLASS.

OK?
0
 
abelAuthor Commented:
Ok, in a nutshell, this is the theory you mean, please correct me if I'm wrong:
super(arglist)
--calls the superclass with argument list arglist
this(arglist)
--calls this class, the one the call is in, with argument lis arglist.

Right?

This also means, that it is impossible to match in a single call/statement all the constructors of a parent class, you'll have to manually create them again, right?

Thanks :)
0
 
OviCommented:
what do you meen by -  "And again, our postings crossed :)" ? (I'm not a very good english speaker)
0
 
OviCommented:
Yep.
0
 
abelAuthor Commented:
And once more, we postings crossed :)

Thanks for the explanations here. And for calling the superclass of the superclass (of the superclass) what would be the statement here? Let's say, the superclass of this class is OutputStream, can I call that also directly, like with "super"?
0
 
abelAuthor Commented:
I meant with "postings crossed" that while I was typing, you posted another comment in this thread. Sometimes you answered something I was still asking. Then, my question came after you answered, which looks quite odd, don't you think? Looking at this thread, I asked you to elaborate on your answer. At almost the same time you already posted a comment elaborating (=going into the subject more deeply) on your own answer. That's why my comment, which should stand right after your first answer, now stands there after the second comment.

Of course, that is in no way bad or something, it might just confuse readers (and us too).
0
 
OviCommented:
Oky,

to your comment : "And once more, we postings crossed :)

                     Thanks for the explanations here. And for calling the superclass of the superclass (of the superclass) what would be
                     the statement here? Let's say, the superclass of this class is OutputStream, can I call that also directly, like with
                     "super"?"

see this :


import java.awt.Window;
import java.awt.Frame;
import java.awt.Dialog;
import java.awt.Point;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class NewSmart extends Dialog {
    private Dimension dim = new Dimension(900, 450);
    private Point pos = new Point(100, 130);
   
    public NewSmart() {

//you can call the super.super constructor, see ?
        super(new Frame(), "title", false).super(new Frame());
        setSize(dim);
        setLocation(pos);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                EventObserver.dispatchEvent(panelID, EventObserver.disposeManager);
            }
        });
        show();
    }
}


I think now we are sinchronized :)
0
 
abelAuthor Commented:
Yes, indeed we are :)

And thanks a lot for this crash course in Java inheritance programming (if I may call it so). I think you deserve your points, so here's the A!

Greetz,
Abel
0
 
OviCommented:
understand now ?
0
 
abelAuthor Commented:
Yep. I have quite a great expertise in other (OO) languages, I only need to "brush up" when I am about to use (yet) another language. I started using Java only a few weeks ago and some things have startled me a bit since then. With information like you've posted here I can more quickly get up to speed with this language.
0
 
OviCommented:
Tnx and good luck.
0
 
abelAuthor Commented:
One final remark here.

When I call for example in my example "super().super();" I actually call the empty constructor of OutputStream. If I call in that same example "super().super().super()" I call the constructor of Object, right? Then, going some further, what _am_ I calling when I call "super().super().super().super()"? I don't get any error, so the compiler doesn't mind at all, and of course nobody will probably ever call something like that, but I am just wandering and you might know it. Actually I guess I just get Object again. Also, "super().super().super().super().super()" or even more "supers" don't give any compile error.

You don't have to answer this of course, but nevertheless, if you know, you might want to let me know it too :)

Greetz,
Abel
0
 
OviCommented:
"You put me in the fog "  I don't know what your super() method call, but you made me courious too, so if I know, you will know too.

Regards
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 12
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now