Wednesday, July 2, 2008

Java Interview Concepts

Hi,
i am writing the java interview concepts i am not giving complete details i am giving the overall structure for glance when ever interview is there it is for quick reference i hope this might help
Object class Details
Object Class is Super class of all java classes
Object class having the following methods
public final Class getClass()
public int hashCode()
public boolean equals(Object obj)
protected Object clone() throws CloneNotSupportedException
public String toString()
public final void notify()
public final void notifyAll()
public final void wait(long timeout) throws InterruptedException
protected void finalize() throws Throwable
--------etc
you will get all the implementation details and some where most of the people
ask about these details some questions are
Can we override finalize() method
Ans: yes we can override this method when ever we know object is going out of memory
that type object we call in finalize method. this work automatically done by java only
it has good alogitihm to remove the object from memory.
notify(), wait(),notifyAll() are related to threads but why there are overrided in
Object Class
Ans: this methods not only for Threads it is external resources also that's why they are
overrided inside Object class
Difference b/n equal (==) operator and equals() method why should we override
Ans: in equals() method also they are using == operator only but when ever we want to
compare content of the two objects and both objects content has same but when
we compare it it return false so we should have to override equals() in our class.
Now i am going to explain methods in Objects class with examples
1. toString()
Syntax: public String toString()
{
return " ";
}
> in String class toString() method is overrided. when ever you are working with String class you no need to override toString() method
.
example without overriding toString()
public class Person
{
private String mname;
public Person(String aname)
{
mname= aname;
}
public static void main(String args[])
{
Person p = new Person("siva");
System.out.println("Name is " + p);
or //toString() method is Object class method so it will come for
any class
System.out.println("Name is " + p.toString())
}

}
ANS: Name is ClassName i.e Person@some hexadecimal value
here hexa decimal value is where the object stored in memory area
so result is not correct we need correct result for that we have to override
toString() method
attach this code for Person class
public String toString()
{
return mname;
}
Now run the program you will get result as ----- Name is siva

AddToAny

Contact Form

Name

Email *

Message *