Monday, 3 October 2016

Q. Explain the concept of Class and objects. How can constructors be used in JAVA explain with a program?

Answer(its Vedio Lecture in your language) 


Class is a collection of objects of same type. It provides a convenient way for packing together a group of related data items and functions. A class is declared by using a keyword class.
class classname
Field declarations;
Methods declarations:
A class can contain any of the following variable types.
1.Local variables: Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.
2.Instance variables: Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.
3.Class variables: Class variables are variables declared with in a class, outside any method, with the static keyword. It can be tedious to initialize all of the variables in a class each time an instance is created. Because the requirement for initialization is so common, Java allows objects to initialize themselves when they are created. This automatic initialization is performed through the use of a constructor.
Creating and declaring objects;
Obtaining objects of a class in two steps
• Declare a variable of class type, it does not define an object, instead it is simply a variable that
can refer to an object. Rectangle rect1; // declare
• Declaration creates a physical copy of that object and assign it to that variable and returns
reference to it. rect1=new Rectangle(); // instantiation
Accessing class members
We can access the instance variables and the methods with the use of concerned objects and dot operator.
Obj.method(parameter list);


A constructor initializes an object immediately upon creation. It has the same name as the class in which it resides and is syntactically similar to a method. Once defined, the constructor is automatically called immediately after the object is created, before the new operator completes.
Constructors look a little strange because they have no return type, not even void. This is because the implicit return type of a class' constructor is the class type itself. It is the constructor's job to initialize the internal state of an object so that the code creating an instance will have a fully initialized, usable object immediately.
class Box {
double width;
double height;
double depth;
// This is the constructor for Box.
Box() {
System.out.println("Constructing Box");
width = 10;
height = 10;
depth = 10;
// compute and return volume
double volume() {
return width * height * depth;
class BoxDemo
public static void main(String args[])
// declare, allocate, and initialize Box objects
Box mybox1 = new Box();
Box mybox2= new Box();
double vol; // get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);

No comments:

Post a Comment