0% found this document useful (0 votes)
4 views3 pages

Constructor Java ICSE10 Notes

A constructor in Java is a special function that initializes an object's data members and is executed automatically upon object creation. There are two main types of constructors: default (without parameters) and parameterized (with parameters), and constructors can be overloaded. Key features include the same name as the class, no return type, and automatic invocation, while limitations include that they cannot be inherited or overridden.

Uploaded by

ashmitk1985
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Constructor Java ICSE10 Notes

A constructor in Java is a special function that initializes an object's data members and is executed automatically upon object creation. There are two main types of constructors: default (without parameters) and parameterized (with parameters), and constructors can be overloaded. Key features include the same name as the class, no return type, and automatic invocation, while limitations include that they cannot be inherited or overridden.

Uploaded by

ashmitk1985
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Constructor in Java – Detailed Point Wise Notes (ICSE Class

10)

Introduction
• Constructor is a special function of a class.
• It initializes the data members (instance variables) of an object.
• It is executed automatically when an object is created.

Why Constructor is Used


• To assign initial values to variables.
• To reduce repeated coding.
• To initialize objects easily and automatically.

Rules of Constructor
• Constructor name must be same as class name.
• Constructor has no return type.
• Constructor cannot return a value.
• Constructor is automatically called.
• Constructor is called only once for each object.
• Constructor can be overloaded.
• Constructor can have access specifiers like public.
• Constructor cannot be static, final, or abstract.

Working of Constructor
• Object is created using new keyword.
• Memory is allocated to the object.
• Constructor is called automatically.
• Variables are initialized.
• Object becomes ready for use.

Types of Constructors
• 1. Default Constructor
• 2. Parameterized Constructor

Default Constructor
• Constructor without parameters.
• Gives fixed/default values.
• No arguments passed.
• Easy to use.
• Same values for all objects.

Parameterized Constructor
• Constructor with parameters.
• Used for different values for different objects.
• Accepts arguments.
• Flexible initialization.
• Reduces extra assignment statements.

Constructor Overloading
• Creating multiple constructors in same class with different parameter lists.
• Used to initialize objects in multiple ways.

Advantages of Constructor
• Automatic initialization.
• Saves time and code.
• Improves readability.
• Makes program organized.

Limitations of Constructor
• Cannot be inherited.
• Cannot be overridden.
• Cannot be called like normal methods.

Important Exam Points


• If no constructor is written, Java provides a default constructor automatically.
• Constructor is invoked using the new keyword.
• Constructor overloading is important for ICSE exams.
• Difference between constructor and method is frequently asked.

Example: Default Constructor


class Student
{
int marks;

Student()
{
marks = 90;
}

void show()
{
[Link](marks);
}

public static void main(String args[])


{
Student obj = new Student();
[Link]();
}
}

Example: Parameterized Constructor


class Demo
{
int x;

Demo(int a)
{
x = a;
}

void display()
{
[Link](x);
}
}

Constructor vs Method
Constructor Method

Initializes object Performs task


Same name as class Any valid name
No return type Can return value
Called automatically Called manually
Runs once during object creation Can run many times

You might also like