Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Library System Program to implement constructors Version: 1.0 Date: Library system Defining classes using constructor Version 1.0 Revision History Date Version 1.0 Description Author Original Program Title: Using constructors Estimated time to complete this experiment: 2 hrs Objective: Defining classes using constructor with display function Confidential St. Francis Institute of Technology (Engg. College) Page 2 Library System Program to implement constructors Version: 1.0 Date: Books/ Journals/ Websites referred: http://javarevisited.blogspot.in/2012/12/what-is-constructor-in-java-example-chainning-overloading.html http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html Pre Lab/ Prior Concepts: Constructor in Java is block of code which is executed at the time of Object creation. But other than getting called, Constructor is entirely different than methods and has some specific properties like name of constructor must be same as name of Class. Constructor also can not have any return type, constructor’s are automatically chained by using this keyword and super. Since Constructor is used to create object, object initialization code is normally hosted in Constructor. Similar to method you can also overload constructor in Java. New Concepts to be learned: A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations—except that they use the name of the class and have no return type. For example, Bicycle has one constructor: public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } To create a new Bicycle object called myBike, a constructor is called by the new operator: Bicycle myBike = new Bicycle(30, 0, 8); new Bicycle(30, 0, 8) creates space in memory for the object and initializes its fields. Although Bicycle only has one constructor, it could have others, including a no-argument constructor: public Bicycle() { gear = 1; cadence = 10; speed = 0; } Bicycle yourBike = new Bicycle(); invokes the no-argument constructor to create a new Bicycle object called yourBike. Both constructors could have been declared in Bicycle because they have different argument lists. As with methods, the Java platform differentiates constructors on the basis of the number of arguments in the list and their types. You cannot write two constructors that have the same number and type of arguments for the same class, because the platform would not be able to tell them apart. Doing so causes a compile-time error. Confidential St. Francis Institute of Technology (Engg. College) Page 2 Library System Program to implement constructors Version: 1.0 Date: Requirements: Software & Hardware Required: Text editor, jdk1.6.0_16, Command Prompt Conclusion: Hence the above program shows the implementation of constructors in Java Real Life Application: In object-oriented programming, a constructor (sometimes shortened to ctor) in a class is a special type of subroutine called to create an object. It prepares the new object for use, often accepting parameters that the constructor uses to set member variables required Post Lab Questions: What is constructor chaining? Constructor chaining occurs through the use of inheritance. A subclass constructor method's first task is to call its superclass' constructor method. This ensures that the creation of the subclass object starts with the initialization of the classes above it in the inheritance chain. There could be any number of classes in an inheritance chain. Every constructor method will call up the chain until the class at the top has been reached and initialized. Then each subsequent class below is initialized as the chain winds back down to the original subclass. Viva Questions: Explain the behavior of constructors under inheritance. Confidential St. Francis Institute of Technology (Engg. College) Page 2