Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  abstract class declaring a derived class as a return  (Read 2058 times)
0 Members and 1 Guest are viewing this topic.
Offline keldon85

Senior Member


Medals: 1



« Posted 2007-07-21 01:28:07 »

I almost had a bit of a problem when turning an interface into an abstract class. Basically I wanted it to provide an implementation (which is why I changed it to abstract), however since it was mutable and therefore returning itself the abstract class caused problems for any code that implemented the derived class.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
abstract class Abstract {
   public Abstract mutableMethod (){
      return createInstance ().doSomething();
   }
}

abstract class Derived extends Abstract {
   public Derived doSomething (){
      ...
   }
}

class Main {
   void poorMethod (){
      // problem !!!
     Derived derived = new Derived().doSomething();
   }
}


However using templates you can eliminate this problem without overriding every method or making main look weirder in any way.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
abstract class Abstract <DerivedClass extends Abstract> {
   public DerivedClass mutableMethod (){
      return createInstance ().doSomething();
   }
}

abstract class Derived extends Abstract <Derived> {
   public Derived doSomething (){
      ...
   }
}

class Main {
   void poorMethod (){
      // no more problem !!!
     Derived derived = new Derived().doSomething();
   }
}

Offline broumbroum

Junior Member





« Reply #1 - Posted 2007-07-21 01:33:25 »

note that you cannot namely instantiate an abstract-class, but you must use a non-abstract extending class or an anonymous instanciation...   Wink

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline keldon85

Senior Member


Medals: 1



« Reply #2 - Posted 2007-07-21 02:03:17 »

note that you cannot namely instantiate an abstract-class, but you must use a non-abstract extending class or an anonymous instanciation...   Wink
Yes, that was the aim. Basically it is for an implementation of a tone generator - at first I had a oscillator interface. As soon as I created another waveform it was more than obvious to keep a majority of the implementation in the oscillator since very little is going to change between the sub classes other than what it thinks is at a particular phase.

In fact the only abstract methods in Oscillator are getAmplitude and createInstance. createInstance is there so that the abstract Oscillator can still provide implementations to mutable methods. Also I amended the example so that you can not later create an error by doing something like "class Derived extends Abstract<NotDerrived>".

Disclaimer: this is not for anything sound related, other than the fact that you could use some principles. It's for a game involving waves!

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline karmaGfa

Junior Member




Miaow


« Reply #3 - Posted 2007-08-08 18:11:06 »

Hi,

I have been using this pattern for year, and there is something you need to realize:

It is not because you write :

public abstract class X<C extends X<C>> {...}

... that you should suppose that X will always be used like that:

public class A extends X<A> {...}

It could also be used like that:

public class B extends X<A> {...}

<a href="http://www.le-moulin-studio.com">Le Moulin Studio</a> - MMO Technologies and Services.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (41 views)
2014-09-24 16:13:29

Pippogeek (32 views)
2014-09-24 16:12:22

Pippogeek (22 views)
2014-09-24 16:12:06

Grunnt (47 views)
2014-09-23 14:38:19

radar3301 (30 views)
2014-09-21 23:33:17

BurntPizza (65 views)
2014-09-21 02:42:18

BurntPizza (37 views)
2014-09-21 01:30:30

moogie (44 views)
2014-09-21 00:26:15

UprightPath (53 views)
2014-09-20 20:14:06

BurntPizza (55 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!