answersLogoWhite

0


Best Answer

Of course they can, It is a little weird, but there are not any type of restriction about it. You can have something like this:

public class Fun{

public Fun(){

//do nothing

}

public Fun(Fun otherFun){

//do somethingFun with otherFun object

}

}

and then in a boring day do this:

//More code goes here

public static void main (String[] args){

Fun fun1 = new Fun(); // Create a Fun object with the default constructor

Fun fun2 = new Fun(fun1); // Create a Fun object with the constructor that expect a Fun object

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can constructor takes an object of its own class as a Parameter?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you call multiple constructors with single object?

You can have any number of constructors for a class. All we need to do is implement constructor overloading. Ex: let us say we want to create multiple constructor for a class Test Public class Test { Public Test() { //code } Public Test(int vals) { //code } Public Test(String val) { //code } }


What is the method of constructor overloading in c plus plus?

Constructor overloading, just like any function's overloading, is where more than one configuration of parameters exists for the function. Based on the number and type of the parameters, different versions of the function can be resolved by the linker. This is typically used in the constructor as the default constructor (no parameters), the copy constructor (one reference parameter of the same type as the class), and the conversion constructor (any other combination of parameters).


Can you override the constructor?

A constructor should be overloaded when there is a parameter with default value that is commonly used, or if it is possible that not all the information for creating the instance will be gathered all at once. As an example, say there is a class that describes bank accounts. Assuming I can choose to deposit or not deposit money under my name upon creating an account, there is a chance for a meaningful overload. One constructor could take in the name of the account owner and the initial balance as parameters; the overloaded constructor could just take the name as a parameter while setting the initial balance to zero.


How do you get a default constructor?

The default constructor is an empty (only call the super constructor) with no parameters constructor inserted by the java compiler when you don't define a constructor in your class. If you write something like this: public class NoConstructorClass{ //no constructor goes here } Then you get something like this: public class NoConstructorClass{ public NoConstructorClass(){ // Default constructor that you didn't write super(); } }


Default constructor in java?

If you don't type a constructor into your class code, a default constructor will be automatically generated by the compiler. The default constructor is ALWAYS a no-arg constructor. (Obviously the compiler has no clue what all arguments you might want for your class. So it takes the safe way out with a no argument constructor) A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor, although the default constructor is always a no-arg constructor. The default constructor is the one the compiler provides! While the default constructor is always a no-arg constructor, you're free to put in your own no-arg constructor.

Related questions

What is the method of constructor overloading?

The first thing to note about constructor overloading is that Java creates a no argument constructor for you if and only if you have not typed a constructor yourself. Every class has a constructor even abstract ones (default no argument constructor). Abstract constructors are always executed. To overload a constructor you can do the following: class Test { String name; Test(String n) { name = n; System.out.println("Constructing Test Object named: " + name); } } In the case above we are overloading the default no argument constructor with a constructor that takes a String parameter. You can write you own no argument constructor as follows: class Test { Test() { System.out.println("Constructing Test Object"); } } To override our own no argument constructor we do this: class Test { Test() { // our no argument constructor System.out.println("Constructing Test Object"); } String name; Test(String n) { // overloading our no argument constructor with this // constructor that take a String parameter name = n; System.out.println("Constructing Test Object named: " + name); } }


Is constructor created automatically in java?

There is a default constriuctor that takes no argument for every class that extends Object.


Discrbe breifly about Copy constructor overloading?

A copy constructor usually refers to a constructor which takes an object, and returns a copy of that object. I can think of no way to overload the constructor without changing its functionality.


How do you call multiple constructors with single object?

You can have any number of constructors for a class. All we need to do is implement constructor overloading. Ex: let us say we want to create multiple constructor for a class Test Public class Test { Public Test() { //code } Public Test(int vals) { //code } Public Test(String val) { //code } }


What is the method of constructor overloading in c plus plus?

Constructor overloading, just like any function's overloading, is where more than one configuration of parameters exists for the function. Based on the number and type of the parameters, different versions of the function can be resolved by the linker. This is typically used in the constructor as the default constructor (no parameters), the copy constructor (one reference parameter of the same type as the class), and the conversion constructor (any other combination of parameters).


Can you override the constructor?

A constructor should be overloaded when there is a parameter with default value that is commonly used, or if it is possible that not all the information for creating the instance will be gathered all at once. As an example, say there is a class that describes bank accounts. Assuming I can choose to deposit or not deposit money under my name upon creating an account, there is a chance for a meaningful overload. One constructor could take in the name of the account owner and the initial balance as parameters; the overloaded constructor could just take the name as a parameter while setting the initial balance to zero.


Using default constructor in inheritance and in what reason it throws error?

The compiler places a default no-arg constructor in any java class that does not have an explicit constructor coded into it. for ex: public class Car { ... ... //lots of code but no constructor } In the above case, the compiler will place the below constructor into the code: public Car() { super(); } But, if you have a constructor in your class that takes arguments then the compiler will not put the default constructor. Ex: public class Car { public Car(String name){ ... } ... //lots of code } Above, we have a Car constructor that takes a string name as argument. so, the compiler wont put the default constructor in the code. now, if you try to do: Car obj = new Car(); you will get an error because this constructor is not defined.


How do you get a default constructor?

The default constructor is an empty (only call the super constructor) with no parameters constructor inserted by the java compiler when you don't define a constructor in your class. If you write something like this: public class NoConstructorClass{ //no constructor goes here } Then you get something like this: public class NoConstructorClass{ public NoConstructorClass(){ // Default constructor that you didn't write super(); } }


Default constructor in java?

If you don't type a constructor into your class code, a default constructor will be automatically generated by the compiler. The default constructor is ALWAYS a no-arg constructor. (Obviously the compiler has no clue what all arguments you might want for your class. So it takes the safe way out with a no argument constructor) A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor, although the default constructor is always a no-arg constructor. The default constructor is the one the compiler provides! While the default constructor is always a no-arg constructor, you're free to put in your own no-arg constructor.


What are the three parts of simple empty class?

Three parts of a simple empty java class are; A simple empty class of java supports legacy, as it automatically acquires object class, therefore can produce the functionality described in the object hierarchy. It automatically generates a fault constructor inside the class with a blank description. It also accompanies polymorphism, as can be practiced in any scheme by the extensive class.


Is Java always provides a default constructor to a class?

Correct. If you omit a constructor, Java will assume that an empty one exists. Given the following code for a class: class MyClass { } You can make a call to the default constructor: MyClass mc = new MyClass(); Just keep in mind that the default constructor "goes away" if you implement another constructor. class MyClass { public MyClass(String str){ } } This line will now result in a "cannot find symbol" compiler error: MyClass mc = new MyClass();


Difference between method and constructor in java?

A constructor is technically a type of method, but it is a very special type. Whereas other methods can be used to do just about anything, the only purpose of a constructor method is to create an instance of the class that contains it, often with parameters passed to it through another part of the program. This instance is called an "object" and is a central part of not only Java, but other object-oriented languages as well. A constructor method always has the same name as its containing class, and does not have a return type. Think of it this way: a class in Java is like a generic blueprint for a house. Your instance variables are like different attributes of the house - how many bathrooms will your house have, what colour will it be? Once you decide on the exact specifications for your house, you can give those parameters to the construction company, which will actually create that house. That's what a constructor method does - takes input parameters (or, lacking them, sets defaults) and creates an object.