Classes in Java

A class is a user-defined blueprint or prototype from which objects are created. It represents the set of variable and methods that are common to all objects of one type.

Modifiers : A class can be public or has default access

Class name: The name should begin with alphabets and most of the time we follow CamelCase convention, the First letter of all the words are in Upper case.

Superclass: Like everyone in the real world, even Java classes can have parents, these parents are called as Superclass, Any given Class can have only one class as Parent. (Do not ask whether it is the mother or father)

Interfaces: Interfaces are empty containers which set the rules for other interfaces and for classes.

Body : A class body is contained within curly braces { }, the class can contain methods, variable, inner classes, constructors, static blocks, default blocks

Object : As everyone says, it is like a real object in our world, But Objects are nothing but the container of the non-static elements in the class, Objects will have value, address

Object Creation

We can create an object for any class in Java, to create we will use most preferred way. i.e with the new keyword.

Type variable = new constructor();
A objA = new A();

Type is nothing but the type of the object

a variable is nothing but any name for the object

new is the keyword to reserve the memory space on the stack

Constructor is the one which will loads all the non-static elements in the class into space that reserved by the new keyword, Constructor name is the same as the Class name

We can access all non-static members of the class via object reference with the dot operator.

public class A { public void test() { System.out.println("test method"); } public static void main(String[] args) { A objA = new A(); objA.test(); } }

Constructors in Java

A Constructor is a block of code that allows you to create an object of the class. This can also be called creating an instance.

The constructor looks like a method but it’s not, for example, methods can have any return type or no return type but constructors don’t have any return type not even void.

Constructor has following properties:

  • Constructor has same name as class name.
  • It is used for initializing variables of a class.
  • It is called whenever an object of a class is created.
  • It does not have a return type, not even void.
  • It can have parameters.
  • Constructors can be overloaded, which means a class can have more than one constructor.
  • Overriding does not applicable for a constructor, as the overriding occurs on inheritance with different classes.
  • A constructor may have any access modifier like private, protected, default, public.
  • The default constructor is automatically inserted when Compiler does not find any valid declaration of a constructor inside a class.

Types of Constructor:

  • Default Constructor
  • No-Param constructor
  • Parameterized Constructor
  • Copy Constructor

Default Constructor :

Default Constructors will not accept any parameters; default constructor is created during the compile time.

The default constructor will be created only in case of when the user does not provide a constructor.

Why do we need default constructor ? At the beginning of an object's life, the Java virtual machine (JVM) allocates memory on the heap to accommodate the object's instance variables.

When that memory is first allocated, however, the data it contains is unpredictable. If the memory were used as is, the behavior of the object would also be unpredictable.

To guard against such a scenario, Java makes certain that memory is initialized, at least to predictable default values before it is used by any code.

The default constructor assigns the default values to the non-static elements present in the class

In below example, there is no constructor a default constructor will be formed by the constructor, as like everyone I also think that default constructor may look like No-Param constructor

public class A { public void test() { System.out.println("test method"); } public static void main(String[] args) { A objA = new A(); objA.test(); } }

No-Param constructor :

No-param constructor is nothing but a constructor without any parameters, Once a constructor is present in the class then compiles will not create a constructor.

public class A { // no param constructor public A() { // constructor block } public void test() { System.out.println("test method"); } public static void main(String[] args) { A objA = new A(); objA.test(); } }

Parameterized Constructor :

A constructor having parameter is called parameterized constructor, parameterized constructor can have any number of parameters and any type of parameters.

public class B { // no param constructor public B(int i, boolean flag) { System.out.println("Parameterized constructor"); } public void test() { System.out.println("test method"); } public static void main(String[] args) { B objA = new B(10, false); objA.test(); } }

Copy Constructor :

As the name suggests it create a copy of the constructor, A copy constructor is a constructor that takes only one argument which is of the type as the class in which the copy constructor is implemented.

For example, let us assume a class namely Animal and it has a constructor called copy constructor which accepts only one argument and that too type of Animal.

Copy constructors are widely used for creating duplicates of objects known as cloned objects. The duplicate object in the sense the object will have the same characteristics of the original object from which duplicate object is created.

But we have to ensure that both original and duplicate objects refer to different memory locations. So we are free to use copy constructors instead of clone method in java, but recommended is the clone method.

class Foo { public int x; public int y;
// Sample constructor Foo(int x, int y) { this.x = x; this.y = y; } // Copy constructor Foo(Foo foo) { this.x = foo.x; this.y = foo.y; } }
public class AD { public static void main(String[] args) { Foo fooA = new Foo(3, 5); // initialize foo1 Foo fooB = new Foo(fooA); // copy foo1 to foo2 fooB.x = 7; fooB.y = 8; System.out.println(fooA.x + " " + fooA.y); // prints "3 5" System.out.println(fooB.x + " " + fooB.y); // prints "7 8" } }

Constructor Overloading

Like methods, a constructor can also be overloaded. Overloaded constructors are differentiated on the basis of their type of parameters or number of parameters.

Constructor overloading is not much different than method overloading. In the case of method overloading, you have multiple methods with the same name but different signature, whereas in Constructor overloading you will have multiple constructors with a different signature.

The benefit of Constructor overloading: Benefit of having constructor overloading is Backward/Forward Compatibility. Say after some time you decide that Employee class is going to have one more property "age", along with the existing name.

Now what you do, If you change the parameterized constructor then you will have to change at all the places where you have used it.

So to make sure that your class is adapting to the new changes while keeping the old standards intact you create a new parameterized constructor with 2 parameters.

So now you have two constructors with a completely different and valid purpose.

class Employee { public Employee(String empName) { System.out.println("constructor with name"); } public Employee(String empName, int age) { System.out.println("constructor with name and age"); } }
public class EmployeeTest { public static void main(String[] args) { Employee emp = new Employee("karthiQ"); Employee emp1 = new Employee("Tom", 28); } }

Constructor Calling Constructor

We can call one Constructor from another constructor with the help of the this() method in Java, this method always should be the first line in the Constructor.

public class K { public K() { this(10); System.out.println("No param constructor"); } public K(int i) { System.out.println("Parameterized Constructor"); }
public static void main(String[] args) { K objK = new K(); } }

Output of constructor calling constructor

Parameterized ConstructorNo param constructor
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions