Java Questions with Answers #5

1) Enlist the difference between Procedural Programming and Objet Oriented Programming Language.
Sr. Procedural Programming Language Object Oriented Programming Language(OOP)
The procedural programming language executes series of procedures sequentially. In object oriented programming approach there is a collection of objects.
This is a top down programming approach. This is a bottom up programming approach.
3 The major focus is on procedures or functions. The main focus is on objects.
4 Data reusability is not possible.  Data reusability is one of the important feature of OOP.
5 Data binding is not possible. Data binding can be done by making it private.
It is simple to implement. It is complex to implement.

2) What is class and Object in JAVA?

Each class is a collection of data and the function that manipulate the data.  The data components of the class are called data fields and the function components of the class are called member functions or module. The class that contains main function is called main class. 

Object is an instance of a class. The objects represent real world entity. The objects are used to provide a practical basis for the real world. Objects are used to understand the real world. The object can be declared by specifying the name of the class. For example for the class rectangle we can create the objects as follows:- rectangle r1,r2,r3;

The declaration of objects is similar to declaration of variables. The operator new is used to create an object.

3) What do you mean by constructor? Explain with example.

The constructor is a specialized method used for initializing the objects.  Note that the name of the constructor and the name of the class must be the same.  The constructor invoked whenever an object of its associated class is created. It is called constructor because it creates the values for the data fields of the class.

Example

public class rectangle 
{
       int height;
       int width;
       rectangle()
      {
             System.out.orintln(“Sumple constructor “);
             height=10;
             width=20;
       }
       rectangle(itn h,int w)
      {
              height=h;
              width=w;
       }
}

4) What is finalizer? Explain.

Java has a facility of automatic garbage collection. Hence even though we allocate the memory and then forget to deallocate it then the objects are no longer is used get freed. Inside the finalize() method you will specify those actions that must be performed before an object is destroyed. The garbage collector runs periodically checking for objects that are no longer referenced by any running state or indirectly though other referenced objects. To add finalizer to a class simply define the finalize method. The syntax to finalize() the code is

void finalize()
{
      Finalization code
}

5) What do you mean by package-ptivate or package-access?

Access modifiers control access to data fields, methods, and classes. There are three modifiers used in java-public, 
private, and default modifier. 

public allows classes, methods and data fields accessible from any class.

private allows classes, methods and data fields accessible only from within the own class.

If public or private is not used then by default the classes, methods and data fields are accessible by any class in the same package. This is called package private or package-access. A package is essentially grouping of classes.

6) How to pass and return the objects to and from the method? How to pass objects to the method?

The object can be passed to a method as an argument. Using dot operator the object’s value can be accessed. Such a method can be represented syntactically as Data_type name_of_method(object_name)
{
   //body of method
}

7) How to Return an object from a method?

We can return an object from a method. The data type such method is a class type.

Example –

public class ObjRefDemo
{
     int a; 
     ObjRefDemo(int val)
    {
          a=val;
     }
     ObjRefDemo fun()
     {
          ObjRefDemo temp=new ObjRefDemo(a+5);
          return temp;
      }
}
class ObjRef
{
          Public static void main(String args[])
         {
              ObjRefDemo obj2=new ObjRefDemo(20);
              ObjRefDemo obj1=obj2.fun();
              System.out.println(“The returned value is = “+obj1.a);
           }
}

Java Tips

See also

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.