Basics, part 3

Classes

A class is a structure that maintains state and behavior. It serves as a template or blueprint for an object. For example, you could define a Robot class. The state of the class would be properties like size, weight, propulsion, etc.
The behavior would be methods like communicate(), track(), etc.

Access modifiers control how accessible data is from outside classes.
* public: public data can be accessed by any class in the application
* protected: protected data can only be accessed by classes in the same package or a subclass (which can be in any other package)
* default: Data with no modifier is considered to have “default” access. This marks the item accessible by any class or subclass in the same package only private data is only accessible the class itself. No other class (or subclass) whether in the same package or not can access it.

This is a convention used by most developers to allow access to private fields (variables). A getter is a method that returns the value of a particular field. It always follows the naming convention of getField, where ‘Field’ is the name of the variable. In some cases, where the variable is a boolean, the convention is hasField. A setter is a method that sets the value of a particular field. It always follows the naming convention of setField, where ‘Field’ is the name of the variable. The following example demonstrates a class with a private field (named size) and a getter method and a setter method.

public class Robot { 
//private field
int size;
//getter method
public int getSize(){
return this.size;
}
//setter method
public void setSize(int size){
this.size = size;
}
}

An IS-A relationship represents a class that is a subclass of another.
A HAS-A relationship is a class that has a field that references another class.

When you create a new class it’ll exist in a .java file. The way to create a class is to use the class keyword and provide a name for your class.

public class Robot { 
//state and behavior related to a Robot
}

A class is a Java structure that you define. An object is a instance (something that exists in memory) that follows the class template.

A constructor is a specialized block of code that serves to initialize the state of a class. It is called once and is called immediately after you invoke the new keyword on this class in some code. A constructor will always have an access modifier and the name of the class followed by a list of arguments.
The example below demonstrates a no-arg constructor. It is a constructor that has zero arguments.

public class Robot { 
//an example constructor
public Robot(){ … }
}

You can overload a method by creating more than one method with the same name. The difference is in the parameters listed for the method. You can also change the return type or the exceptions thrown from a method, but the parameter list has to be different, otherwise, the compiler will complain. It will claim that you have duplicate methods and there is no way to differentiate between the two when the code wishes to call one over the other. Thus the parameter list has to be different.

The keyword, this, is a special constructor in Java that is used in a class to refer to the current instance.

The Object class is the parent of all classes in Java.
• toString() — provides a String representation of a class
• hashCode() — provides a unique identifier for the object
• equals() — returns true if this object is equivalent to another
• finalize() — this method is called just prior to the object being destroyed by the Garbage Collector
• clone() — creates a copy of the object

The Object class provides several convenience methods that are marked protected:
The following class demonstrates a way to override the toString() method:

public class Person {
private String name;
return this.name;
public String getName() {
}
this.name = name;
public void setName(String name) {
}
return this.name;
public String toString() {
}
}
Since every class is implicitly derived from the Object class, you can refer to any class as an Object

SOFTWARE ENGINEER — WEB DEVELOPER WITH HEART FOR FRONT-END, MINIMALISM AND RASMENTALISM.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store