Doing Every Operation that extends the parent class

Started by
4 comments, last by BlackSunStudios 12 years, 7 months ago
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?
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.
Do you mean how to draw all object instances that inherits from Element?

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.
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() {...}"

Remember to mark someones post as helpful if you found it so.

Journal:

http://www.gamedev.net/blog/908-xxchesters-blog/

Portfolio:

http://www.BrandonMcCulligh.ca

Company:

www.gwnp.ca


[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;

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!

This topic is closed to new replies.

Advertisement