Lesson 3, Topic 1
In Progress

Java: Object Oriented Programming Review

Yasin Cakal 14 Oct 2021
Lesson Progress
0% Complete

Let’s review the basics of Object Oriented Programming in Java!

What are Instance Variables and Methods?

In Java, objects refer to instances of a class. Each object has state and behavior, which are represented by instance fields and methods. Let’s look at an example:

public class Employee
{
    // state or field of an object
    String employee_name;
    int employee_ID;
  
    // behaviors or methods of an object
    public void set_employee() {
        employee_name = "Joe";
        employee_ID = 111222;
    }
    public void get_employee() {
        System.out.println("Employee's name is " + employee_name);
        System.out.println("Employee's ID is " + employee_ID);
    }
  
    // This is the main method.
    public static void main(String [] args) {
        // creates a new Employee object.
        Employee example_employee = new Employee(); 
    
        // Adds a name and ID to the example_employee
        example_employee.set_employee();
    
        // Prints out example_employee details.
        example_employee.get_employee();
    }
}

Output:

Employee's name is Joe
Employee's ID is 111222

What is an Instance of an Object?

Java instances are class-based objects. For instance, Joe could be a member of the class Employee. Each instance has access to a set of variables referred to as instance fields, which are variables declared within the instance’s scope. The constructor method is used to assign values to instance fields.

public class Employee
{
    // state or field of an object
    String employee_name;
    int employee_ID;
    
    // Constructor Method for class Employee
    public Employee(String employee_name, int employee_ID) {
        this.employee_name = employee_name;
        this.employee_ID = employee_ID;
    }
  
    public void get_employee() {
        System.out.println("Employee's name is " + employee_name);
        System.out.println("Employee's ID is " + employee_ID);
    }
  
    // This is the main method.
    public static void main(String [] args) {
        // creates a new Employee object.
        Employee employee_1 = new Employee("Joe", 111222); 
        Employee employee_2 = new Employee("Mary", 333444);
    
    
        // Prints out employee details.
        employee_1.get_employee();
        employee_2.get_employee();
    }
}

Output:

Employee's name is Joe
Employee's ID is 111222
Employee's name is Mary
Employee's ID is 333444

What is the Dot Notation in Java?

To access the variables and methods of an object or a class in the Java programming language, we use the . dot notation. Line 26 in the program above, employee_1.get_employee();, is an example of how dot notation is used.

What is a Constructor Method in Java?

Java classes include a constructor method for instantiating the class. The constructor has the same name as the class. If no constructor is defined, an empty constructor is used as a fallback. Here is an example:

public class Employee
{
    public Employee() {
        System.out.println("Default constructor message.");
    }
  
    // This is the main method.
    public static void main(String [] args) {
        // creates a new Employee object.
        Employee employee_1 = new Employee(); 
    }
}

Output:

Default constructor message.

How do you create a New Instance of a Class?

In Java, we create a new instance of a class by using the new keyword followed by a call to the class constructor. The constructor method can be used to initialize instance fields. Here is an example:

public class Employee
{
    String name;
    public Employee(String name) {
        this.name = name;
    }
    public static void main(String [] args) {
        // New instance of the Employee class
        Employee employee_1 = new Employee("Joe");
        System.out.println("Employee's name = " + employee_1.name);
    }
}

Output:

Employee's name = Joe

What are Reference Data Types?

A variable with the reference data type has a value that refers to an instance’s memory address. When a variable is declared, its type is determined by the class name. Here is an example:

public class Employee
{
    public Employee() {
    }
    public static void main(String [] args) {
        // employee_1 has a reference type Employee
        Employee employee_1 = new Employee();
        System.out.println(employee_1);
    }
}

Output:

Employee@5a07e868

What are Constructor Signatures?

Multiple constructors may exist within a class as long as their parameter values are distinct. A signature enables the compiler to distinguish between various constructors. A signature contains the constructor’s name and a list of the constructor’s parameters. Here is an example:

public class Employee
{
    String employee_name;
    int employee_ID;
    
    /* The following is the signature:
       Employee(String employee_name, int employee_ID)
    */
    public Employee(String employee_name, int employee_ID) {
        this.employee_name = employee_name;
        this.employee_ID = employee_ID;
    }
    public static void main(String [] args) {
        Employee employee_1 = new Employee("Joe", 111222);
    }
}

What are null Values in Java?

null is a special value that indicates that an object’s reference is void. Let’s take a look at an example:

public class Employee
{
    String employee_name;
    public Employee(String employee_name, int employee_ID) {
        this.employee_name = employee_name;
    }
    public static void main(String [] args) {
        Employee employee_1 = new Employee("Joe", 111222);
        System.out.println(employee_1);
        employee_1 = null;
        System.out.println(employee_1);
    }
}

Output:

Employee@5a07e868
null

What is inside a Method in Java?

What are Method Parameters in Java?

How are Variables inside Methods used?

What is the return Keyword?

How do you Declare a Method?

%d bloggers like this: