Java and Object Oriented Programming [#2]

Java and Object Oriented Programming [#2]

Recap

In the previous article, we discussed Object-Oriented Programming, objects, and classes, as well as how to access members of a class. We also saw examples of these concepts in the first article. Check out that article #1

In today's article, we are going to look at Constructors, their types and how to use them. This will be backed by examples.

What is a Constructor?

A constructor in Java is similar to a method that is invoked when an object of the class is created.

Specifically, a constructor has the same name as that of the class and does not have any return type. Why constructors don't have return types.

An example to demonstrate this;

class Exam {
    Exam() {
       // Constructor body
   }
}

In this case, the Exam() is the constructor and it has the same name as that of the class and no return type.

NB: By default, there is a constructor when a class is created.

class Exam {
  private String name;

  // constructor
  Exam() {
    System.out.println("The constructor is called:");
    type = "Computer";
  }

  public static void main(String[] args) {

    // constructor is invoked while
    // creating an object of the Main class
    Exam obj = new Exam();
    System.out.println("The type is " + obj.type);
  }
}

Output:

The constructor is called:
The type is Computer

In the above demonstration, the constructor we created is Exam() and inside it we are initializing the value of the type variable. Take note of the creation an object for the Exam class

Exam obj = new Exam();

The value of the type variable is initialized and the program prints the value of the type variables as Computer.


Types of Constructors

There are three types of Constructors:

1. Java No-Arg Constructors

A Java constructor, like methods, may or may not contain any parameters (arguments).

A constructor is known as a no-argument constructor if it does not accept any parameters.

Example 1:

Constructor() {
    // body of the constructor
}

Example 2:

class Exam {

  int i;

  // constructor with no parameter
  private Exam() {
    i = 5;
    System.out.println("Constructor is called");
  }

  public static void main(String[] args) {

    // calling the constructor without any parameter
    Exam obj = new Exam();
    System.out.println("Value of i: " + obj.i);
  }
}

Output:

Constructor is called
Value of i: 5

In the above example, we have created a constructor Exam(). The constructor is referred to as a no-arg constructor because it does not accept any parameters.

It's also worth noting that the constructor has been declared private, meaning it can't be accessed from outside the class. Therefore creating objects from outside the class is prohibited using the private constructor.

2. Parameterized Constructor

A Java constructor can also accept one or more parameters. Such constructors are known as parameterized constructors Example:

class Exam {

  String languages;

  // constructor accepting single value
  Exam(String lang) {
    languages = lang;
    System.out.println(languages + " Programming Language");
  }

  public static void main(String[] args) {

    // call constructor by passing a single value
    Exam obj1 = new Exam("Java");
    Exam obj2 = new Exam("Python");
    Exam obj3 = new Exam("C");
  }
}

Output:

Java Programming Language
Python Programming Language
C Programming Language

In this case, the constructor named Main() takes a single parameter. Therefore the expression passes a single value to the constructor

Exam obj1 = new Exam("Java");

3. Java Default Constructor

When we don't create a constructor, the Java compiler automatically creates a no-arg constructor during the execution of the program, thus the default constructor.

class Exam {

  int a;
  boolean b;

  public static void main(String[] args) {

    // A default constructor is called
    Exam obj = new Exam();

    System.out.println("Default Value:");
    System.out.println("a = " + obj.a);
    System.out.println("b = " + obj.b);
  }
}

Output:

a = 0
b = false

In this case, we haven't created any constructors. As a result, the java compiler creates the default constructor.

Java Constructor Overloading

class Exam {

  String language;

  // constructor with no parameter
  Exam() {
    this.language = "Java";
  }

  // constructor with a single parameter
  Exam(String language) {
    this.language = language;
  }

  public void getName() {
    System.out.println("Programming Langauage: " + this.language);
  }

  public static void main(String[] args) {

    // call constructor with no parameter
    Exam obj1 = new Exam();

    // call constructor with a single parameter
    Exam obj2 = new Exam("Python");

      obj1.getName();
      obj2.getName();
  }
}

Output:

Programming Language: Java
Programming Language: Python

In this case, we have two constructors in these examples: Exam() and Exam(String language). Here both the constructor initialize the value of the variable language with different values.

NB: We have used this keyword to specify the variable of the class. Read more about this keyword.

For a further and more deep explanation about Object Oriented Programming, check out this wonderful playlist. Object Oriented Programming in Java by Kunal Kushwaha.

Feel free to connect with me on Linkedin, github and Twitter thank you.