method overriding

Discussion in 'Java' started by chris sennitt, Mar 6, 2004.

  1. the following code shows a simple class setup.
    i want to be able to use code in the following manner.
    b.aa().bb();

    rather than lots of seperate lines
    b.aa();
    b.bb();

    as there will be many method calls per object

    the code fails for obvious reasons. the return type of b.aa() is of class
    aClass which doesnt have the method bb().

    so, i thought, duplicate the code in bClass and return the bClass object.

    public bClass aa() {
    super.aa(); // call the real method that does the
    work....
    return this;
    }
    however, this complains that the return type is incompatible with
    aClass.aa(), so that doesnt work.

    any suggestions on class design / changes so that this can be made to work ?
    the class tree is way more complex than shown here.

    public class test {
    public static void main(String[] args) {

    aClass a;
    bClass b;

    a = new aClass();
    b = new bClass();

    a.aa();

    b.aa();
    b.bb();

    b.aa().bb();

    }
    }
    class aClass {

    public void call() {
    System.out.println( "aClass" );
    }
    public aClass aa() {
    System.out.println( "aa" );
    return this;
    }
    }

    class bClass extends aClass {
    public bClass bb() {
    System.out.println( "bb" );
    return this;
    }
    }
    chris sennitt, Mar 6, 2004
    #1
    1. Advertising

  2. chris sennitt

    Joe Guest

    In article <Uio2c.1520$>,
    says...
    > the following code shows a simple class setup.
    > i want to be able to use code in the following manner.
    > b.aa().bb();
    >
    > rather than lots of seperate lines
    > b.aa();
    > b.bb();



    I'm having a lot of trouble understanding the problem domain. Your
    example above doesn't make clear what you're trying to do.


    > as there will be many method calls per object



    If this is the problem, and you want to reduce the number of method
    calls, then one solution would be to create one public method and have
    the method call the rest of the private methods. That way you only have
    to make one method call in your test class.
    Another possibility would be to use reflection to get an array of a
    class's methods and invoke them all.


    > class aClass {
    >
    > public void call() {
    > System.out.println( "aClass" );
    > }
    > public aClass aa() {
    > ...
    > }




    In your sample aClass, if you want to return an instance of aClass,
    you'll want to do this:


    public aClass aa() {

    public aClass aa() {
    return new aClass();
    }
    }
    Joe, Mar 6, 2004
    #2
    1. Advertising

  3. > I'm having a lot of trouble understanding the problem domain. Your
    > example above doesn't make clear what you're trying to do.
    >

    as i said :---
    i want to be able to use code in the following manner.
    > > b.aa().bb();
    > >
    > > rather than lots of seperate lines
    > > b.aa();
    > > b.bb();

    and this code fails
    b.aa().bb();
    the code fails for obvious reasons. the return type of b.aa() is of class
    aClass which doesnt have the method bb().
    chris sennitt, Mar 6, 2004
    #3
  4. chris sennitt

    Ryan Stewart Guest

    "chris sennitt" <> wrote in message
    news:Uio2c.1520$...
    > the following code shows a simple class setup.
    > i want to be able to use code in the following manner.
    > b.aa().bb();
    >
    > rather than lots of seperate lines
    > b.aa();
    > b.bb();
    >

    Even if it worked, b.aa().bb(); is not necessarily the same as b.aa();
    b.bb(); In your example below, yes, but it can be very different.

    > as there will be many method calls per object
    >
    > the code fails for obvious reasons. the return type of b.aa() is of class
    > aClass which doesnt have the method bb().
    >
    > so, i thought, duplicate the code in bClass and return the bClass object.
    >
    > public bClass aa() {
    > super.aa(); // call the real method that does the
    > work....
    > return this;
    > }
    > however, this complains that the return type is incompatible with
    > aClass.aa(), so that doesnt work.
    >
    > any suggestions on class design / changes so that this can be made to work

    ?
    > the class tree is way more complex than shown here.
    >
    > public class test {
    > public static void main(String[] args) {
    >
    > aClass a;
    > bClass b;
    >
    > a = new aClass();
    > b = new bClass();
    >
    > a.aa();
    >
    > b.aa();
    > b.bb();
    >
    > b.aa().bb();
    >
    > }
    > }
    > class aClass {
    >
    > public void call() {
    > System.out.println( "aClass" );
    > }
    > public aClass aa() {
    > System.out.println( "aa" );
    > return this;
    > }
    > }
    >
    > class bClass extends aClass {
    > public bClass bb() {
    > System.out.println( "bb" );
    > return this;
    > }
    > }
    >

    I assume you have some reason for trying to do this. As you presented it, it
    doesn't make a lot of sense. You're trying to call a method that doesn't
    exist, as you pointed out. If an object of type aClass needs to perform the
    action represented by bb(), then that method, or at least an abstract
    declaration of it, belongs in aClass. If you provide a clearer picture of
    what you need to do, we might be able to help more.
    Ryan Stewart, Mar 6, 2004
    #4
  5. chris sennitt

    Andrew Hobbs Guest

    "chris sennitt" <> wrote in message
    news:Evq2c.2985$...
    > > I'm having a lot of trouble understanding the problem domain. Your
    > > example above doesn't make clear what you're trying to do.
    > >

    > as i said :---
    > i want to be able to use code in the following manner.
    > > > b.aa().bb();
    > > >
    > > > rather than lots of seperate lines
    > > > b.aa();
    > > > b.bb();


    But in your case

    b.aa().bb();

    is not the same as

    b.aa();
    b.bb();

    because of the way you have written the classes.

    In addition I am a little puzzled by your subject line.
    b.aa().bb();
    is not method overriding.

    However you could cascade your method calls by using

    b.bb().aa();

    In any case the code doesn't achieve anything so until you tell us a little
    more of what you are trying to achieve then it is going to be difficult for
    anyone to say much more.

    Cheers

    Andrew


    --
    ********************************************************
    Andrew Hobbs PhD

    MetaSense Pty Ltd - www.metasense.com.au
    Australia

    61 8 9246 2026
    metasens AntiSpam @iinet dot net dot au


    *********************************************************

    > and this code fails
    > b.aa().bb();
    > the code fails for obvious reasons. the return type of b.aa() is of class
    > aClass which doesnt have the method bb().
    >
    >
    >
    Andrew Hobbs, Mar 7, 2004
    #5
  6. > However you could cascade your method calls by using
    >
    > b.bb().aa();
    >
    > In any case the code doesn't achieve anything so until you tell us a

    little
    > more of what you are trying to achieve then it is going to be difficult

    for
    > anyone to say much more.
    >

    well, cascading methods calls is as close as i can get to describing what i
    am trying to achieve.
    i have been used to another OOP language where you could omit a return value
    and the method would default to the object that recieved the method call.
    making cascading calls trivial to implement.
    this ment that, for example, a HTML object call could look like

    oHtmlText.bold().italic().font("blahblah").size(12).colour("blahblah");

    instead of

    oHtmlText.bold();
    oHtmlText.italic();
    oHtmlText.font("blahblah");
    oHtmlText.size(12);
    oHtmlText.colour("blahblah");

    and it didnt matter where the methods "lived" in the class tree. as long as
    the object oHtmlText had those methods define somewhere AND they did a
    return this ( or self in the other language ).

    for a single class - this is easy to todo. but when i create a class tree i
    cannot see a way of doing this.

    even if i attempt to duplicate the method in a sub class i get an error
    message because the sub class method ( of the same name and parameter list )
    is returning a different type.

    so, at the moment i am coming to the conclusion that the language cant do
    this.

    but, trying to find a solution is a great way to re-learn a language :)
    chris sennitt, Mar 7, 2004
    #6
  7. chris sennitt

    fox_fire Guest

    If you want to be able to do this, could you not class cast the object back to bClass?
    ((bClass)b.aa()).bb()
    That's the only way I think you can do what you want to do in one line.
    fox_fire, Mar 7, 2004
    #7
  8. > If you want to be able to do this, could you not class cast the object
    back to bClass?
    > ((bClass)b.aa()).bb()
    > That's the only way I think you can do what you want to do in one line.
    >

    lol - well the idea was to make the code concise and simple to read :)
    chris sennitt, Mar 7, 2004
    #8
  9. chris sennitt

    Ryan Stewart Guest

    "chris sennitt" <> wrote in message
    news:kbF2c.2$...
    > > If you want to be able to do this, could you not class cast the object

    > back to bClass?
    > > ((bClass)b.aa()).bb()
    > > That's the only way I think you can do what you want to do in one line.
    > >

    > lol - well the idea was to make the code concise and simple to read :)
    >

    A cast doesn't make code difficult to read, but chaining method calls does.
    Ryan Stewart, Mar 7, 2004
    #9
  10. "Ryan Stewart" <> wrote in message
    news:...
    > "chris sennitt" <> wrote in message
    > news:kbF2c.2$...
    > > > If you want to be able to do this, could you not class cast the object

    > > back to bClass?
    > > > ((bClass)b.aa()).bb()
    > > > That's the only way I think you can do what you want to do in one

    line.
    > > >

    > > lol - well the idea was to make the code concise and simple to read :)
    > >

    > A cast doesn't make code difficult to read, but chaining method calls

    does.

    i agree, chaining can make the code un readable, however in this case it
    works well.

    oHtmlText.bold().italic().font("blahblah").size(12).colour("blahblah");

    seems reasonably easy to read

    or casting as a work around .........

    ( (htmlText)( (htmlText) ( (htmlText) ( (htmlText)
    oHtmlText.bold() ).italic() ).font("blahblah") ).size(12) ).colour("blahblah
    ");

    seems pretty confusing to me and , yes i may have missed some brackets
    somewhere but as u can see, casting is not an option here
    chris sennitt, Mar 7, 2004
    #10
  11. chris sennitt

    Ryan Stewart Guest

    "chris sennitt" <> wrote in message
    news:y2G2c.575$...
    >
    > "Ryan Stewart" <> wrote in message
    > news:...
    > > "chris sennitt" <> wrote in message
    > > news:kbF2c.2$...
    > > > > If you want to be able to do this, could you not class cast the

    object
    > > > back to bClass?
    > > > > ((bClass)b.aa()).bb()
    > > > > That's the only way I think you can do what you want to do in one

    > line.
    > > > >
    > > > lol - well the idea was to make the code concise and simple to read :)
    > > >

    > > A cast doesn't make code difficult to read, but chaining method calls

    > does.
    >
    > i agree, chaining can make the code un readable, however in this case it
    > works well.
    >
    > oHtmlText.bold().italic().font("blahblah").size(12).colour("blahblah");
    >
    > seems reasonably easy to read
    >
    > or casting as a work around .........
    >
    > ( (htmlText)( (htmlText) ( (htmlText) ( (htmlText)
    >

    oHtmlText.bold() ).italic() ).font("blahblah") ).size(12) ).colour("blahblah
    > ");
    >
    > seems pretty confusing to me and , yes i may have missed some brackets
    > somewhere but as u can see, casting is not an option here
    >

    Yeah, that's ugly. Is that what you're trying to do with Java? The
    ..bold().italic().... thing? Why don't you make a Style object that lets you
    call those methods to set its state, then associate your oHtmlText and
    whatever other objects with a Style object? Then the methods in your Style
    class can each return a Style object and you get the effect you're looking
    for. Something like:

    public class HtmlText {
    private Style style;
    private String text;

    public void setStyle(Style style) {
    this.style = style;
    }

    public void setText(String text) {
    this.text = text;
    }

    public static void main(String[] args) {
    Style myStyle = new Style().bold().italic();
    HtmlText ht = new HtmlText();
    ht.setStyle(myStyle);
    }

    }

    class Style {
    private boolean bold = false;
    private boolean italic = true;
    // Other states

    private Style bold() {
    this.bold = true;
    return this;
    }

    private Style italic() {
    this.italic = true;
    return this;
    }
    }

    This code may have typos. I just typed it in here, don't have time to check
    it right now. You should be able to get the general idea though.
    Ryan Stewart, Mar 7, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Noé R. Barranco

    Overriding finalize() method

    Noé R. Barranco, Aug 29, 2003, in forum: Java
    Replies:
    1
    Views:
    959
    Lee Fesperman
    Aug 30, 2003
  2. Paul J. Lucas
    Replies:
    5
    Views:
    930
    Chris Uppal
    Apr 12, 2004
  3. puzzlecracker
    Replies:
    4
    Views:
    401
    Sashi
    Jan 18, 2006
  4. Allan Streib

    question about overriding method

    Allan Streib, Oct 29, 2003, in forum: Python
    Replies:
    0
    Views:
    297
    Allan Streib
    Oct 29, 2003
  5. Koszalek Opalek

    Overriding a class method with an object method

    Koszalek Opalek, Jul 2, 2007, in forum: Perl Misc
    Replies:
    8
    Views:
    142
    -berlin.de
    Jul 6, 2007
Loading...

Share This Page