method overriding

C

chris sennitt

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;
}
}
 
J

Joe

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();
}
}
 
C

chris sennitt

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.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().
 
R

Ryan Stewart

chris sennitt said:
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.
 
A

Andrew Hobbs

chris sennitt said:
as i said :---
i want to be able to use code in the following manner.

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


*********************************************************
 
C

chris sennitt

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 :)
 
F

fox_fire

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

chris sennitt

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 :)
 
R

Ryan Stewart

chris sennitt said:
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.
 
C

chris sennitt

Ryan Stewart said:
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
 
R

Ryan Stewart

chris sennitt said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top