Object Oriented Programming Using Java 25MCA551
mca a= new mca();
[Link]();
}
}
Example for final class
final class Bike
{
int a=10;
}
class Honda1 extends Bike
{
int b;
}
class finalc
{
public static void main(String args[])
{
Honda1 a= new Honda1();
a.b=25;
}
}
The Object Class
• The Object class is the parent class of all the classes in java by default. In other
words, it is the topmost class of java.
• Object class is present in [Link] package.
• Every class in Java is directly or indirectly derived from the Object class.
• If a class does not extend any other class then it is a direct child class of
Object and if extends another class then it is indirectly derived.
• Therefore the Object class methods are available to all Java classes.
• Here we can see that the class Vehicle is not inheriting any class, but it inherits
the Object class. It is an example of the direct inheritance of object class.
class Vehicle
{
Page 22
Object Oriented Programming Using Java 25MCA551
//body of class Vehicle
}
class Car extends Vehicle
{
//body of class Car
}
• Here we can see that the Car class directly inherits the Vehicle class. the
Car indirectly inherits the Object class.
Method Purpose
Object clone( ) Creates a new object that is the same as the
object being cloned.
boolean equals(Object object) Determines whether one object is equal to
another.
void finalize( ) Called before an unused object is recycled.
Class getClass( ) Obtains the class of an object at run time.
int hashCode( ) Returns the hash code associated with the
invoking object.
void notify( ) Resumes execution of a thread waiting on the
invoking object.
void notifyAll( ) Resumes execution of all threads waiting on the
invoking object.
String toString( ) Returns a string that describes the object.
void wait( ) Waits on another thread of execution.
void wait(long milliseconds) void wait(long
milliseconds, int nanoseconds)
The methods getClass( ), notify( ), notifyAll( ), and wait( ) are declared as final. You may override the
others. The equals( ) method compares the contents of two objects. It returns true if the objects are
equivalent, and false otherwise. The precise definition of equality can vary, depending on the type of
objects being compared. The toString( ) method returns a string that contains a description of the object on
which it is called. Also, this method is automatically called when an object is output using println( ).
Many classes override this method.
Page 23