The default constructor is eliminated from a class once you create a constructor with arguments. There's the confirmation! The default constructor is always invisibly present in your classes.īut you need to know one more thing about it. If the phrase is displayed, then the constructor was invoked. To see it, let's explicitly write an empty constructor for the Cat class. We created an object, so what? Where is the constructor doing anything here? It doesn't take any arguments, but it is invoked every time you create any object of any class.Ĭat smudge = new Cat() // The default constructor is invoked hereĪt first glance, it's invisible. The fact is that every class in Java has what is called a default constructor. Remember that at the beginning of the lesson we said that you've already used constructors without realizing it? In precisely the same way that we overloaded methods in previous lessons. In addition to the original constructor with "name" and "age" parameters, we added one more with only a name parameter. In this case, we want our program to be able to create two kinds of cats: those with a name and age (for the first cat), and those with only a name (for the second cat).įor this, we will overload the constructor: But the second one you took in from the street when it was already grown, and you don't know exactly how old it is. Just like methods, constructors can be overloaded. The similarity between constructors and methods doesn't end here. Now you can't create a car without a speed or model! You can see that the constructor accomplishes what we were trying to achieve. And just like calling a method, calling a constructor won't work unless you specify them: Just like with methods, we passed arguments to our constructor. A constructor is really a method, but with specific features :) You may have noticed that a constructor is very similar to an ordinary method! So it is. The constructor correctly assigned the required values. The maxSpeed for this car (the one we're creating) is the maxSpeed argument passed to the constructor." "The model for this car (the one we're creating now) is the model argument passed to the constructor. The keyword this is for indicating a particular object. Moreover, the constructor specifies the class name ( Car) starting with an uppercase letter.Īdditionally, the constructor is used with a keyword that is new for you: this. It's similar to a regular method, but it doesn't have a return type. And creating an object now looks like this:Ĭar bugatti = new Car("Bugatti Veyron", 378) For convenience, let's go back to the simpler version of the Car class with two fields.Ĭonsidering our requirements, the Car class's constructor will look like this: The constructor creates a kind of class "skeleton" that each new object must match. Otherwise, we want to prevent the creation of the object. We need to restrict our program: when creating a new Car object, we want the fields, such as the model and maximum speed, to always be specified. We need to avoid such situations somehow. You can only imagine how such a mistake can "go off" when the program is running! no wheels at all), a missing trunk, a cabin made of an unknown material, and above all, an undefined owner. null"!īut seriously, the bottom line is that our program created an object incorrectly: a car with a wheel width of 0 (i.e. Your buyer, who gave up $2 million for the car, obviously won't like being called " Mr. Cabin material: " + bugatti.cabinMaterial + Engine volume: " + bugatti.engineVolume + ". As a result, the program will behave incorrectly: In this situation, a programmer can easily make a mistake and fail to initialize some field. There's one problem: we have 16 fields, but we only initialized 12! Look at the code now and try to find the fields we forgot! Not so easy, huh? Int accelerationTo100km // how long it takes to accelerate to 100 km/h (in seconds) String manufacturerCountry // manufacturer country String cabinMaterial // interior materialīoolean insurance // flag indicating whether car is insured Int seatsInTheCar // number of seats in the car String ownerLastName // last name of ownerīoolean isNew // flag indicating whether car is new String ownerFirstName // first name of owner We created our car, and set its model and maximum speed.īut the Car object would obviously not have 2 fields in a real project. What in the world are constructors and why are they needed?Let's consider two examples. You just didn't realize it :) We convince ourselves of this later. This may be the first time you're hearing this term, but you've actually already used constructors. Without exaggeration, we can say you'll use this topic in real life every day! Hi! Today we'll consider a very important topic that concerns our objects.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |