Constructors in Java - Exam Notes with Examples
Definition and Key Features of Constructors
A constructor in Java is a special method that is used to initialize objects. It has the same name as
the class and does not have a return type.
It is automatically called when an object is created.
Key Features:
- Same name as class
- No return type (not even void)
- Automatically invoked at object creation
- Can be overloaded
- Used to initialize object variables
Types of Constructors
1. Default Constructor
Q1. Write a program that prints a welcome message using a default constructor.
class Welcome {
Welcome() {
System.out.println("Welcome to Java Programming!");
}
public static void main(String[] args) {
Welcome w = new Welcome();
}
}
Output:
Welcome to Java Programming!
Q2. Write a Java class that initializes a student's admission year using a default constructor.
class Student {
int admissionYear;
Student() {
admissionYear = 2025;
}
void show() {
System.out.println("Admission Year: " + admissionYear);
}
public static void main(String[] args) {
Student s = new Student();
s.show();
}
}
Output:
Admission Year: 2025
Q3. Create a Java class that displays a default interest rate for a bank account.
class Bank {
double interestRate;
Bank() {
interestRate = 3.5;
}
void showRate() {
System.out.println("Default Interest Rate: " + interestRate + "%");
}
public static void main(String[] args) {
Bank b = new Bank();
b.showRate();
}
}
Output:
Default Interest Rate: 3.5%
2. Parameterized Constructor
Q1. Write a Java program to store and display employee details using parameterized
constructor.
class Employee {
String name;
int id;
Employee(String n, int i) {
name = n;
id = i;
}
void display() {
System.out.println("Employee: " + name + ", ID: " + id);
}
public static void main(String[] args) {
Employee e = new Employee("John", 101);
e.display();
}
}
Output:
Employee: John, ID: 101
Q2. Create a class Rectangle and calculate area using parameterized constructor.
class Rectangle {
int length, width;
Rectangle(int l, int w) {
length = l;
width = w;
}
void area() {
System.out.println("Area: " + (length * width));
}
public static void main(String[] args) {
Rectangle r = new Rectangle(5, 4);
r.area();
}
}
Output:
Area: 20
Q3. Write a Java class to represent a Book using parameterized constructor.
class Book {
String title;
int pages;
Book(String t, int p) {
title = t;
pages = p;
}
void info() {
System.out.println("Book: " + title + " (" + pages + " pages)");
}
public static void main(String[] args) {
Book b = new Book("Java Basics", 300);
b.info();
}
}
Output:
Book: Java Basics (300 pages)
3. Copy Constructor
Q1. Write a program to copy student data using copy constructor.
class Student {
String name;
Student(String n) {
name = n;
}
Student(Student s) {
name = s.name;
}
void show() {
System.out.println("Student Name: " + name);
}
public static void main(String[] args) {
Student s1 = new Student("Anita");
Student s2 = new Student(s1);
s2.show();
}
}
Output:
Student Name: Anita
Q2. Write a Java program to clone an Account object using copy constructor.
class Account {
String accountNo;
Account(String acc) {
accountNo = acc;
}
Account(Account a) {
accountNo = a.accountNo;
}
void display() {
System.out.println("Account No: " + accountNo);
}
public static void main(String[] args) {
Account a1 = new Account("123456");
Account a2 = new Account(a1);
a2.display();
}
}
Output:
Account No: 123456
Q3. Create a Java class to copy product details using copy constructor.
class Product {
String name;
double price;
Product(String n, double p) {
name = n;
price = p;
}
Product(Product p) {
name = p.name;
price = p.price;
}
void show() {
System.out.println(name + " costs $" + price);
}
public static void main(String[] args) {
Product p1 = new Product("Laptop", 999.99);
Product p2 = new Product(p1);
p2.show();
}
}
Output:
Laptop costs $999.99