Understanding Object-oriented programming.

Discussion in 'Resources' started by KraZ__, Jan 30, 2014.

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

    KraZ__

    So allow me to explain basic object-oriented programming to you. Object-oriented programming was designed to resemble real world objects. You can use this to your advantage, an example of an object-oriented concept would be:

    Animal > dog > husky

    Husky is a form of a dog which is also a form of an animal. So in this case dog extends or implements everything in the animal class, and husky implements or extends everything that's in the dog class. Now classes are extended whilst interfaces are implemented. So lets say for an example we want to create a few monsters for a game in java, we can do this by creating an interface Monster because we want to create a template for each monster. Think of classes which extend as extensions of an object, and think of interfaces like templates for an object.

    Code:java
    1.  
    2. public interface Monster {
    3.  
    4. public int getScareLevel();
    5.  
    6. }
    7.  


    So now when we create a monster class like a zombie for example we can implement the 'template' of Monster if you will.

    Code:java
    1.  
    2. public class Zombie implements Monster {
    3.  
    4. //interface automatically forces us to add the method.
    5. @override
    6. public int getScareLevel() {
    7. //the 'this' keyword references THIS class, so this means that we're accessing the properties of this class
    8. //And because we added a int data type with the variable of 'scareLevel' in the interface and we're accessing
    9. //this class which implements the properties, we can return it's value.
    10. return this.scareLevel;
    11. }
    12.  
    13. }
    14.  


    So now extends works similarly, except it extends an object so for example, Human could be the main object, but we can also create 2 seperate objects which extends Human which could be Male/female. Here is how that would look:

    Code:java
    1.  
    2. public class Human {
    3.  
    4. }
    5.  
    6. public class Male extends Human {
    7.  
    8. }
    9.  
    10. public class Female extends Human {
    11.  
    12. }
    13.  


    However these classes do nothing at the moment, we need to add static references or we need to create a constructor. In this example a constructor would be the best approach. So here is how a constructor looks, and here is how to initalise an object.

    Code:java
    1.  
    2. public class Human {
    3.  
    4. public Human() {
    5.  
    6. }
    7.  
    8. }
    9.  
    10. //Object, variable, new Object()
    11. Human myHuman = new Human();
    12.  
    13.  


    if you have constructor for Human object too or you wish to access a method from human you can use the 'super' keyword.

    we can also add parameters to constructors and specify what specific properties each instance of the class should have. You can also use the instanceof operator to check if a class is an instance of another class, i.e

    Code:java
    1.  
    2. if(Male instanceof Human) {
    3. //Then he's a human
    4. } else {
    5. //Then's he's something else...
    6. }
    7.  


    So here is an example of the instanceof operator in Bukkit:

    Code:java
    1.  
    2. if(sender instanceof Player) {
    3. //Then the command was operated by a player
    4. } else {
    5. //You must be a player to operate the command
    6. }
    7.  


    So here is an example of how to initiate an object and access it from other classes.

    Code:java
    1.  
    2. public class MainComponent extends JavaPlugin {
    3.  
    4. public MyObject obj;
    5.  
    6. public void onEnable() {
    7. //here we intialise the variable (create a new instance of the class), however we only do so
    8. //when the plugin is enabled.
    9. obj = new MyObject(this);
    10. }
    11. }
    12.  
    13. public class MyObject {
    14.  
    15. public MainComponent plugin;
    16.  
    17. public MyObject(MainComponent plugin) {
    18. //so we refrence the plugin variable of THIS class using the this keyword
    19. //and we set its value of it to the parameter variable of plugin.
    20. this.plugin = plugin;
    21. }
    22. }
    23.  


    We can now access other methods and variables using the variable 'plugin'.
    Code:java
    1.  
    2. //i.e
    3. plugin.myMethod();
    4. plugin.myVariable = 42;
    5.  


    I hope this helps!

    If anyone could please correct me on anything I may be missing or I have misunderstood that would be most appreciated! I just think this tutorial would help a lot of people.

    Sorry if this was poorly written but I wrote it on my phone.
     
    Junrall likes this.
  2. Offline

    ferrybig

    there seems to be a small errors at the first example, you cannot have variables and filled in code blocks inside interfaces. you need to use an abstract class for that purposes
     
  3. Offline

    KraZ__


    Thanks for that, I wasn't sure about variables, but I don't have filled in code blocks for the interface.
     
  4. Offline

    ferrybig

    KraZ__

    The filled in code block was just a mistake from my, I saw it too late that the last { was actually an }
     
  5. Offline

    Maximvdw

    I always use the Human example to explain OO xD:
    "A human has a nose ,two eyes and a mouth"
    "A male is an extension to a human (also nose, ...) but he has something extra"
    "A female is an extension to a human (also nose,...) but again, she has some extra things"

    :)
     
  6. Offline

    KraZ__

    I know right :D haha, best way to explain it, do you think this tutorial was okay?
     
  7. Offline

    Maximvdw

    sure ;) maybe give some more information how this is used in bukkit?
    Maybe show them the purpose of 'instanceof' when checking if a Human is male or female

    - Maximvdw
     
  8. Offline

    KraZ__


    Good idea, I'll add that in now...
     
  9. Offline

    desht

    Take care with that sort of design; male and female are probably better represented as a property of a Human (or better yet, of sexually-reproducing organisms in general), not as subclasses. With your approach, you'll end up needing to extend every class of living creature you may need to implement in future, which isn't really scalable.
     
    TigerHix likes this.
  10. Offline

    AlmarM

    Want to know my opinion?

    Show Spoiler
    First of all, your more or what trying to explain polymorphism, not OOP. :p

    Second, people who don't know how to code in OOP (or code at all, which I have seen on these forums), shouldn't coding Minecraft plugin. They should learn it with regular Java, or any other programming language used for creating software.

    Learning coding is 1 thing.
    Learning OOP is another.
    Learning Minecraft plugins is yet another thing.

    Don't flame me for this, it's just my opition. ;)
     
    TigerHix likes this.
  11. Offline

    MTN

    this should be in the resource section
     
  12. Offline

    KraZ__

    Lol but polymorphism is just an oop concept. So really the title is correct. Also I disagree I find learning haha with a goal behind it i,e bukkit plugins is a good way to learn. I learned everything I know through bukkit and Ive learned tons of languages all thanks to the awesome,people here who helped me. Now im just returning to help others.

    Sorry, but its to help with plugin development, i thought this was the best place for it..

    i know, I just thought it was the best way of trying to explain, didn't really want to go roo indepth aa it may have confused people, thought that explaining it simple would be the best way.

    Sorry about multiple posts, hard to control this website on my phone, so i cant tag people in one post. Sorry once again. GODDAMN IT XENFORO MAKE A MOBILE VERSION!
     
  13. Offline

    AlmarM

    Just as you said, it's a concept of OOP. I recommend learning OOP before polymorphism, that's all I'm saying. :p

    Some new people to coding might have a hard time with coding plugins, some other might not (taking you as an example here). Let me rephrase what I said before, because after reading it again, it doesn't seem quite right.

    Depending on how well you pick up programming, coding plugins might not be the best to start with. I myself am a game-developer and have loads of experience as well. My experience all started with coding easy software applications. It was a really nice way of learning how coding works. Minecraft plugins require you to understand ANOTHER certain level of coding, on which I for example mean keeping track of players individually, instead of making 1 global variable.
     
    TigerHix likes this.
  14. Offline

    The_Doctor_123

    I have one problem with this tutorial. It suggests that you can start making Bukkit plugins without proper Java knowledge.
     
  15. Offline

    KraZ__


    The_Doctor_123 How come?

    AlmarM Well I know a few OOP concepts, I don't know everything about OOP but this tutorial was only made to help people. If you wish for me to change anything, please let me know.
     
  16. Offline

    The_Doctor_123

    KraZ__
    Be a little more specific with your question.
     
  17. Offline

    Garris0n

    You can. I did. You just come to regret it afterwards...
     
  18. Offline

    The_Doctor_123

    So did I, and I regretted it as well. Why not help others make the right choice?
     
  19. Offline

    Garris0n

    I was simply pointing out what you said was technically incorrect. You can make plugins without learning Java. IT certainly shouldn't be encouraged, that's for sure, but you can do it. The thing is (completely) new developers don't know the difference between creating a collage of copy-pasted or memorized code and actually writing the code yourself.
     
    The_Doctor_123 likes this.
  20. Offline

    KraZ__


    How come? How does it give people that impression?
     
  21. Offline

    The_Doctor_123

    KraZ__
    Because OOP should be learned before you even view these forums.
     
  22. Offline

    KraZ__


    It should but day by day you get these people coming on here so where's the harm in educating them? I mean they're coming on here anyway, so why not?
     
  23. Offline

    The_Doctor_123

    KraZ__
    You don't get them deeper in the hole by having them read this, you redirect them to learn Java.
     
  24. Offline

    Iaccidentally

    Just a few little points here:
    • You most certainly can make Bukkit plugins without knowing OOP (or even anything more than basic Java), however, you will be very limited in what you can do. Learning the finer points of Java and OOP is what opens up an entire world of new possibilities. (This is not me recommending people try writing plugins before learning to code, just pointing out that it is possible).
    • The tutorial isn't bad, but I feel like it doesn't really take the final step of applying what it says to Bukkit. Everything here could be found in any other decent OO tutorial. I feel like the reader would finish and say "Great, now what does that have to do with what I'm doing and how do I use it?".
    • I'd guess about 3/4 of Bukkit plugin developers probably don't fully understand and use Object Oriented design. The Bukkit API makes plugin writing more akin to scripting than OOP (which is not a bad thing at all), however, some of the best things that can be done require a real knowledge of how all this works. Most plugins don't create their own objects, they simply use and modify ones from the Bukkit API.
     
  25. Offline

    xTrollxDudex

    You're technically not allowed to post Java tutorials in that sections (which a crazy amount of people happening to break these days)

    Removed false information post and moved to correct section.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 17, 2019
  26. Offline

    Junrall



    Once again... I disagree with this statement or any statement similar to this.

    It is true that a person should learn some basics before jumping in feet first, however tinkering with bukkit plugins is a fun way to learn some aspects of java. It is very satisfying and rewarding for some people to be able to make changes to some code, then load it up into Minecraft and see what the outcome is. If the outcome is not what they expected , they figure it out by researching, asking questions here on the forums, and experimenting. This is fun for all levels of programmers who are Minecraft fans.

    As for this tutorial... I'm glad that KraZ__ posted this tutorial! Tutorials like this give people a "stepping stone" to jump from. It sparks their "inner tinkerer" to thrive for more knowledge on the subject of OOP.

    Besides, if people aren't allowed to "look under the hood" and tinker, then how do you expect those same people have a desire to become a mechanic?

    KraZ__
    Nice work!
     
  27. Offline

    RawCode

    if you going to learn oop from random articles you will have problems.

    go oracle and read documentation
     
  28. Offline

    KraZ__


    People come here all the time without a basic understanding of object-oriented programming. My point still stands to why not educate them a little instead of sending them to a poorly formated documentation page where they will struggle to understand. Besides my tutorial uses a little less formality so it should make it much easier for those who don't speak english or understand formal english words.
     
  29. Offline

    RawCode

    people who come here to learn java (not bukkit API) have absolutely no change to advance over "copypaste code from tutorial and post one more hat plugin"
     
    KraZ__ likes this.
Thread Status:
Not open for further replies.

Share This Page