Basics, part 3


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 :

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;

IS-A vs. HAS-A relationships :

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.

Defining a new 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

Class vs. Object

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

Class Constructors

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(){ … }

Method Overloading

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.

This Keyword

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

Object Class

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;
public String getName() {
} = name;
public void setName(String name) {
public String toString() {
Since every class is implicitly derived from the Object class, you can refer to any class as an Object