CONSTRUCTOR
What is Constructor?
A constructor is a member method having the same name as that of
the class and is used to initialize the instance variables of the objects.
Features Of The Constructor:
• A constructor must be same name as class name.
• it does not have any return data type.
• A constructor is automatically called when object of the class
is created.
• Constructor is always public
• Constructors can be overloaded.
How to invoke the Constructor?
<Class name> <object name> = new <Constructor()>;
Types of Constructor
• Default Constructor
• Parameterized Constructor
• Non-Parameterized Constructor
• Copy Constructor
Default Constructor
A Constructor that is used to initialize the instance variables with
default initial values is called default constructor
Parameterized Constructor Non-Parameterized Constructor
• A Constructor which initializes the • A Constructor which initializes the
instance variables with the help of instance variables of the objects with
parametric values is called definite values readily available within
Parameterized constructor. it, known as Non-Parameterized.
Eg: Eg:
Test( int x, int y) Test()
{ {
a=x; a=10;
b=y; b=5;
} }
What is Constructor Overloading?
Constructor overloading in JAVA occurs when class has multiple
constructors, each with a seperate and unique method signature
Constructor Method
• It has different name as that of class
• It has same name as that of class
name.
name.
• It has a return data type.
• It has no return data type.
• Functions can be public, private ,
• Constructors are always public.
protected.
• Constructors are automatically called
• It executes only when it is called.
when the object is created
Define New Operator
The new operator in java is used to create new objects of the class.
Computer keyboard = new Computer();
Define Dot Operator
Dot operator is used to access class structure or union members. It is also
helpful for invoking packages.
Eg: import [Link].*;
Sample Program 1
Define a class named movieMagic with the following description:
•Instance Variables:
◦ int year – to store the year of release of a movie.
◦ String title – to store the title of the movie.
◦ double rating – to store the popularity rating of the movie.
•Methods:
◦ void accept() – to input and store year, title, and rating.
◦ void display() – to display the title of the movie and a message based on the
rating as per the table given below:
Rating Message
0.0 to 2.0 Flop
2.1 to 3.4 Semi-Hit
3.5 to 4.4 Hit
4.5 to 5.0 Super-Hit
Write a main method to create an object of the class and call the above member
methods.
import [Link];
class movieMagic
{
int year;
String title;
double rating;
public void accept()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the title of the movie: ");
title = [Link]();
[Link]("Enter the year of release: ");
year = [Link]();
[Link]("Enter the rating of the movie (0.0 to 5.0): ");
rating = [Link]();
[Link]();
}
public void display()
{
[Link]("Title: " + title);
if (rating >= 0.0 && rating <= 2.0)
{
[Link]("Rating: Flop");
}
else if (rating >= 2.1 && rating <= 3.4)
{
[Link]("Rating: Semi-Hit");
}
else if (rating >= 3.5 && rating <= 4.4)
{ Output:
[Link]("Rating: Hit");
} Enter year, title and rating
else if (rating >= 4.5 && rating <= 5.0) 2021
{ Mother’s Day
[Link]("Rating: Super-Hit"); 5.0 Mother’s Day Super Hit
}
else
{
[Link]("Invalid rating!");
}
}
public static void main(String[] args) {
movieMagic movie = new movieMagic();
[Link]();
[Link]();
}}