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

9 Java Constructor

A constructor is a special method used to initialize objects in Java, sharing the same name as the class and lacking return types. There are various types of constructors including default, non-parameterized, parameterized, and copy constructors, each serving different purposes in object creation. The document also provides examples demonstrating the use of these constructors in Java classes.

Uploaded by

Vignesh gs
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)
2 views

9 Java Constructor

A constructor is a special method used to initialize objects in Java, sharing the same name as the class and lacking return types. There are various types of constructors including default, non-parameterized, parameterized, and copy constructors, each serving different purposes in object creation. The document also provides examples demonstrating the use of these constructors in Java classes.

Uploaded by

Vignesh gs
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
You are on page 1/ 5

CONSTRUCTOR

 Constructor is similar to method it’s an


block of method
 Constructor is used to initialize objects
when they are created
 Constructor is also called as special
method
 Constructor is should be same as class
name
 Constructor doesn't have return or non-
return type
 Constructor doesn't have an abstract
because they can't be override or inherit
 Constructor doesn't have a final because
they can't be override or inherit
 Constructor doesn't have a static because
they means to initialize object not a
class
 Constructor can't be override because the
class name should be constructor
 In constructor we can't use this and
super keyword
 We don't have to call constructor name as
specific it called when object created
based on parameter
 Mostly java used to initialize objects
and Avoids writing more different methods
with different names
Constructor are more types:
 default constructor (but we can't use a default
keyword if didn't using other access modifiers
public, private and protected it consider as
default)
 non-parameterized constructor (default and non-
parameterized are almost same but little
difference)
 parameterized constructor (using one or more
arguments in parameter)
 Copy constructor (Copies values from an
existing object.)

Difference between default and non-parameterized


constructor:

 default constructor are created by java


compiler automatically while create an object
 no parameters in default constructor
 We will not use default keyword for constructor
 non-parameterized constructor are created by
user manually can include with logic statements
 no parameters

package javaprogram;
//Constructor
class Games
{
// In constructor We can use access modifiers
of public,private and protected
// same as method if we didn't use
public,private and protected it consider as default
// We can't use default keyword for default
constructor
public Games()
{
String gamename="cricket";
System.out.println("default constructor"+"
"+gamename);
}
// parameterized constructor with single
parameter
public Games(String gamename)
{
System.out.println("non-parameterized
constructor"+" "+gamename);
}
// parameterized constructor with double
parameter
public Games(String gamename,int
startingYearofgame)
{
System.out.println("non-parameterized
constructor"+" "+gamename+" "+startingYearofgame);
}
}
public class TopicConstructor{
public static void main(String[] args) {
// we don't have call specific constructor
// creating an object It calls constructor
based on parameters(arguments)
Games cricketgame=new Games(); // calling
default constructor
Games cricketgameobj=new
Games("vollyball");// calling parameterized
constructor with single parameter
Games cricketgameobj2=new
Games("football",1888);// calling parameterized
constructor with double parameter
}
}
A copy constructor is a constructor that creates a new
object by copying values from another object of the same
class.
package javaprogram;
//Constructor
class additions
{
int a; // instance variable a
int b; // instance variable b

public additions(int a,int b) // constructor


{
this.a=a;
this.b=b;
}
public additions(additions add) // copy
constructor
{
this.a=add.a;
this.b=add.b;
}
void displayValues() // constructor values
display method
{
System.out.println(a+b);
}
}
public class TopicConstructor{
public static void main(String[] args) {

additions ob=new additions(10,20); //


constructor object
additions ob1=new additions(ob); // copy
constructor object
ob.displayValues(); // calling constructor
ob1.displayValues(); // calling copy
constructor
}
}

You might also like