Constructors
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.
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.
- Anna06Guru
- Posts : 10
Join date : 2018-08-08
Re: Constructors
Wed Aug 08, 2018 4:08 pm
does a default constructor is defined as one that is created automatically?
- Anna06Guru
- Posts : 10
Join date : 2018-08-08
Re: Constructors
Wed Aug 08, 2018 4:09 pm
can I overload constructors?
- Anna06Guru
- Posts : 10
Join date : 2018-08-08
Re: Constructors
Wed Aug 08, 2018 4:10 pm
difference between default constructors and non default constructors?
- Anna06Guru
- Posts : 10
Join date : 2018-08-08
Re: Constructors
Wed Aug 08, 2018 4:11 pm
can i create constructors for a class without parameters?
- GraceAnnGuru
- Posts : 10
Join date : 2018-08-08
Re: Constructors
Wed Aug 08, 2018 4:42 pm
Default constructor is one type of constructor.Anna06 wrote:difference between default constructors and non default constructors?
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.
- GraceAnnGuru
- Posts : 10
Join date : 2018-08-08
Re: Constructors
Wed Aug 08, 2018 4:43 pm
how to use constructor in java?
- GraceAnnGuru
- Posts : 10
Join date : 2018-08-08
Re: Constructors
Wed Aug 08, 2018 4:44 pm
can I make a class that have multiple constructors that assign the fields in different ways?
- Pamela16Guru
- Posts : 10
Join date : 2018-08-15
Re: Constructors
Wed Aug 15, 2018 4:43 pm
differences between a constructor and a method?
- Pamela16Guru
- Posts : 10
Join date : 2018-08-15
Re: Constructors
Wed Aug 15, 2018 4:44 pm
Here’s the basic format for coding a constructor:GraceAnn wrote:how to use constructor in java?
public ClassName (parameter-list) [throws exception...]
{
statements...
}
- Pamela16Guru
- Posts : 10
Join date : 2018-08-15
Re: Constructors
Wed Aug 15, 2018 4:44 pm
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:GraceAnn wrote:how to use constructor in java?
public Actor(String first, String last)
{
firstName = first;
lastName = last;
}
- Pamela16Guru
- Posts : 10
Join date : 2018-08-15
Re: Constructors
Wed Aug 15, 2018 4:46 pm
A class can have multiple constructors, as long as their signature (the parameters they take) are not the same.GraceAnn wrote:can I make a class that have multiple constructors that assign the fields in different ways?
Permissions in this forum:
You cannot reply to topics in this forum
|
|