Page 1 of 1

Constructors in Java Rate Topic: ***** 1 Votes

#1 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Posted 04 February 2010 - 07:28 AM

*
POPULAR

This part of the tutorial is going to show almost all about the constructors in java
let us first define what is a constructor
Constructor: -
A constructor is something that has the name same as that of the class name and it is used to construct the values those are created by objects
Whenever an object is created in a class then the constructor of that respective class is called.
for example, the constructor in a class can be as follows
class FirstClass     // name of the class
{
  FirstClass()      // this is a constructor
  {
      // the coding of constructor goes here

  }   // end of constructor
}



In the example above the constructor is with no arguments. There can be constructors with no arguments or constructor with arguments.
There are some rules that need to be followed while declaring and defining a constructor
1.The constructor should have the name of the class itself (Always remember java is a case sensitive language)
2.The constructor cannot have a return type
3.The access specifier of the constructor is the access specifier of the respective class if not given.
4.Constructor does not return any value

The valid declaration for the constructor is shown above

Now let us see what makes the constructor call with some programming examples
class Constructor
{
	
		Constructor()                                    // no argument constructor
		{
			System.out.println("First Constructor");
		}

		Constructor(int a,int b)                        // two argument constructor
		{
			System.out.println("Second Constructor");
		}

		Constructor(int a,int b,int c)                  // three argument constructor
		{
			System.out.println("Third Constructor");
		}

		public static void main(String args[])		// Main Started
		{			
			Constructor a = new Constructor();	// this call no-arg constructor
			Constructor b = new Constructor(10,20);  // this calls two arg constructor
			Constructor c = new Constructor(10,20,30); // this calls three arg constructor
		}// End of Main
}// End of class


Now look at the code above whenever the objects are created respective constructors are called and the output is as follows

Quote

F:\Java\Concepts\DeclarationAndAccessControl>java -cp F:\Java\Concepts\DeclarationAndAccessControl Constructor
First Constructor
Second Constructor
Third Constructor


Another thing about the constructor is that if the derived class constructor is called using the object of derived class then the constructor of the base class is also called.
This is because the JVM calls the constructors of the parent classes using the super reference to the parent class
the complete procedure will be shown in another tutorial which will explain the use or super and this reference.
to elaborate this more
consider the following code
this is how we write the code in the text editor
class SomeClass
{

}


and here is what the compiler takes it as
class SomeClass
{
SomeClass()
{
super();
}
}


Now wait here
even if we have not declared a constructor here then how come the constructor was defined automatically, the fact is that every class has a default constructor despite we declare it or not. it has a call to the parent class with super
following example shows it thoroughly
class BaseClass
{
	BaseClass()
	{
		System.out.println("This is base class constructor");
	} // end of base class constructor
	
	BaseClass(int x)
	{
		System.out.println("This is another base class constructor");
	}	// end of another constructor

}

class DerivedClass extends BaseClass
{
	DerivedClass()
	{
	System.out.println("This is derived class constructor");
	}

	public static void main(String args [])
	{
		DerivedClass dc = new DerivedClass();	// this calls the base class constructor also
	}
}



The output is

Quote

This is base class constructor
This is derived class constructor


Hope this explains much about the constructor

Is This A Good Question/Topic? 10
  • +

Replies To: Constructors in Java

#2 bvarghese2155  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-August 12

Posted 14 August 2012 - 10:02 AM

I still don't get how to use the super() method, or even why to use it... :huh:
Was This Post Helpful? 0
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Posted 14 August 2012 - 11:13 AM

View Postbvarghese2155, on 14 August 2012 - 10:47 PM, said:

I still don't get how to use the super() method, or even why to use it... :huh:

What you dint get? Ask it in java forum and we are happy to make you get it!
Was This Post Helpful? 0
  • +
  • -

#4 DrPerry  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 19
  • Joined: 21-January 13

Posted 22 January 2013 - 02:03 PM

Brilliant! Helped a lot! Thanks!

Keep up the good work. :-)
Was This Post Helpful? 0
  • +
  • -

#5 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Posted 23 February 2013 - 03:07 PM

Extremely clarifying tutorial. Thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1