Classes and Objects in Java · Wiki · W.A. Wood / JISA · GitLab Skip to content GitLab Menu Projects Groups Snippets Loading... Help Help Support Community forum Keyboard shortcuts ? Submit feedback Contribute to GitLab Sign in Toggle navigation Menu J JISA Project information Project information Activity Labels Members Repository Repository Files Commits Branches Tags Contributors Graph Compare Issues 0 Issues 0 List Boards Service Desk Milestones Merge requests 0 Merge requests 0 CI/CD CI/CD Pipelines Jobs Schedules Deployments Deployments Environments Releases Monitor Monitor Incidents Packages & Registries Packages & Registries Container Registry Analytics Analytics Value stream CI/CD Repository Wiki Wiki Snippets Snippets Activity Graph Create a new issue Jobs Commits Issue Boards Collapse sidebar Close sidebar Open sidebar W.A. Wood JISA Wiki Classes and Objects in Java Last edited by William Wood Nov 29, 2018 Page history Classes and Objects in Java Classes and Objects in Java This page is designed to give you an overview over what classes and objects are and how they are implemented in Java. Object Orientated Programming (OOP) OOP is a programming paradigm where emphasis is put on the individual entities that can perform certain actions. To help understand what on Earth that means, let's consider the polar opposite of OOP: procedural programming where the focus is on actions being performed ON entities, and let's use the example of reading from a file to illustrate these definitions. In procedural programming, for example in MATLAB, we would open and read a file like so: file = fopen('path/to/file', 'r');
line1 = fgetl(file);
line2 = fgetl(file); What has happened here is that the method fopen() has opened the file and returned a "handle" or "identifier" (which is basically just a number) that corresponds to the now open file. From this point onwards we can read lines from the file by using fgetl() and supplying it with the identifier to tell it which file to read from. Therefore, we can see that in procedural programming what normally happens is we have a suite of methods/functions that we give an identifier to to perform an action. That is, in a sense, we act ON the entity (in this case a file). In OOP, for example in Java, things are slightly different: BufferedReader file = new BufferedReader(new FileReader("path/to/file"));
String line1 = file.readLine();
String line2 = file.readLine(); As we can see, the difference is that file is now an object and instead of passing it to a readLine method, it has such a method as a member, ie we write file.readLine(). Therefore, the file is now the focus of the code and the way it reads is that the entity itself performs the actions, as opposed to file being just an argument and the methods being the focus of how the code reads. There many advantages to such a paradigm over procedural programming but perhaps one of the simplest is the way it categorises actions based on what type of object we are talking about. For instance, a String object has methods member methods like .trim() and .toLowerCase() but these methods are not present in an Integer object as they don't make sense in such a context (ie what is a "lower case" number exactly?). If this were done procedurally, trim() and toLowerCase() would be methods just floating around in our default namespace and it would be perfectly possible to write code where we pass them an integer instead of a string and cause a runtime error as a result. It would be up to you to read the documentation (assuming it exists) to know what you can and cannot pass as arguments to these methods. Classes To define different types of objects, we have classes. Writing a class tells the compiler what properties and what functionality an object of a given type has when created. In Java, classes are normally public which means that they can be accessed from anywhere inside your programme but also from other programmes that include your programme as a library. Each public class in Java must be in its own file with its file name the same as the class name. For example, if we wanted to define the class Person to represent a person (for example in an address book) we would write in the file Person.java: public class Person {
} Inside a class we can add four different things: properties/variables, methods, constructors and other classes. These are each covered in the relevant sub-sections below. Protection Levels You will often find key-words like public, protected and private dotted around inside a class. These are what's called protection levels and define how code external to that inside your class is allowed to interact with different parts of your class. If something is Public then it means that it can be accessed by anything, including code inside other classes. If something is Protected then it means that it can only be accessed by code inside the same class or another class that extends it. If something is Private then it means that it can be accessed inside the same class only, not even derived classes. Generally it is good to use a tight a level as possible. Often you will find that you have class variables/properties as private but then have methods that access them which as public. Class Properties Perhaps the most rudimentary thing to add to a class is variables, normally called class properties when in the context of a class. Defining this is simple, you just specify the protection level, variable type and name like so: public class Person {
private int id;
private String name;
private String phoneNumber;
} Now, every Person object that we create will have its own id, name and phoneNumber. You can refer to these inside your class by simply writing their names. However, if there's any ambiguity you can specify that you're talking about the name variable in "this" object by use of the this key-word, for example: this.name = "Name"; Class Methods Adding a method to a class is done similarly to variables, you specify the protection level, the return type, the name and arguments like so: public class Person {
private int id;
private String name;
private String phoneNumber;
public String getName() {
return name;
}
} Constructors A constructor is what's used to create a new object of your class. It defines what happens when it is created and what arguments are required to create it. To define a constructor you first specify its protection level, then write the name of the class and specify arguments like so: public class Person {
private int id;
private String name;
private String phoneNumber;
public Person(int id, String name, String phoneNumber) {
this.id = id;
this.name = name;
this.phoneNumber = phoneNumber
}
} In the case above we have specified that when we create a Person object we will require id, name and phoneNumber to be specified: Person myPerson = new Person(12, "Bob", "01234567890"); You can define multiple different constructors that take different arguments. If you do not define any constructors, the "default" constructor is assumed which takes no arguments: Person myPerson = new Person(); Clone repository Guides Getting Started Java Basics (Incomplete) Program Structure Instrument Basics Result Handling Basics Simple GUIs Using SMUs Using T-Controllers Using Lock-In Amps Using JISA in Python Interfaces and Threads Advanced GUI Creation Logging Configurations References SMU MCSMU LockIn Examples Testing SR830