Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Class constructors

Sat Feb 07, 2009 1:21 am

Class constructors
Constructor is a special method called when you create a new instance of the class using the “new” operator. The constructor method has the same name as the class and it has no return type. If you didn’t define a constructor in your class the java compiler will create a constructor with not argument which is called default constructor. But if you define any constructors in your class, there will be no default constructor creation. Take in mind that you can create a constructor which has parameters but same name of class and has no return type.
Code:
public class Car {
   int CarSpeed;
   String CarModel;

    // Main Function
    public static void main(String[] args) {
       Car mycar=new Car();
    }
   
}


Where constructor called?
Constructors can be called from inside the class or from outside the class .from outside the class is when you use the “new” operator and from inside the class is where you call it in other constructors. You use the “this” keyword to call a constructor in the same class and “super” keyword to call constructor in the base class.


Note
: if you define a constructor with parameters and you didn't define a default one ,this may cause error if you didn't send any parameters in the creations , like the following code have compile error .
Code:
public class Car {
   int CarSpeed;
   String CarModel;
   public Car(String CarModel)
   {
       
       this.CarModel=CarModel;
   }
    // Main Function
    public static void main(String[] args) {
       Car mycar=new Car();
    }
   
}


Note: if you have a super class and you didn’t call its constructor using the “super” keyword, the compiler will call it first before the base constructor. The variables of the super class must be initiated before the variables of the base class initiated.

Look at the following example :
Code:
public class Car {
   int CarSpeed;
   String CarModel;
   public Car()
   {
       
   }
   public Car(String CarModel)
   {
       
       this.CarModel=CarModel;
   }
    // Main Function
    public static void main(String[] args) {
       RaceCar racecar=new RaceCar();
    }
   
}
class RaceCar extends Car
{
    public RaceCar()
    {
       super("SuperCar") ;
    }
    public void RunTurbo()
    {
        CarSpeed+=100;
    }
   
}


The constructors can't be overridden or inherited .



Post a reply
  Related Posts  to : Class constructors
 What are Constructors and Destructors?     -  
 Define class helper class to check the method existance     -  
 java abstract class,concrete class and interface     -  
 relationship between the Canvas class and the Graphics class     -  
 inner class that is a member of an outer class?     -  
 Define class inside another class C++     -  
 load class to applet- load frame class to applet     -  
 PHP example for a class     -  
 PHP class example     -  
 Circle class in C++     -  

Topic Tags

Java OOP