Abstract Data Classes
So you don't need to rewrite everything 100 times
Data abstraction states that programmers should hide specific code from the user. This means that the user is only presented with information that is relevant, and shouldn't be able to modify or see how background processes work. This ensures programs operate properly and aren't tampered with. The OOP principle of encapsulation states that data should be kept abstracted from the user. Java implements this principle through abstract data classes. Through these data classes, Java controls what can be accessed from where in the application. Abstract data classes define new data types. For example, a new data type could be an Animal, which has data in form of what type of animal it is, how old it is, and other characteristics. Using fields and methods, we give these abstract data classes these properties and functions
Creation
public class MyClass{ // fields and methods}To create an abstract data class, you must first write the class declaration in a Java file. An example of this can be seen on the left.
Fields and Methods
Fields, usually declared at the top of the class, are variables of primitives, and other data classes represented by objects. These give the class the properties it needs to fulfill its role in the program. Methods give functionality to these properties and the class itself. One important method is the constructor, which is called when the class is being created as a object somewhere else in the program. The constructor sets initial values to fields to ensure the functionality of the whole class. Below, there are examples of fields and methods for the Point data class. Constructors, like other methods, can be overriden to give multiple ways to create the object.

public class Point{ // a point on a cartesian plane // fields int x; // the location on the x axis int y; // the location on the y axis // methods Point(int x, int y){ this.x = x; // setting fields, if there are two vars with same this.y = y; // name, use this.var to refer fields } void print(){ // void methods run code and have no return value System.out.println("Point is at " + x + ", " + y); this.y = y; // name, use this.var to refer fields } int getX(){ // accessor method, returns value of fields return x; // needs to return an int as it is a method of type int } void setX(int x){ // mutator method, sets value of fields this.x = x; }}Accessor Levels
Accessor levels implement in the principle of encapsulation into abstract data classes. Using keywords, Java controls what fields and methods are accessible and where they are accessible. Below are a list of keywords and a rewritten version of Point with proper keywords.
| Keyword | Properties |
|---|---|
| public | Field or method is accessible anywhere as long if class is correctly imported |
| private | Field or method is only accessible in the same class which it is in |
| protected | Field or method is accessible in the same package or subclass as the class it is in |
| None(like actually none) | Somewhere in between private and protected |
public class Point{ // access levels are implemented // fields private int x; // fields are usually private or protected private int y; // methods public Point(int x, int y){ this.x = x; // constructors are usually public this.y = y; } public void print(){ // most methods are public unless only to be used within class System.out.println("Point is at " + x + ", " + y); // then they are private/protected this.y = y; } public String toString(){ // special toString method that sets the String content of the object in a print method return x + " " + y; // always public } public int getX(){ // accessors and mutators are public return x; } public void setX(int x){ this.x = x; }}