Abstraction & Object Class
Abstraction & Object Class
Interfaces,
Object Class,
T.Varun
Abstraction
• Abstraction is a process hiding the
implementation
of details and showing only
functionality to the user.
Object obj=showObject(); //we don't know what object will be returned from this method
• The Object class defines the basic state and behavior that all objects must
have,
– such as the ability to compare oneself to another object,
– to convert to a string,
– to wait on a condition variable,
– to notify other objects that a condition variable has changed, and
– to return the object's class.
Methods of Object Class
• The equals Method: Use the equals to compare two objects
for equality. This method returns true if the objects are
equal, false otherwise.
if (A.equals(B))
System.out.println("objects are equal");
o Though A and B reference two different, distinct objects, they are considered equal because they
contain the same integer value.
o Your equals method should compare the contents of the objects to see if they are functionally
equal.
Contd..
• The getClass Method: The getClass method is a final method (cannot be
overridden) that returns a runtime representation of the class of this
object. This method returns a Class object.
• You can query the Class object for a variety of information about the
class, such as its name, its superclass, and the names of the interfaces
that it implements.
• Example: The following method gets and displays the class name of an object:
• One handy use of the getClass method is to create a new instance of a class
without knowing what the class is at compile time. This sample
method creates a new instance of the same class as obj which can be any
class that inherits from Object (which means that it could be any
class):
Object createNewInstanceOf(Object obj)
{ return obj.getClass().newInstance();
}
Contd..
• Example:
System.out.println(Thread.currentThread().toString());
Methods of Object class
Method Description
returns the Class class object of this object. The Class class
public final Class getClass() can further be used to get the metadata of this class.
public int hashCode() returns the hashcode number for this object.
public final void notify() wakes up single thread, waiting on this object's monitor.
Contd..
public final void notifyAll() wakes up all the threads, waiting on this object's monitor.
public final void wait(long causes the current thread to wait for the specified
timeout)throws milliseconds, until another thread notifies (invokes notify()
InterruptedException or notifyAll() method).
public final void wait(long causes the current thread to wait for the specified
timeout,int nanos)throws milliseconds and nanoseconds, until another thread
InterruptedException notifies (invokes notify() or notifyAll() method).
public final void wait()throws causes the current thread to wait, until another thread
InterruptedException notifies (invokes notify() or notifyAll() method).
protected void finalize()throws is invoked by the garbage collector before object is being
Throwable garbage collected.