Share
Go down
avatar
Admin
Posts : 7
Join date : 2018-08-07
View user profilehttp://jenreyes.forumotion.com

Constructors

on Wed Aug 08, 2018 2:39 pm
A method that has the same name as a class and that establishes an object is a constructor method, or more simply, a constructor .
A default constructor is one that requires no arguments. If a constructor requires arguments, it
is a nondefault constructor or a parameterized constructor .

Default Constructors

-A class can contain a default (parameterless) constructor that is created automatically. When a class contains an automatically created default constructor, it means that no constructors have been explicitly written for the class.
-A class can contain a default (parameterless) constructor that you create explicitly. A class with an explicitly created default constructor no longer contains the automatically supplied version, but it can coexist with a nondefault constructor.
-A class can contain a nondefault constructor (with one or more parameters), which mustbe explicitly created. A class with a nondefault constructor no longer contains the automatically supplied default version, but it can coexist with an explicitly created default constructor.


Nondefault Constructors

You can write one or more nondefault constructors for a class. Nondefault constructors accept one or more parameters. When you create any constructor, whether it is default or nondefault, the automatically supplied default constructor is no longer accessible. You could explicitly create a default (parameterless) constructor in addition to a nondefault one, in which case the constructors would be overloaded.
Guru
Guru
Posts : 10
Join date : 2018-08-08
View user profile

Re: Constructors

on Wed Aug 08, 2018 4:08 pm
does a default constructor is defined as one that is created automatically?
Guru
Guru
Posts : 10
Join date : 2018-08-08
View user profile

Re: Constructors

on Wed Aug 08, 2018 4:09 pm
can I overload constructors?
Guru
Guru
Posts : 10
Join date : 2018-08-08
View user profile

Re: Constructors

on Wed Aug 08, 2018 4:10 pm
difference between default constructors and non default constructors?
Guru
Guru
Posts : 10
Join date : 2018-08-08
View user profile

Re: Constructors

on Wed Aug 08, 2018 4:11 pm
can i create constructors for a class without parameters?
Guru
Guru
Posts : 10
Join date : 2018-08-08
View user profile

Re: Constructors

on Wed Aug 08, 2018 4:42 pm
Anna06 wrote:difference between default constructors and  non default  constructors?
Default constructor is one type of constructor.

Where as we have other conctructors namely:

Parameterised constructor
Copy constructor

If we don't define any constructor then a default constructor is provided. But if we define any constructor then no default constructor is provided.
Guru
Guru
Posts : 10
Join date : 2018-08-08
View user profile

Re: Constructors

on Wed Aug 08, 2018 4:43 pm
how to use constructor in java?
Guru
Guru
Posts : 10
Join date : 2018-08-08
View user profile

Re: Constructors

on Wed Aug 08, 2018 4:44 pm
can I make a class that have multiple constructors that assign the fields in different ways? Question
Guru
Guru
Posts : 10
Join date : 2018-08-15
View user profile

Re: Constructors

on Wed Aug 15, 2018 4:43 pm
differences between a constructor and a method?
Guru
Guru
Posts : 10
Join date : 2018-08-15
View user profile

Re: Constructors

on Wed Aug 15, 2018 4:44 pm
GraceAnn wrote:how to use constructor in java?
Here’s the basic format for coding a constructor:

public ClassName (parameter-list) [throws exception...]
{
statements...
}
Guru
Guru
Posts : 10
Join date : 2018-08-15
View user profile

Re: Constructors

on Wed Aug 15, 2018 4:44 pm
GraceAnn wrote:how to use constructor in java?
A constructor allows you to provide initial values for class fields when you create the object. Suppose that you have a class named Actor that has fields named firstName and lastName. You can create a constructor for the Actor class:

public Actor(String first, String last)
{
firstName = first;
lastName = last;
}
Guru
Guru
Posts : 10
Join date : 2018-08-15
View user profile

Re: Constructors

on Wed Aug 15, 2018 4:46 pm
GraceAnn wrote:can I make a class that have multiple constructors that assign the fields in different ways? Question
A class can have multiple constructors, as long as their signature (the parameters they take) are not the same.
Sponsored content

Re: Constructors

Back to top
Permissions in this forum:
You cannot reply to topics in this forum