Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Function that returns derived class?

Reply
Thread Tools

Function that returns derived class?

 
 
aaronfude@gmail.com
Guest
Posts: n/a
 
      05-07-2007
Hi,

I want to define a function in the base class that will do some stuff
and then "return this;" but in a such a way that when called on an
object of a derived class it will return an instance of that class?

public class A {
public A cool() {
System.out.println("Hello, World");
return this;
}
}

public class B extends A {
public void anotherFunction() {

}
}

And then I want the following to work

new B().cool().anotherFunction();

Thank you,

Aaron Fude

 
Reply With Quote
 
 
 
 
Piotr Kobzda
Guest
Posts: n/a
 
      05-07-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> I want to define a function in the base class that will do some stuff
> and then "return this;" but in a such a way that when called on an
> object of a derived class it will return an instance of that class?
>
> public class A {
> public A cool() {
> System.out.println("Hello, World");
> return this;
> }
> }
>
> public class B extends A {
> public void anotherFunction() {
>
> }


public B cool() {
super.cool();
return this;
}

> }
>
> And then I want the following to work
>
> new B().cool().anotherFunction();



piotr
 
Reply With Quote
 
 
 
 
Spring Liu
Guest
Posts: n/a
 
      05-07-2007
On May 8, 6:23 am, (E-Mail Removed) wrote:
> Hi,
>
> I want to define a function in the base class that will do some stuff
> and then "return this;" but in a such a way that when called on an
> object of a derived class it will return an instance of that class?
>
> public class A {
> public A cool() {
> System.out.println("Hello, World");
> return this;
> }
>
> }
>
> public class B extends A {
> public void anotherFunction() {
>
> }
>
> }
>
> And then I want the following to work
>
> new B().cool().anotherFunction();
>
> Thank you,
>
> Aaron Fude


((B) new B().cool()).anotherFunction();

 
Reply With Quote
 
aaronfude@gmail.com
Guest
Posts: n/a
 
      05-08-2007
Can the overriding and the casts be avoided with some kind of template
structure?

Thanks
On May 7, 7:41 pm, Spring Liu <(E-Mail Removed)> wrote:
> On May 8, 6:23 am, (E-Mail Removed) wrote:
>
>
>
>
>
> > Hi,

>
> > I want to define a function in the base class that will do some stuff
> > and then "return this;" but in a such a way that when called on an
> > object of a derived class it will return an instance of that class?

>
> > public class A {
> > public A cool() {
> > System.out.println("Hello, World");
> > return this;
> > }

>
> > }

>
> > public class B extends A {
> > public void anotherFunction() {

>
> > }

>
> > }

>
> > And then I want the following to work

>
> > new B().cool().anotherFunction();

>
> > Thank you,

>
> > Aaron Fude

>
> ((B) new B().cool()).anotherFunction();- Hide quoted text -
>
> - Show quoted text -



 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      05-08-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi,
>
> I want to define a function in the base class that will do some stuff
> and then "return this;" but in a such a way that when called on an
> object of a derived class it will return an instance of that class?
>
> public class A {
> public A cool() {
> System.out.println("Hello, World");
> return this;
> }
> }
>
> public class B extends A {
> public void anotherFunction() {
>
> }
> }
>
> And then I want the following to work
>
> new B().cool().anotherFunction();


You can do something similar with a static method, but I don't know any way
to use generics to define an instance method that does what you want.

class A
{
static<T extends A> T cool(T t)
{
t.whateverYouWant();
return t;
}
}


 
Reply With Quote
 
Ben Schumeth
Guest
Posts: n/a
 
      05-08-2007
On Tue, 08 May 2007 02:41:36 +0200, <(E-Mail Removed)> wrote:

> Can the overriding and the casts be avoided with some kind of template
> structure?
>


I very much doubt it, since there are no templates in Java. Despite the
unfortunate similarity in syntax, Java generics are nothing like
C++ templates (for details search the newsgroup archives).

class A
{
public <T extends A> T cool( T in ) // parameter is used to determine
return type
{
System.out.println("Hello, World");
return (T)this;
}
}

class B extends A
{
public void anotherFunction()
{
System.out.println("other func");
}
}

B myB = new B();
myB.cool( myB ).anotherFunction();

No casting in the calling code, but is this really any better than what
Piotr and Liu suggested? I doubt it.

Ben

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
 
Reply With Quote
 
Piotr Kobzda
Guest
Posts: n/a
 
      05-08-2007
(E-Mail Removed) wrote:

> Can the overriding and the casts be avoided with some kind of template
> structure?


As Ben already explained, there are no "template structures" in Java.

Assuming a "kind of ..." means Java generics, just another simple
approach in your case might be:

public class A<T extends A> {
public T cool() {
System.out.println("Hello, World");
return (T)this; // unsafe!
}
}

public class B extends A<B> {
public void anotherFunction() { }
}


It allows for usage like that:

new A().cool(); // raw type A is used here!
new B().cool().anotherFunction();


However, mixing a raw and parameterized types is discouraged, and usage
of it is limited to a single level of inheritance only (erasure of B
declared as "class B<T extends B<T>> extends A<T>", without overriding a
cool() method, results in A (not B) taken as a return type for that method).


Safer approach is to have a generic base class for A and B:

public abstract class Base<T extends Base<T>> {
protected abstract T getT();

public T cool() {
System.out.println("Hello, World");
return getT();
}
}

public class A extends Base<A> {
protected A getT() { return this; }
}

public class B extends Base<B> {
protected B getT() { return this; }

public void anotherFunction() { }
}


There is no problem now in using A and B the way you expect (there is
also easy way to extend that "pattern" with other classes, e.g.
introducing another generic base class for B derived from the A's base
class). Unfortunately, it disallows a direct inheritance of A from B,
so then B can not be used as replacement of A anymore. Partial solution
for that problem may appear already mentioned extended version of that
"pattern", i.e.:

public abstract class ABase<T extends ABase<T>> {
protected abstract T getT();

public T cool() {
System.out.println("Hello, World");
return getT();
}
}

public abstract class BBase<T extends BBase<T>> extends ABase<T> {
public void anotherFunction() { }
}

public class A extends ABase<A> {
protected A getT() { return this; }
}

public class B extends BBase<B> {
protected B getT() { return this; }
}


There's still illegal to do that:

A a = new A();
B b = new B();
a = b;

But the following is legal now:

ABase<?> a = new A();
BBase<?> b = new B();
a = b;


(Using A and B as a names for the abstract base classes, and e.g. AImpl
and BImpl for a concrete implementations' names appears more convenient
for me in that extended "pattern". Consider that if you'll decide to
use it.)


piotr
 
Reply With Quote
 
Hendrik Maryns
Guest
Posts: n/a
 
      05-08-2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Piotr Kobzda schreef:
> (E-Mail Removed) wrote:
>
>> I want to define a function in the base class that will do some stuff
>> and then "return this;" but in a such a way that when called on an
>> object of a derived class it will return an instance of that class?
>>
>> public class A {
>> public A cool() {
>> System.out.println("Hello, World");
>> return this;
>> }
>> }
>>
>> public class B extends A {
>> public void anotherFunction() {
>>
>> }

>
> public B cool() {
> super.cool();
> return this;
> }
>
>> }


I guess you meant

public B cool() {
return super.cool();
}

That’s the way I’d prefer.

Note that this is only available in Java 1.5+ (covariant return types).

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGQEGDe+7xMGD3itQRAoVVAJ9MwwIUH82qfsPHLVafuP +tvEAnNgCfc0sm
A5z86iq8/dLZYNtF0aDEpwI=
=4Moa
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Piotr Kobzda
Guest
Posts: n/a
 
      05-08-2007
Hendrik Maryns wrote:
> Piotr Kobzda schreef:


>> public B cool() {
>> super.cool();
>> return this;
>> }


> I guess you meant
>
> public B cool() {
> return super.cool();
> }


No. It doesn't compile.

> That’s the way I’d prefer.


I guess you meant:

public B cool() {
return (B) super.cool();
}


My intent was to prevent from unnecessary cast.


piotr
 
Reply With Quote
 
Hendrik Maryns
Guest
Posts: n/a
 
      05-08-2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Piotr Kobzda schreef:
> Hendrik Maryns wrote:
>> Piotr Kobzda schreef:

>
>>> public B cool() {
>>> super.cool();
>>> return this;
>>> }

>
>> I guess you meant
>>
>> public B cool() {
>> return super.cool();
>> }

>
> No. It doesn't compile.


Really? Sorry for not testing. That should be a bug. Hm, well no, ok.
The compiler is not supposed to know ‘this’ is returned, it just knows
it is an A. You’re right.

>> That’s the way I’d prefer.

>
> I guess you meant:
>
> public B cool() {
> return (B) super.cool();
> }
>
>
> My intent was to prevent from unnecessary cast.


Makes sense.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGQFLSe+7xMGD3itQRAsZjAJ0dMK9cCh4Ushdbu42vHh 2XqCGafQCeMenN
Z9P4qQHZsjRomRuyDcjY0p8=
=JUm/
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Derived Structure in Derived Class?? David C++ 3 01-29-2008 07:38 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) developereo@hotmail.com C++ 4 05-23-2007 09:32 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM



Advertisments