Is it possible to call one constructor from another constructor? Saturday, Feb 6 2010 

Java supports this feature using ‘this’ pointer.

Example:

class sample

{

private:

int a, b;

public:

sample(int a)

{

this.a=a;

}

sample(int a, int b)

{

this(a); //calls the single parameter constructor

this.b=b;

}

}

Advertisements

Is constructor really needed for the program? Saturday, Feb 6 2010 

Constructor Requirement…

Java does not actually require an explicit constructor in the class description. If you do not include a constructor, the Java compiler will create a default constructor in the byte code with an empty argument. This default constructor is equivalent to the explicit constructor.

Example:

class sample

{

private:

int a, b;

public:

….

}

This sample class doesn’t contain any constructor. But the java compiler will create a default constructor in the byte code with an empty argument… sample() { }

If a class includes one or more explicit constructors like …

class sample

{

private:

int a, b;

public:

sample(int a, int b)

{

this.a=a;

this.b=b;

}

}

then the java compiler does not create the default constructor “sample(){}”.

What is the difference between C++ and Java? Saturday, Feb 6 2010 

C++ Vs Java

Both C++ and Java use similar syntax and are Object Oriented, but:

  • Java does not support pointers. Pointers are inherently tricky to use and troublesome.
  • Java does not support multiple inheritances because it causes more problems than it solves. Instead Java supports multiple interface inheritance, which allows an object to inherit many method signatures from different interfaces with the condition that the inheriting object must implement those inherited methods. The multiple interface inheritance also allows an object to behave polymorphically on those methods.
  • Java does not support destructors but adds a finalize() method. Finalize methods are invoked by the garbage collector prior to reclaiming the memory occupied by the object, which has the finalize() method. This means you do not know when the objects are going to be finalized. Avoid using finalize() method to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these resources through the finalize() method.
  • Java does not include structures or unions because the traditional data structures are implemented as an object oriented framework.
  • All the code in Java program is encapsulated within classes therefore Java does not have global variables or functions.
  • C++ requires explicit memory management, while Java includes automatic garbage collection.

Core Java – Technical Questions 2 Friday, Dec 12 2008 

What is Abstract factory? And its use?

  1. Users of an Abstract Factory can create families of related objects without any knowledge of their concrete classes.

2.      Using references to interfaces instead of references to concrete classes is an important way of minimizing ripple effects.

3.      The user of an interface reference is always protected from changes to the underlying implementation.

How we restrict to inherit a class?

By using final keyword

How we call a method without creating the instance of an object?

By using the static keyword

How we create a String instance without using new keyword?

(more…)

Core Java – Technical Question Thursday, Dec 11 2008 

What is the difference between final, finally, finalize in java?

Final – it is a keyword, final variables are constants, final methods are not overridden in subclass, final class – we can not inherit final class.

Finally – in exception handling whether the exception is arise or not, finally block must execute its code.

Finalize – In java garbage collection, before an object is removed from memory finalize is called by the garbage collector. It releases the resources from that object.

What is the difference between Abstract class and Interface?

Abstract classes are partially defined classes in java.

Interface – fully abstract behavior

Various states of the Thread?

Ready, Running, Waiting, Dead

(more…)

Next Page »