• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why constructors

 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why do we need to difine constructors explicitly
public class xyz
{
public xyz()
{
//some code;
}
}
can somebody explain .
 
Shivaji Marathe
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you want to create an instance of the object and make sure that some default processing has occurred,before any methods on that instance can be called, you use the connstructo and place the code there.
Sometimes you can pass parameters to the constructor so that each instance of the object has different values associated with it.
If you had a class Car with a constructor that took the model name as an argument, then you can create instances of Car object by specifying the model name of the car .
For simple objects, where you do not need any variables initialized or any processing to be completed when the instance is created, you don't need to provide any constructor, as is often the case with examples programs you see in books and tutorials.
HTH
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raghav
Another case in which you need to define an explicit constructor is if you define any constructors in a class that take arguments.
If you do not define any constructors the compiler will create a default (no argument) consructor for you. If you create any other constructors the compiler assumes that you know what you are doing and will not create a default constructor for you.
This can lead you into trouble if you try to create an instance of that class and do not call one of the constructors you defined. Usually this occurs when trying to create an instance of a subclass and then don't include a specific call to super(args), the compiler automatically inserts a call to the superclasses default constructor.
Check this code out:

In this example at the line with the ****** the compiler will insert a call to super() - the default constructor of SuperClass. Since there is a constructor defined for SuperClass the compiler will not create a default constructor and the call to super() wont compile because the constructor doesn't exist.
hope that helps
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dave Vick:
raghav
Another case in which you need to define an explicit constructor is if you define any constructors in a class that take arguments.
If you do not define any constructors the compiler will create a default (no argument) consructor for you. If you create any other constructors the compiler assumes that you know what you are doing and will not create a default constructor for you.
This can lead you into trouble if you try to create an instance of that class and do not call one of the constructors you defined. Usually this occurs when trying to create an instance of a subclass and then don't include a specific call to super(args), the compiler automatically inserts a call to the superclasses default constructor.
Check this code out:

In this example at the line with the ****** the compiler will insert a call to super() - the default constructor of SuperClass. Since there is a constructor defined for SuperClass the compiler will not create a default constructor and the call to super() wont compile because the constructor doesn't exist.
hope that helps

Dave,
So, in this case, we need to add a no-argument
constructor to SuperClass to make the code compile, since the compiler will generate a call
to this constructor in Sub constructor. Correct me if I am wrong.
victor
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Boy, Why constructors? :roll:
after 260 posts?.....
- satya
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by victor gu:

So, in this case, we need to add a no-argument
constructor to SuperClass to make the code compile, since the compiler will generate a call
to this constructor in Sub constructor. Correct me if I am wrong.
victor

That is correct, Victor
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic