3 Replies - 1280 Views - Last Post: 27 April 2015 - 10:14 AM

#1 Recoil  Icon User is offline

  • D.I.C Addict

Reputation: 51
  • View blog
  • Posts: 504
  • Joined: 28-June 08

Interfacing parent classes

Posted 26 April 2015 - 02:24 PM

My application is working and my data is saving as it should, but I want to know if this is the proper way to do this.

Right now I have a parent class (Entity) that has a "Name" property.
Spoiler


From there I can inherit (Entity) in a child class (Player) that has a "Size" property. I am using an interface to get the name from the parent class (Entity)
Spoiler


Next I create a child class (NPC), and inherit (Player). I am interfacing yet again to get the size from the parent class (Player).
Spoiler


This is a very abbreviated portion of my code as an example. Each class above has more info than what is listed. I want to have multiple "Entity" child classes. I also want to have multiple "Player" child classes. I am not asking how to do these...

I just want to know if this is the proper way to lay this out? I am also looking for any suggestions on what I am doing.

Is This A Good Question/Topic? 0
  • +

Replies To: Interfacing parent classes

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5061
  • View blog
  • Posts: 13,659
  • Joined: 18-April 07

Re: Interfacing parent classes

Posted 26 April 2015 - 03:50 PM

Actually instead of creating these interface setups, you should be setting your properties and such (size for instance) as protected variables. That is what protected is for. Your child classes can access parent member data which is declared "protected" and you don't need to be using this interface setup. This will simplify things quite a bit for you. You won't have to mess with and maintain an interface definition, you will automatically have access to those variables in a derived class so you don't need to create some separate method/interface to manipulate them.

Look up "protected" access for more information. :)
Was This Post Helpful? 1
  • +
  • -

#3 Recoil  Icon User is offline

  • D.I.C Addict

Reputation: 51
  • View blog
  • Posts: 504
  • Joined: 28-June 08

Re: Interfacing parent classes

Posted 26 April 2015 - 05:19 PM

Thanks Martyr2. I have made a few modifications to get rid of the interfaces. I wanted to see if this looks a bit more practical?

Entity:
Spoiler


Player:
Spoiler


NPC:
Spoiler


When I get rid of the interfaces the 2 data files that I created with them have to be remade. It is not much data to them so that isn't a big deal to just remake those 2. But once I get this properly setup I will be creating a lot of data files (300+).

I have racked my brain trying to figure out if there is ever going to be a need to interface any of the classes so I won't have to go back and completely remake them again and I honestly can't think of a reason...but I also might not understand what to use them for if not like I was doing.

I'm going to go back and look back at how the data is being saved to the files, because regardless of how I change how the data comes it shouldn't be different in the file.

Edit: Disregard all that. I think I figured out what was going on. When I was accessing a class and assigning one of the variables, it was also saving the variables for the parent class. If that parent had a parent class it was saving them too, so up to 3 different versions of 1 thing.

I had to save this as just an xml and bypass my serializer just to see what exactly it was doing, and it was showing things being saved multiple times. Once I just assigned the variables once through the editor, and deleted the additional properties in the class files, everything saves once as it should.

It looks like I have a bit of work to do to clear out all this editing and comments.

This post has been edited by Recoil: 26 April 2015 - 06:34 PM

Was This Post Helpful? 0
  • +
  • -

#4 Recoil  Icon User is offline

  • D.I.C Addict

Reputation: 51
  • View blog
  • Posts: 504
  • Joined: 28-June 08

Re: Interfacing parent classes

Posted 27 April 2015 - 10:14 AM

I finally got the double data issue resolved not not creating additional get/set properties in each child class...because I am an idiot and it is already accessible.

I am working on this project by myself and have no one else to run ideas about the best way to go about this. I need some advice on how better to structure the classes because I think I may have too many, and there might be a better way to do this.

My data is being stored in separate files and contain a "FileType" so I know how to handle them when they are launched (launches with editor when the file is clicked). I can create an XML file with the data, as well as a custom serialized file type. Each one of these are Entities which are used by certain areas of my application. Here is the current planned list of Entities:
Spoiler


Here are some of my classes I have done so far with partial lists of properties that they contain:
Spoiler


It probably would be easier to just use a relational database but I do not have that luxury, and all Entities I have listed have to be in separate files. I want to find out if what I have lined up so far is a logical approach? To me it just seems like I have to many classes inheriting from parent classes, which inherit from another parent class. Stuff like PlayerAttributes class was created because I am unable to inherit 2 classes, and it was the only way I could think of to resolve this issue.

I am open to any suggestions on this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1