How to correctly use abstract and interface classes

Discussion in 'Plugin Development' started by unrealdesign, Jun 1, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    unrealdesign

    So I've recently been trying to teach myself abstract and interface classes, but I'm coming up short when I want to implement them into my code. Here is what I have so far:

    Code:java
    1.  
    2. package org.fragzone.fzeco.foundation;
    3.  
    4. import org.bukkit.entity.Player;
    5.  
    6. public abstract interface Economy {
    7.  
    8. public abstract Player getPlayer();
    9. public abstract String getName();
    10. public abstract int getBal();
    11. public abstract void setBal(int amount);
    12. public abstract void addBal(int amount);
    13. public abstract void remBal(int amount);
    14. public abstract boolean isVIP();
    15.  
    16. }
    17.  


    Code:java
    1.  
    2. package org.fragzone.fzeco.economy;
    3.  
    4. import java.io.File;
    5.  
    6. import org.bukkit.configuration.file.FileConfiguration;
    7. import org.bukkit.configuration.file.YamlConfiguration;
    8. import org.bukkit.entity.Player;
    9. import org.fragzone.fzeco.foundation.Economy;
    10.  
    11. public class PlayerEco implements Economy {
    12.  
    13. private Player player;
    14. private String playerName;
    15. private int bal;
    16. private boolean vip;
    17.  
    18. File fileFile = new File("plugins"+File.separator+"FZEco"+File.separator+"banks"+File.separator+"users"+File.separator+getName()+".yml");
    19. FileConfiguration file = YamlConfiguration.loadConfiguration(fileFile);
    20.  
    21. public PlayerEco(Player player){
    22. this.player = player;
    23. this.playerName = player.getName();
    24. this.bal = file.getInt("Balance");
    25. this.vip = file.getBoolean("VIP");
    26. }
    27.  
    28. @Override
    29. public Player getPlayer(){
    30. return player;
    31. }
    32.  
    33. @Override
    34. public String getName() {
    35. return playerName;
    36. }
    37.  
    38. @Override
    39. public int getBal() {
    40. return bal;
    41. }
    42.  
    43. public void setBal(int amount) {
    44. bal = amount;
    45. file.set("Balance", bal);
    46. }
    47.  
    48. @Override
    49. public void addBal(int amount) {
    50. bal = file.getInt("Balance");
    51. bal = bal + amount;
    52. file.set("Balance", bal);
    53. }
    54.  
    55. @Override
    56. public void remBal(int amount) {
    57. bal = file.getInt("Balance");
    58. bal = bal - amount;
    59. file.set("Balance", bal);
    60. }
    61.  
    62. @Override
    63. public boolean isVIP() {
    64. vip = file.getBoolean("VIP");
    65. return vip;
    66. }
    67.  
    68. }
    69.  


    Code:java
    1.  
    2. package org.fragzone.fzeco.economy;
    3.  
    4. import org.bukkit.entity.Player;
    5.  
    6.  
    7. public class EcoManager {
    8.  
    9. private static Player player;
    10.  
    11. private static PlayerEco playerEco = new PlayerEco(player);
    12.  
    13. public static PlayerEco getEco(Player player){
    14. return playerEco;
    15. }
    16.  
    17. }
    18.  


    And I want to be able to do something like:
    Code:java
    1. int amount = EcoManager.getEco(p).getBal();


    But I keep getting errors: http://pastebin.com/raw.php?i=hsfSw0gX

    Thanks in advance :)
     
  2. Offline

    Technius

  3. Offline

    Wingzzz

    When using an interface you can simply do it this way:
    Code:java
    1. public interface Economy {
    2.  
    3. public void setBal(int amount);
    4.  
    5. }

    This is because it is automatically assumed abstract due to it being an interface.
     
  4. Offline

    unrealdesign


    How do I make it un-null? This is the part I'm not understanding. If I use a constructor, I have to change the value of player, and that conflicts with it being static.


    Thanks! That should help me :)
     
  5. Offline

    Unknowncmbk

    Just know that an interface is basically a class file that inherits all the public members from it's superclass so that the client can use them.

    Say you need to make someone a calculator interface that can add and subtract.

    The client (who you are making it for) doesn't care that you need to actually do behind the scenes math and helpers method they are just interested in seeing the public methods.

    public int add (int num1, int num2); //sums the two numbers together
    etc.
     
  6. Offline

    unrealdesign

    The reason I'm doing it, is to learn some more java while also code for a custom economy plugin. I may be doing way more than I need, but I think it'd be good to learn some abstract and interface class types. So even if there is a, persay, easier way, it's alright.

    As a side note, I thought interfaces weren't meant as guidelines for other classes, so that it has to use those methods to define things in the new class. Then abstracts is where I think I'm more confused on and still reading about.

    What I'm trying to do using the interface is set it as a guideline for my PlayerEco class. Then be able to call upon from my manager class EcoManager.getEco(p).getBal(); So what I was thinking that would do is, get the ecomanager class, so I could then call the PlayerEco class, then I could get the balance of that player, so I could send them a message of what their balance is. Again, I bet this could be done a more simple way, by just checking the config as the player does the command, but I just want to explore different ways of doing things so I can better use interface and asbtract classes in the future. As I think they work well together hand in hand?
     
  7. Offline

    Technius

    unrealdesign
    I believe abstract classes can have constructors, unlike interfaces. Also, you can only extend one abstract class, unlike interfaces.
     
  8. Offline

    unrealdesign

    Technius Okay that could be helpful

    But overall, how do I fix my problem?
     
  9. Offline

    Sagacious_Zed Bukkit Docs

    The solution to your problem will become much more apparent when you stop using static in your EcoManager class.
     
  10. Offline

    unrealdesign

    Sagacious_Zed I tried not to use it at all, but then I couldn't use EcoManager.getEco(player) because it said it had to be static
     
  11. Offline

    Sagacious_Zed Bukkit Docs

    It is tell you it has to be static, because you are trying to use it in a static maner. Don't use it in a static manner....
     
  12. Offline

    Wingzzz

    Make an instance of the EcoManager like you do normally with certain things:
    Code:java
    1. EcoManager ecoManager = new EcoManager();

    Although you may want a constructor so the above code may vary.

    EDIT: Oh, yeah didn't explain much, but you should hopefully know what that is above; making a object of type EcoManager so you can then do things like:
    Code:java
    1. ecoManager.doSomething();

    Using the object(instance) instead of a static method that is the same instance no matter what throughout all instances of a class.
     
Thread Status:
Not open for further replies.

Share This Page