Total members 11894 |It is currently Thu Nov 21, 2024 10:47 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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 .



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  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     -  
 Define class inside another class C++     -  
 inner class that is a member of an outer class?     -  
 load class to applet- load frame class to applet     -  
 PHP example for a class     -  
 PHP class example     -  
 JPA entity class example     -  



Topic Tags

Java OOP






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com