A short guide to: Object oriented programming!

Discussion in 'Programming' started by xTimPugz, Apr 18, 2017.

  1. Hello

    The reason behind this guide is me not being able to take class names such as "Teleport", or "jump". Got any constructive feedback? Comment or PM me. Also, move this thread if you find any other section that's more suiting.

    I will cover the following topics - more will be added later on.
    1. What is OOP?
    2. Differences classes and objects
    3. The principles of OOP
    4. The static keyword
    5. Access modifiers
    6. Constructors
    7. Everything is an object!
    8. The this and super keyword
    9. Conventions
    10. OOP in Spigot

    1. What is OOP?
    Object oriented programming. Everything is an object! Object oriented (or in short: OO) is a paradigm used to write software that's more efficient, reusable and even better. You have many paradigms, but the one Java and C# primarily use is OO. And yes, you can actually write Java code that's more functional but just stick to the OO for plugins.
    2. Difference classes and objects
    This is a most vital part of OOP. A class is a blueprint, it describes what the object will look like, and that includes things such as fields, methods and so on. An object is an instance of this class. For instance (nice pun right?), you have a class called chair. If you look at the piece of code on your editor, that's your class, the blueprint. The object (or instance of this class) is the actual chair, which you can touch, and feel. Compare it to houses, if you have a blueprint, it'll tell you what it looks like, the materials, amount of floors. This is related to your class. Your object is the actual house.

    3. The *4* principles of OOP

    There are 4 principles which support this entire system of OOP. Yes, you could argue there are more, but these are the ones I find most important. Deal with it.
    • Inheritance
    • Encapsulation
    • Polymorphism
    • Abstraction
    Inheritance: you can make classes inherit from one another using the extends keyword in Java. This is particularly useful when you're dealing with classes that are in a parent-child relation. There is a rule you'll have to remember though: The child should inherit EVERYTHING from the parent! If you have one field for instance that the parent does not have, you're probably making a mistake.

    Encapsulation: you make fields private or protected and encapsulate all data from the outer world which they don't need. This is (extremely) handy if you have a lot of code, you won't get as confused messing through your thousands of lines of code. Also, separation of concerns! If you are in a team, and you want to write a module for a plugin, you'll want the main code encapsulated with only the interface visible. Same principle counts for hardware, you probably don't feel like checking what socket type you have before you calculate 1 + 1.

    Polymorphism: Multiple forms. You can use polymorphism through the following concepts in Java:

    • Overloading
    • Generics
    • Inheriting
    Overloading: You can define constructors and methods with the same name, but different parameters. Instance of 2 method overloads:
    Code (Text):

    //No parameters added, this one uses a default value.
    public void run(){
    //Parameters are different! This one has a distance specified making it possible to pass that to the walk method!
    public void run(int meters){
        player.walk(Walkspeed.Fast, meters);
    And then constructor overloading:
    Code (Text):

    //A constructor that sets the name and age fields. The this keyword will be explained here too.
    public Person(String name){
        this.name = name;
        this.age = 0;
    //A constructor that sets the name and age fields but here the age is passed into the constructor.
    public Person(String name, int age){
        this.name = name;
        this.age = age;

    Abstraction: Important one! Abstraction is generalizing something. To make it more abstract, is to make it less detailed in terms of implementation. You can make classes abstract by using the abstract keyword. This allows you to define some sort of interface you can separate from its implementation. Warning; this is not an interface you define with the keyword interface.

    4. What is the static keyword?
    Everything is an object. But what if you don't feel like using an object for a certain function? This is where the static keyword comes in. Static methods (or fields, classes) don't operate on an instance of a class. You can declare methods, fields, classes and other stuff static. Just beware; static data might cause some side effects!
    5. What are access modifiers?
    Access modifiers are keywords that tell the computer how accessible something (such as a field) is. If you want your method to be accessible from everywhere, you'd use public. There are a couple of these:
    • public
    • private
    • protected
    • No modifer is also one!
    public makes it accessible to all other objects, private makes it accessible only to the class itself and protected makes it accessible to everywhere except for the world. Here's the full list:
    • public: class, package, subclass and world
    • protected: class, package and subclass
    • No modifier: class, package
    • private: class
    Credit for this exact data goes to Oracle. Thanks, Oracle!
    6. Constructors
    A constructor of a class is kind of like a method but gets called whenever you instantiate a new Object(params...);. Constructors are used to instantiate an object's data mainly. Example:
    Code (Text):

    package me.xtimpugz.guide;

    public class SomeProgram {

        public static void main(String[] args) {
            Potato potatoHenk = new Potato("Henk");
            Potato potatoFrederick = new Potato("Frederick");
            Potato potatoFish = new Potato("Fish");


    Code (Text):
    package me.xtimpugz.guide;

    public class Potato {

        private String nickname;

        public Potato(String nickname) {
            this.nickname = nickname;

        public String getNickname() {
            return nickname;
    As you can see, constructors are very useful to pass data, otherwise you'd have to use getters and setters to get the data from your main class to Potato class.

    7. Everything is an object? What?!
    Don't take that as serious as you just did, please. Not EVERYTHING is an object, but I assure you, you'll surely encounter a load of them. All pieces of data you work with are objects, for instance a variable with type Chair is an object, an Integer is an object and so on. Basically, all classes by default inherit from the super class "Object". Methods are not however. To clarify what it means, we should look at real life. If I asked you to define an object, you'd probably define an object as a "thing" or even as an "object" (which isn't really smart, you don't clarify words using them). Well, that's not really wrong. In my own words, I'd describe an object in real life as a a tangible object, that has properties and behavior. Let's look at a few examples!

    For instance, a chair. This could be a class called Chair. A few properties (or fields, or attributes, there is a small nuance but that won't matter for now) could be:

    • Color
    • Material
    • Age of the chair
    and so on and so forth.
    Behavior could be:

    • Collapse (if it's a collapsable chair)
    • To cradle (Not sure if this is the correct term)
    And so on.
    8. The 'this' and 'super' keyword
    The 'this' keyword operates on the current instance and the super keyword on the base class. The this keyword is able to access stuff that's in the class itself basically, and won't use the base class' fields/methods. You can also use the this keyword to call a constructor within a constructor without creating another object of this type.

    The base keyword does the same basically but then for the base class (super class, parent, head class, however you want to call it).
    9. Conventions within Java
    Java is a language. In order for a language to be a good language and universally understood, we need clear Conventions. A convention is an agreement between programmers to keep the code readable for anyone. That results in having better code, which is more maintainable and scalable. Conventions include
    • Naming
    • Passing of variables
    • Scopes
    • Access modifying.
    Naming: always use 'Pascal Casing' for classes, 'camel Case' for fields and methods. There are a few others out there, but that's out of the scope of this guide.
    Passing of variables: You are supposed to create a constructor that gets passed the data on creation. You could of course use getters and setters on the line after, but that's less good as you might deal with different threads and they might access this object in the time between.
    Scope: Very important one and will surely help you avoid a lot of trouble. Declare your variables as 'deep' as possible. Avoid fields to possible extents!
    Access modifying: In order to have accessible objects, you need to add access modifiers. We discussed this earlier. They are a vital concept in encapsulation and you'll use them all the time.

    10. Sounds fun, but this is Spigot, not some 3D printer shop!
    Please do realise that whatever code you write, you'll always be stuck with OOP somehow. It's Java and that's not going to change (hopefully!). Here's an example of a small OOP-structured plugin (the classes won't do anything in the game, they only hold data in here).
    Code (Text):

    public class MyPlugin extends JavaPlugin{
        //A method we inherited from the JavaPlugin class.
        public void onEnable() {
            //We create a new chair of the type Chair by calling the constructor with 3 parameters.
            Chair chair = new Chair("Oak", true, false);
    Code (Text):
    public class Chair {
        //Should explain itself. These are fields. Kind of properties the chair has.
        private String material = "Oak";
        private boolean collapsable = true;
        private boolean collapsed = false;

        //This is a constructor! Some sort of method (but not quite) which gets called whenever an object of that type is made.
        public Chair(String material, boolean collapsable, boolean collapsed){
            this.material = material;
            this.collapsable = collapsable;
            this.collapsed = collapsed;
        //A regular method. This one prints to the console! Beware this is not supposed to be done, but I'll do it to keep simplicity.
        public void cradle(){
            System.out.println("BOOOOGOOOOHOOHOHOH! Spoopy.");
        //A bunch of getters and setters! These are supposed to set and get the values stored in the fields.
        public String getMaterial() {
            return material;

        public void setMaterial(String material) {
            this.material = material;

        public boolean isCollapsable() {
            return collapsable;

        public void setCollapsable(boolean collapsable) {
            this.collapsable = collapsable;

        public boolean isCollapsed() {
            return collapsed;

        public void setCollapsed(boolean collapsed) {
            this.collapsed = collapsed;
    If you see any code that's not clear, or any questions or constructive criticism, tell me. Also, if you see anything that's not following the conventions, tell me! I am a C# developer, so I will probably manage to make a few mistakes somehow.

    I hope you learned something, I know this wasn't a really long tutorial but it probably will help out at least a few peeps. If you want me to make another **BIGGER** guide, like this post or comment something.

    #1 xTimPugz, Apr 18, 2017
    Last edited: Apr 19, 2017
    • Informative Informative x 2
    • Like Like x 1
  2. latiku


    One word; conventions.
    • Agree Agree x 1
  3. As I mentioned below the snippet , I did not follow conventions for the sake of simplicity.
  4. latiku


    Contradictory to the header paragraph, in which you state that you're tired of users who name their classes things like "jump", aka poor conventions. Using bad conventions isn't a proper way to introduce someone to OOP.
  5. If you show me where I made mistakes tell me. I will correct them. Just know that it's not my intention to make this a too complex post, hence removing the polymorphism and inheritance. I just saw your edit. So you think I should make it a little bit more difficult? If that's the case, I will gladly improve this.
  6. maldahleh

    Wiki Team

    I don't think he means you should make it more difficult, but you should demonstrate stuff like how a constructor works with objects, attributes should be private, methods should start with a lowercase, and so forth.
  7. Ah yes! I forgot the method names. I learn C# mainly, hence the upper case. Thanks for the feedback guys! I'll improve this right away. Just know that this is my first guide.
  8. I got some comments to your example:
    • You should add a package declaration. This is the first step many people fail at.
    • I don't see any reason why you call the methods #onEnable and #onDisable of the super class. You declared them as overriding, so thing is, that you not want to execute the methods of the super class. Furthermore these methods were empty by the implementation of JavaPlugin.
    • Why is the inner class (Chair) static? That breaks OOP principles. It isn't static.
    • Why are the fields of Chair public? In most cases, it's better practice to make them private and provide getters and setters if necessary,
    • For tutorial examples you definitely should follow the conventions given by Oracle (e.g. field / method naming conventions). You're publishing bad practices otherwise. Take time to make it properly.
    All in all i think it's a bad tutorial for learning OOP.
    • You did not explain the difference between classes and instances
    • You did not explain how inheritance works
    • You did not mention polymorphism
    • You did not follow conventions
    • You did not explain access concepts (public / protectet / private)
    • You did not mention primitive datatypes
    • You did not explain, what "static" acually should be used for when programming OOP like
    • You only showed one specific point
      • i don't think anyone will understand what OOP really is about after reading this
      • all in all OOP can't be explained by a single forum thread, don't even try it

    EVERYTHING is an object in Java WHEN following OOP concepts completely. Period.
    Of course there are couple of "primitive datatypes" like boolean, int, long, float, double,... but you even can avoid these (int -> Integer). So everything can be an object (except ENUMS i guess).
    #8 Michel_0, Apr 18, 2017
    Last edited: Apr 18, 2017
    • Package declaration is added automatically in most IDEs and not needed to add here as I don't think anyone will use the same package name as I did.
    • The onEnable was used here to make it easier for people to test it, if they wanted they could copy the code and that way run the method called on chair on startup.
    • Edited, that was indeed a bad convention, just added it as a nested class to prevent people getting confused. It seems like it caused even more confusion, my bad.
    • Edited that too, it's not necessarily a bad convention, I don't like it either and I use getters and setters now to be more accurate.
    • Edited that, too. I think you didn't see the edit.
    You're allowed to think this is a bad tutorial, I agree that it does need improvement, that's why I'm asking feedback ;)
    • Now that's actually a very good idea! Thanks I will add this ASAP
    • Inheritance polymorphism and such aren't for a basic tutorial on a Spigot forum I think. As I mentioned in the last few lines, that'd be for a bigger one ;)
    • Conventions fixed thanks to the other feedback.
    • Indeed a good idea, but that's for another tutorial.
    • Good idea to add for this one!
    • Good idea fo this one too.
    • True, not all of OOP can be explained in one guide, that is why I want to make a couple of guides.
    I appreciate the feedback, but check the edit please. :)
    Thank you!
    • Winner Winner x 1
  9. Yeah and they're probably better or at least equal to this one. :p
    Oh well. Keep in mind that I want to keep improving this one.
  10. I added more - If you can think of anything else OOP-related that's also very important: Tell me! Thanks for the feedback.
  11. Overall a fairly nice article and information in this seems legit and correct from what I can see. One thing I'd like to add that might be usefull to point out: No access scope is equal to public. You're mentioning that no scope is also a scope, which is true, but you never explained it furthur :p

    Edit: In Java it looks like no access scope differes from C#...

    See here for java: http://image.prntscr.com/image/7a912570e88e4e44896e5ed1d51c5053.png

    Also, there's a few more scopes (in C#), which is the internal and protected internal. They don't exist in Java I think so no point in adding them to this guide I suppose.
  12. Yeah I also took a look at that table. :)
    Thank you!
  13. Should of covered how everything is actually a copy of an object (unless it's concurrent) and not a reference (reason why not to Java :().
  14. Excellent idea! Will add that when I get home!
    • Like Like x 1
  15. Strahan


    Nice that you want to help people, but generally one should be an expert at something, or at least vastly experienced, before trying to teach.
  16. I am not an expert, that's right. But that doesn't matter, as long as the information is correct IMO. And if you think that the information is bad or simply wrong, tell me. Personally I think this is a pretty decent guide now after improving it :) It'll at least give some people a basic idea of OOP.
  17. @xTimPugz This guide was very interesting for me to read. Well, I am german tho so some terms were hard to understand for me because I used to be more present in german-speaking forums (Tho lot's of words are the same in english as well as in german related to programming). But I'd like to hear your personal opinion on oop. C and Java are often shown as opponents, so what's your opinion on that? Do you prefer the principle of oop?
  18. I don't really have an opinion on OOP as I believe that in order to have an opinion on something, you should've tried the alternatives. I haven't tried any other kinds of programming except for a little bit of functional programming. But that wasn't nearly enough to really get an idea of functional programming. It's been years since my first time in OOP and I remained a fan ever since. I do have one small thing which I like about OOP, is the fact that you can really adapt it to real life. The explanation about the house wasn't chosen randomly of course. :) So in short: I do prefer OOP but have no real ways of comparing.(All languages I used are pretty straight forward without needing an indepth level of knowledge of the paradigma).

    Just in case you're wondering these are my languages which I use (haven't mastered one yet, I believe that takes at least 10 years):
    • C#
    • Java
    • JavaScript
    • Php
    • Simple markup languages such as Html
    • Css and other related technologies such as LESS or SASS
    • C (only in a really simple arduino environment)
    • A little bit of Lua in my days of scripting addons for Eso :)