0% found this document useful (0 votes)
10 views

Constructors in Java

Uploaded by

nandinipoddarqms
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Constructors in Java

Uploaded by

nandinipoddarqms
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Constructors

Q. What is constructor?
Ans: A constructor is a special function that is automatically called,
when the object is created of that class. It has the same name as that
of the class name and its primary job is to initialize the object to a legal
value for the class.
class Test
{
public Test()
{

public static void Abc()


{
Test t=new Test();

}
Q. Why do we need a constructor as a class
member?

Ans: Constructor is used create an


instance of of a class, This can be also
called creating an object.
Q. Why does a constructor should be define
as public?

Ans: A constructor should be define in


public section of a class, so that its
objects can be created in any function.
Q. Mention some characteristics of constructors.

Ans: The special characteristics of constructors are:


(i) Constructors should be declared in the public section of the class.
(ii) They are invoked automatically when an object of the class is created.
(iii) They do not have any return type and cannot return any values.
(iv) Like any other function, they can accept arguments.
(v) A class can have more than one constructor.
(vi) Default constructor do not accept parameters.
(vii) If no constructor is present in the class the compiler provides a default
constructor.
Q. State the difference between Constructor and
Method.

(i) Constructor has the same name as the class name, whereas
functions have different names from their class names.

(ii) Constructors have no return type (not even void), whereas


functions always have a return type

(iii) Constructor is automatically called when the object of that class is


created where as the function must be called in programs
.
Q. What are the types of Constructors used in a
class?

Ans: The different types of constructors are as


follows:

i. Default Constructors.
ii. Parameterized Constructor.
iii. Copy Constructors.
Default or non-parameterized Constructor

When a class name is used as a function followed


by empty brackets is known as a default or non-
parameterized function.
Parameterized Constructor

When the class name is used as a function followed


by one or list of primitive data types as parameters
/ arguments within the bracket is known as
parameterized constructors.
Copy Constructor

This constructor is used to initialize one object with


the stored values to another object of the same
class during declaration.
Q. Explain default constructor?
Ans: The constructor that accepts no parameter is called the default constructor. If
we do not explicitly define a constructor for a class., then java creates a default
constructor for the class. The default constructor is often sufficient for simple class
but not for sophisticated classes.
Example:
class Abc
{
int i;
public static void main()
Abc nc=new Abc();
}
the line new ant() creates an object and calls the default constructor, without it we
have no method to call to build our objects. once you create a constructor with
argument the default constructor becomes hidden.
Q. Explain the Parameterised constructor?
Ans: If we want to initialisze objects with our desired value, we can use parameters with
constructor and initialise the data members based on the arguments passed to it .
Constructor that can take arguments are called Parameterised constructor.
Example:

public class result


{
int per;
int tot;
public result (int percentage)
{
per=percentage;
tot=0;
}
}
Q. Enter any two variables through constructor parameters
and write a program to swap and print the values.
Q. Enter any two variables through constructor parameters and write a program to swap and print
the values.
class swap
{
int a,b;
swap(int x,int y)
{
a=x;
b=y;
}
public void main(String args[])
{
int t=a;
a=b;
b=t;
System.out.println(“the value of a and b after swaping : “+a+” “+b);
}
}
Q. Define Copy constructors.
Ans: A copy constructors initializes the instant variables of an object by copying the initial value
of the instant variables from another objects.
e.g.
class xyz
{
int a,b;
xyz(int x,int z)
{
a=x;
b=y;
}
xyz(xyz p)
{
a=p.x;
b=p.y;
}
}
Q. Give a syntax/example of constructor overloading. Define a class, which accept roll number and marks of a student. Write
constructor for the class, which accepts parameter to initialise the data member. Also take care of the case where the student
has not appeared for the test where just the roll number is passed as argument.
Ans: class student
{
int roll;
float marks;
student(int r, float m) // constructor with two argument.
{
roll=r;
marks=m;
}
student(int r) // constructor with one argument
{
roll=r;
marks=0;
}
student() // default constructor
{
roll=0;
marks=0;
}
}

You might also like