Jump to content
  • Advertisement
Sign in to follow this  
BlackSunStudios

Doing Every Operation that extends the parent class

This topic is 2642 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys need some help again.

I am making a powder game clone and heres what i want to do.
there is a parent class called Element I want a class called solid to extend element.
i also want a gas that extends element.
so i would do it like this:
Public class Element {etcetcetc}
public class solid extends Element{etcetcet}
public class gas extends Element{etcetcet}

how would i be able to do an operation in both solid and gas at the same time.
say i wanted to draw them:
public void draw(graphics g){
Element.draw(g)


}

how would i be able to draw every thing with the element class instead of going:
rock.draw(g)
gas.draw(g)

So basically, how would i do an operation/method to every class that extends a certain class?

Share this post


Link to post
Share on other sites
Advertisement
What you are looking for is something call virtual or abstract methods.
This allows you to declare a method in Element and then override that method in your solid and gas classes.
When you then later handle a list of element objects, you can call this method on the element object and it will be forwarded to your implementation in the solid or gas class depending on the real type of the object.

See this link for an explanation of virtual functions in Java.

Share this post


Link to post
Share on other sites
Kjell Andersson already answered your question. You need to mark your Element class as abstract and than have it declare the common methods you want to use.

public abstract class Element {

public abstract void draw();
}

public class Solid extends Element {

// I am forced to implement draw, I will get a compiler error if I don't
public void draw() {
// your code here
}
}


Now in your main class you would have something like this;


private List<Element> elements = new ArrayList();

public void draw() {
for(int i = 0; i < this.elements.count; i++) {
this.elements.draw();// would call either Solid of Gases draw method depending on the type of object
}
}


**EDIT** Note that the actual override of the method is dependent on the language you are using. From what it looks like you are using java so just putting the method name would override it in the child class, in other languages you have to state "public override void draw() {...}" Edited by XXChester

Share this post


Link to post
Share on other sites
Hidden

[quote name='pulpfist' timestamp='1316538219' post='4863884']
Do you mean how to draw all object instances that inherits from Element?


not necceseraly draw but just do the same method in every class that inherits element.
[/quote]

I haven't used Java in a few years now so I'm not sure this is the "Java way", but if you give the Elements class a static list (Basically a global list that is common to all Elements), and add each object to it in the Element constructor, you can set up a loop that runs through all the elements in the list and calls a virtual draw function on each.
Note that the draw function has to be virtual for this to work, and the list should be a list of the base class. Eg. public static List<Element> renderList;

Share this post


Link to post

Kjell Andersson already answered your question. You need to mark your Element class as abstract and than have it declare the common methods you want to use.

public abstract class Element {

public abstract void draw();
}

public class Solid extends Element {

// I am forced to implement draw, I will get a compiler error if I don't
public void draw() {
// your code here
}
}


Now in your main class you would have something like this;


private List<Element> elements = new ArrayList();

public void draw() {
for(int i = 0; i < this.elements.count; i++) {
this.elements.draw();// would call either Solid of Gases draw method depending on the type of object
}
}


**EDIT** Note that the actual override of the method is dependent on the language you are using. From what it looks like you are using java so just putting the method name would override it in the child class, in other languages you have to state "public override void draw() {...}"





Thanks!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!