Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » Java » Programs » Basic » Java program for demonstrating constructors
Suryateja Pericherla Categories: Basic. No Comments on Java program for demonstrating constructors
0
(0)

Aim

To write a program for demonstrating constructors.

 

Theory

A constructor is a special method which has the same name as class name and that is used to initialize the objects (fields of an object) of a class. A constructor has the following characteristics:

  1. Constructor has the same name as the class in which it is defined.
  2. Constructor doesn’t have a return type, not even void. Implicit return type for a constructor is the class name.
  3. Constructor is generally used to initialize the objects of a class.

 

Syntax for creating a constructor is as shown below:

ClassName( [Parameters List] )

{

            //Code of the constructor

}

 

A constructor is invoked (called) automatically whenever an object is created using the new keyword. Java automatically invokes the default constructor which initializes all the fields to their default values.


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


 

Based on the number of parameters and type of parameters, constructors are of three types:

  • Parameter less constructor or zero parameter constructor
  • Parameterized constructor
  • Copy constructor

 

Pseudo code/Steps

  1. Create a class Student.
  2. Create a parameter less constructor, parameterized constructor, and copy constructor inside Student
  3. Create class Driver.
  4. Create objects for Student class and invoke all 3 types of constructors.

 

Program

Following Java program demonstrates different types of constructors:

class Student
{
	String name;
	int age;
	char grade;
	
	Student()
	{
		name = "";
		age = 0;
		grade = '0';
	}
	
	Student(String n, int a, char g)
	{
		name = n;
		age = a;
		grade = g;
	}
	
	Student(Student t)
	{
		name = t.name;
		age = t.age;
		grade = t.grade;
	}
	
	void getDetails()
	{
		System.out.println("Student name is: " + name);
		System.out.println("Student age is: " + age);
		System.out.println("Student grade is: " + grade);
	}
}

class Driver
{
	public static void main(String args[])
	{
		Student s1 = new Student();
		Student s2 = new Student("Ramesh", 19, 'A');
		s2.getDetails();
		Student s3 = new Student("Suresh", 19, 'B');
		s3.getDetails();
		Student s4 = new Student(s2);
		s4.getDetails();
	}
}

 

Input and Output:

Student name is: Ramesh
Student age is: 19
Student grade is: A
Student name is: Suresh
Student age is: 19
Student grade is: B
Student name is: Ramesh
Student age is: 19
Student grade is: A

 

Result

The concept of constructor is understood and demonstrated successfully.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory