Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Try asking for help in the chatbox
  • Basic Object Oriented Programming Concepts for Scripting and General Programming


    NovaGTX
     Share

    Recommended Posts

    This is not designed to be fully exhaustive with respect to the following topics, but merely to address them or give an example to those who are new to scripting or programming in general. Without further ado let’s look into these object-oriented programming concepts one by one. We will use general Java code examples so that you know how to implement OOPS concepts and can apply them to scripts you create.

    Core OOPS concepts are:

    1. Abstraction
    2. Encapsulation
    3. Polymorphism
    4. Inheritance
    5. Association
    6. Aggregation
    7. Composition

     

    Abstraction

    At a higher level, Abstraction is a process of hiding the implementation details and showing only functionality to the user. It only indicates important things to the user and hides the internal details, ie. While sending SMS, you just type the text and send the message. Here, you do not care about the internal processing of the message delivery. Abstraction can be achieved using Abstract Class and Abstract Method in Java.

     

    Encapsulation

    Encapsulation is the technique used to implement abstraction in object-oriented programming. Encapsulation is used for access restriction to class members and methods. Access modifier  keywords are used for encapsulation in object-oriented programming. For example, encapsulation in java is achieved using private, protected and public keywords.

     

    Polymorphism

    Polymorphism is the concept where an object behaves differently in different situations. There are two types of polymorphism – compile time polymorphism and runtime polymorphism.

    Compile time polymorphism is achieved by method overloading. For example, we can have a class as below.

    public class Circle {
    
    	public void draw(){
    		System.out.println("Drwaing circle with default color Black and diameter 1 cm.");
    	}
    	
    	public void draw(int diameter){
    		System.out.println("Drwaing circle with default color Black and diameter"+diameter+" cm.");
    	}
    	
    	public void draw(int diameter, String color){
    		System.out.println("Drwaing circle with color"+color+" and diameter"+diameter+" cm.");
    	}
    }

    Here we have multiple draw methods but they have different behavior. This is a case of method overloading because all the methods name is same and arguments are different. Here compiler will be able to identify the method to invoke at compile time, hence it’s called compile time polymorphism.

    Runtime polymorphism is implemented when we have “IS-A” relationship between objects. This is also called as method overriding because subclass has to override the superclass method for runtime polymorphism.

    If we are working in terms of superclass, the actual implementation class is decided at runtime. Compiler is not able to decide which class method will be invoked. This decision is done at runtime, hence the name as runtime polymorphism or dynamic method dispatch.

    Shape is the superclass and there are two subclasses Circle and Square .

    package com.db.test;
    
    public interface Shape {
    
    	public void draw();
    }
    
    package com.db.test;
    
    public class Circle implements Shape{
    
    	@Override
    	public void draw(){
    		System.out.println("Drwaing circle");
    	}
    
    }
    
    package com.db.test;
    
    public class Square implements Shape {
    
    	@Override
    	public void draw() {
    		System.out.println("Drawing Square");
    	}
    
    }

    Inheritance

    Inheritance is the object oriented programming concept where an object is based on another object. Inheritance is the mechanism of code reuse. The object that is getting inherited is called superclass and the object that inherits the superclass is called subclass.

    We use extends keyword in java to implement inheritance.
     

    class AbstractScript {
    
    	public void foo(){
    		System.out.println("AbstractScript");
    	}
    	
    }
    
    class SubClass extends AbstractScript{
    		
    	public void bar(){
    		System.out.println("SubClass");
    	}
    	
    }
    
    public class Test {
    	public static void main(String args[]){
    		SubClass a = new SubClass();
    		
    		a.foo();
    		a.bar();
    	}
    }

     

    Association

    It defines that one object has reference of the other. In other words, we can say relationship between two classes defined by their objects. Associativity could be of type one to one, one to many, many to one or many to many. In the following example, we define address of an Employee using Address class object. It defines Aggregation between Employee and Address. In other words, Employee "HAS-A" Address. This kind of relationship is called "HAS A" relationship.

    class Employee{
        String empName;
        Address empAdress;
        int empAge;
        Employee(String empName, Address empAddress, int empAge){
            this.empName = empName;
            this.empAddress = empAddress;
            this.empAge = empAge;
        }
    }
    
    class Address{
        String city;
        String landmark;
        String societyName;
        Address(String city, String landmark, String societyName){
            this.city = city;
            this.landmark = landmark;
            this.societyName = societyName;
        }
    }

     

    Aggregation

    Aggregation is a special type of association. In aggregation, objects have their own life cycle but there is an ownership. Whenever we have “HAS-A” relationship between objects and ownership then it’s a case of aggregation.

     

    Composition

    Composition is a special type of Aggregation. Unlike Aggregation, It is more of a restrictive relationship between two classes.  If composition of an Object A contains another Object B then if A does not exist, B can not have it’s existence. For this example, A Company contains number of Employee, Employee can not exist if there is no Company. This relationship is a Composition between Company and Employee.

    class Company{
        String companyName;
        String CEOName;
        int numberOfEmp;
        ArrayList<Employee> empList;
        Company(String companyName, String CEOName, int numOfEmp, ArrayList<Employee>){
            this.companyName = companyName;
            this.CEOName = CEOName;
            this.numOfEmp = numOfEmp;
            this.empList = empList;
        }
    }
    
    class Employee{
        String empName;
        Address empAdress;
        int empAge;
        Employee(String empName, Address empAddress, int empAge){
            this.empName = empName;
            this.empAddress = empAddress;
            this.empAge = empAge;
        }
    }
    Edited by NovaGTX
    Link to comment
    Share on other sites

    • 3 weeks later...

    Yes thanks a lot. I'm still a bit fuzzy on the first three but other than that I'm understanding. One of the things I don't understand still is how you know how to "phrase" it, and how you know exactly what to write. But I'm pretty sure it's my lack of Java knowledge.. Im not sure. 

    Link to comment
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now
     Share

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.