Q1. Define a constructor in Java. Explain its types with an example.
A constructor in Java is a special method that is automatically called when an object is created. It
has the same name as the class and does not have a return type, not even void. Constructors are
primarily used to initialize the object’s data members when the object is created. If no constructor
is defined, the Java compiler automatically provides a default constructor.
Constructors can be of three types:
1. Default Constructor – Automatically created by the compiler if no constructor is defined. It
initializes variables with default values.
2. Parameterized Constructor – Takes parameters and allows assigning specific values during
object creation.
3. Copy Constructor – A user-defined constructor used to initialize one object using another
object of the same class.
Example:
class Student {
String name;
int age;
Student() { name = "Unknown"; age = 18; } // Default constructor
Student(String n, int a) { name = n; age = a; } // Parameterized constructor
void display() {
[Link](name + " - " + age);
}
}
Q2. Explain Method Overloading in Java with an example.
Method Overloading in Java refers to the ability of a class to have more than one method with the
same name but different parameter lists. This is achieved by changing the number, type, or order
of parameters. It is an example of compile-time polymorphism, as the method to be called is
determined during compilation. Method Overloading improves code readability and reusability,
allowing a single method name to handle different data types or argument counts.
Example:
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
public static void main(String[] args) {
Calculator c = new Calculator();
[Link]("Sum1: " + [Link](10, 20));
[Link]("Sum2: " + [Link](2.5, 3.5));
[Link]("Sum3: " + [Link](1, 2, 3));
}
}
Q3. What is the use of the 'this' keyword in Java? Explain with an example.
The 'this' keyword in Java is a reference variable that refers to the current object of the class. It is
used to differentiate between instance variables and local variables when they share the same
name. It can also be used to invoke another constructor in the same class or to pass the current
object as an argument to another method. By using 'this', code becomes more readable and
eliminates naming conflicts between local and instance variables.
Example:
class Employee {
String name;
double salary;
Employee(String name, double salary) {
[Link] = name; // Refers to instance variable
[Link] = salary;
}
void display() {
[Link](name + " earns ₹" + salary);
}
public static void main(String[] args) {
Employee e = new Employee("Ravi", 45000);
[Link]();
}
}
Q4. Explain Static Members in Java with an example.
In Java, when a member (variable or method) is declared as static, it belongs to the class rather
than to any specific instance of the class. Static members are shared by all objects of the class.
They are used when common data or methods need to be accessed without creating an instance.
Static methods can only access other static members directly and cannot use 'this' or 'super'
keywords. The 'static' keyword is also commonly used for defining constants and utility methods.
Example:
class Employee {
static int count = 0; // Static variable shared by all objects
String name;
Employee(String n) {
name = n;
count++;
}
static void showCount() {
[Link]("Total Employees: " + count);
}
public static void main(String[] args) {
new Employee("Amit");
new Employee("Riya");
[Link]();
}
}
Q1. Write a Java program to demonstrate Constructor Overloading.
class Student {
String name;
int age;
Student() {
name = "Default";
age = 18;
}
Student(String n, int a) {
name = n;
age = a;
}
void display() {
[Link](name + " - " + age);
}
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student("Riya", 21);
[Link]();
[Link]();
}
}
Q2. Write a Java program to overload the method calculate() to find area of square,
rectangle, and circle.
class AreaCalculator {
double calculate(double side) { return side * side; }
double calculate(double length, double breadth) { return length * breadth; }
double calculate(float radius) { return 3.14159 * radius * radius; }
public static void main(String[] args) {
AreaCalculator a = new AreaCalculator();
[Link]("Square: " + [Link](5));
[Link]("Rectangle: " + [Link](5, 10));
[Link]("Circle: " + [Link](7.0f));
}
}
Q3. Write a Java program to demonstrate use of 'this' keyword.
class Employee {
String name;
double salary;
Employee(String name, double salary) {
[Link] = name;
[Link] = salary;
}
void display() {
[Link]("Employee: " + name + " | Salary: ₹" + salary);
}
public static void main(String[] args) {
Employee e = new Employee("Ravi", 45000);
[Link]();
}
}
Q4. Write a Java program to demonstrate Static Members.
class Employee {
static int count = 0;
String name;
Employee(String n) {
name = n;
count++;
}
static void showCount() {
[Link]("Total Employees: " + count);
}
public static void main(String[] args) {
new Employee("Amit");
new Employee("Riya");
new Employee("Nisha");
[Link]();
}
}