0% found this document useful (0 votes)
7 views

Module 3 Voop

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Module 3 Voop

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

OBJECT ORIENTED

PROGRAMMING WITH
JAVA
Department of Computer Science & Engineering

www.cambridge.edu.in
Object-Oriented Programming : The Three OOP Principles -
Inheritence
• Inheritance is the process by which one object acquires the properties of another object.
• Inheritance interacts with encapsulation as well. If a given class encapsulates some attributes, then any
subclass will have the same attributes plus any that it adds as part of its specialization (see Figure 2-2).
• A new subclass inherits all of the attributes of all of its ancestors.
Inheritance
• Inheritance is one of the cornerstones of object-oriented programming offering benefits like code extensibility
(Extensibility is a measure of the ability to extend a system through the addition of new functionality or
through modification of existing functionality) and code reusability (Code reuse is the practice of using existing
code for a new function or software).
• Using inheritance, we can create a general class that defines traits common to a set of related items. This class
can then be inherited by other, more specific classes, each adding those things that are unique to it.
• In the terminology of Java, a class that is inherited is called a superclass (parent class or base class).
• The class that does the inheriting is called a subclass(child class or inherited class). Therefore, a subclass is a
specialized version of a superclass. It inherits all of the instance variables and methods defined by the superclass
and adds its own elements.
Inheritance Basics
• To inherit a class, we simply incorporate the definition of one class into another by using the extends keyword.
• The following program creates a superclass called A and a subclass called B.
Inheritance -
class Demo
Example {
public static void main(String args[])
// Create a superclass. {
class A A superob = new A();
{ B subob = new B();
int i, j; superob.i = 10;
void showij( ) superob.j = 20;
{ System.out.println("Contents of superob: ");
System.out.println("i and j: " + i + " " + j); superob.showij();
} subob.i = 7;
} subob.j = 8;
// Create a subclass by extending class A. subob.k = 9;
class B extends A System.out.println("Contents of subob: ");
{ subob.showij();
int k; subob.showk();
void showk( ) System.out.println("Sum of i, j and k in subob:");
{ subob.sum();
System.out.println("k: " + k); }
} }
void sum()
{
System.out.println("i+j+k: " + (i+j+k));
}
}
Inheritance - We can only specify
one superclass for any

Example subclass that we


create.
Java does not support
Output
the inheritance of
The output from this program is shown here: multiple superclasses
Contents of superob: into a single subclass.
i and j: 10 20 We can, create a
Contents of subob: hierarchy of
i and j: 7 8 inheritance in which a
k: 9 subclass becomes a
Sum of i, j and k in subob: superclass of another
i+j+k: 24 subclass.
As we can see, the subclass B includes all of the members of its superclass, A. This is why subOb
can access i and j and call showij( ). Also, inside sum( ), i and j can be referred to directly, as if There is no
they were part of B. multiple
Even though A is a superclass for B, it is also a completely independent, stand-alone class. inheritance of
Further, a subclass can be a superclass for another subclass. classes in Java
The general form of a class declaration that inherits a superclass is shown here:
class subclass-name extends superclass-name
{
// body of sub class
}
Inheritance – Types of Inheritance of classes in
Java There are 3 types of //Hiererchical //Multilevel Inheritence
Inheritence of Inheritence class A
classes – single, class A {
multi-level, {
hierarchical in Java }
//Single Inheritence } class B extends A
class A class B extends A {
{ {
}
} } class C extends B
class B extends A class C extends A {
{ {
}
} } class D extends C
class D extends A {
There is no {
multiple
}
inheritance of }
classes in Java
Inheritence - Member Access and Inheritance
• Although a subclass includes all of the members of its superclass, it cannot access those members of the
superclass that have been declared as private.
class Demo
class A {
{ public static void main(String args[ ])
int i; // public within a package by default {
private int j; // private to A B subob = new B();
void setij(int x, int y) subob.setij(10, 12);
{ subob.sum();
i = x; System.out.println("Total is " +subOb.total);
j = y; }
} }
}
class B extends A
{
int total;
void sum() Here, the final version of the Box class developed in
{ the preceding chapter will be extended to include
total = i + j; // ERROR, j is not accessible here a fourth component called weight.
}
}
Inheritance – A Practical Example
class Box // Here, Box is extended to include weight.
{ class BoxWeight extends Box
double width, height,depth;
{
Box(Box ob) // pass object to constructor
{
double weight; // weight of box
width = ob.width; // constructor for BoxWeight
height = ob.height; BoxWeight(double w, double h, double d, double m){
depth = ob.depth; width = w;
} height = h;
Box(double w, double h, double d) // constructor used when all depth = d; OUTPUT
{ //dimensions specified Volume of mybox1 is 3000.0
weight = m;
width = w; Weight of mybox1 is 34.3
height = h;
}
depth = d; } Volume of mybox2 is 24.0
} class DemoBoxWeight Weight of mybox2 is 0.076
Box() // constructor used when no dimensions specified {
{ public static void main(String args[]) {
width = -1; // use -1 to indicate BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
height = -1; // an uninitialized
BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);
depth = -1; // box
}
double vol;
Box(double len) // constructor used when cube is created vol = mybox1.volume();
{ System.out.println("Volume of mybox1 is " + vol);
width = height = depth = len; System.out.println("Weight of mybox1 is " + mybox1.weight);
} vol = mybox2.volume();
double volume() // compute and return volume System.out.println("Volume of mybox2 is " + vol);
{
System.out.println("Weight of mybox2 is " + mybox2.weight);
return width * height * depth;
}
}
} }
Inheritance
• BoxWeight inherits all of the characteristics of Box and adds to them the weight component.
• It is not necessary for BoxWeight to re-create all of the features found in Box. It can simply extend Box to meet its own purposes.
• A major advantage of inheritance is that once we have created a superclass that defines the attributes common to a set of
objects, it can be used to create any number of more specific subclasses (Hierarchical Inheritance).

// Here, Box is extended to include color.


class ColorBox extends Box
{
int color; // color of box
ColorBox(double w, double h, double d, int c)
{
width = w;
height = h;
depth = d;
color = c;
}
}
A Superclass Variable Can Reference a Subclass Object

• A reference variable of a superclass can be assigned a reference to any subclass derived from that superclass.
class RefDemo
{
public static void main(String args[ ])
{ Volume of weightbox is 105.0
BoxWeight weightbox = new BoxWeight(3, 5, 7, 8.37); Weight of weightbox is 8.37
Box plainbox = new Box( ); Volume of plainbox is 105.0
double vol;
vol = weightbox.volume( );
System.out.println("Volume of weightbox is " + vol);
System.out.println("Weight of weightbox is " +weightbox.weight);
// assign BoxWeight reference to Box reference
plainbox = weightbox; // all dogs are animals
vol = plainbox.volume( ); // OK, volume() defined in Box
System.out.println("Volume of plainbox is " + vol);
/* The following statement is invalid because plainbox does not define a weight member. */
// System.out.println("Weight of plainbox is " + plainbox.weight);
}
}
A superclass can reference a subclass object
• Here, weightbox is a reference to BoxWeight objects, and plainbox is a reference to Box objects.
• Since BoxWeight is a subclass of Box, it is permissible to assign plainbox a reference to the weightbox object.
• It is important to understand that it is the type of the reference variable—not the type of the object that it refers
to—that determines what members can be accessed.
• That is, when a reference to a subclass object is assigned to a superclass reference variable, we will have access
only to those parts of the object defined by the superclass.
• This is why plainbox can’t access weight even when it refers to a BoxWeight object. If we think about it,
this makes sense, because the superclass has no knowledge of what a subclass adds to it.
• This is why the last line of code in the preceding fragment is commented out. It is not possible for a Box
reference to access the weight field, because Box does not define one.
Using super
• The constructor for BoxWeight explicitly (in the previous example) initializes the width, height, and depth
fields of Box( ).
• Not only does this duplicate code found in its superclass, which is inefficient, but it implies that a subclass must
be granted access to these members.
• However, there will be times when we will want to create a superclass that keeps the details of its
implementation to itself.
• In this case, there would be no way for a subclass to directly access or initialize these variables on its own.
• Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super.
• super is a reference variable that is used to refer to the immediate superclass.
super has two general forms.
1) The first calls the superclass constructor.
2) Used to access a member of the superclass that has been hidden by a member of a subclass.
Using super – (1) Using super to Call Superclass Constructors

• A subclass can call a constructor defined by its superclass by use of the following form of super:
super(arg-list);
• Here, arg-list specifies any arguments needed by the constructor in the superclass.
• super( )must always be the first statement executed inside a subclass’ constructor.
• To see how super( ) is used, consider this improved version of the BoxWeight( ) class:
class BoxWeight extends Box
{
double weight;
BoxWeight(double w, double h, double d, double m)
{
super(w, h, d);
weight = m;
}
}
Using super – (1) Using super to Call Superclass parameterless
class A
{
Constructors class Demo
{
int i, j; public static void main(String args[])
A()
{
{
i=10; B subob = new B();
j=20; subob.showij();
} subob.showk();
void showij( ) System.out.println("Sum of i, j and k in subob:");
{ subob.sum();
System.out.println("i and j: " + i + " " + j); }
}
}
}
class B extends A
{
int k;
B()
{
super();
k=20; i and j: 10 20
}
void showk( ) k: 20
{ Sum of i, j and k in subob:
System.out.println("k: " + k); i+j+k: 50
}
void sum()
{
System.out.println("i+j+k: " + (i+j+k));
}
}
Using super – (1) Using super to Call Superclass Constructors with paramaters
class A class Demo
{ {
int i, j; public static void main(String args[])
A(int x,int y)
{
{
i=x; B subob = new B(10,20,30);
j=y; subob.showij();
} subob.showk();
void showij( ) System.out.println("Sum of i, j and k in subob:");
{ subob.sum();
System.out.println("i and j: " + i + " " + j); }
}
}
}
class B extends A
{
int k;
B(int x,int y,int z)
{
super(x,y);
k=z; i and j: 10 20
}
void showk( ) k: 30
{ Sum of i, j and k in subob:
System.out.println("k: " + k); i+j+k: 60
}
void sum()
{
System.out.println("i+j+k: " + (i+j+k));
}
}
Using super to Call Superclass Constructors

• Here, BoxWeight( ) calls super( ) with the arguments w, h, and d. This causes the Box( )
constructor to be called, which initializes width, height, and depth using these values.
• BoxWeight no longer initializes these values itself. It only needs to initialize the value unique
to it: weight. This leaves Box free to make these values private if desired.
• In the preceding example, super( ) was called with three arguments.
• Since constructors can be overloaded, super( ) can be called using any form defined by the
superclass.
• The constructor executed will be the one that matches the arguments.
• When a subclass calls super( ), it is calling the constructor of its immediate superclass.
• Thus, super( ) always refers to the superclass immediately above the calling class. This is
true even in a multileveled hierarchy.
Using super – The Second use of super keyword
A Second Use for super
• The second form of super acts somewhat like this, except that it always refers to the
superclass of the subclass in which it is used. This usage has the following general form:
super.member

• Here, member can be either a method or an instance variable.


• This second form of super is most applicable to situations in which member names of a
subclass hide members by the same name in the superclass(prevents superclass’s instance
variable hiding).
Using super – The Second use of super keyword
// Using super to overcome name hiding.
class A
{
int i;
} class UseSuper
// Create a subclass by extending class A. {
class B extends A public static void main(String args[ ])
{ {
int i; // this i hides the i in A B subob = new B(1, 2);
B(int a, int b) subob.show();
{ }
super.i = a; // i in A }
i = b; // i in B
} OUTPUT
void show() i in superclass: 1
{ i in subclass: 2
System.out.println("i in superclass: " + super.i);
System.out.println("i in subclass: " + i);
}
}
Using super – The Second use of super keyword
class A class Demo
{ {
int i, j; public static void main(String args[])
A(int x,int y)
{
{
i=x; B subob = new B(10,20,30);
j=y; subob.showk();
} System.out.println("Sum of i, j and k in subob:");
void showij( ) subob.sum();
{ }
System.out.println("i and j: " + i + " " + j); }
}
}
class B extends A {
int i,j,k;
B(int x,int y,int z){
super(x,y);
k=z;
} i and j: 10 20
void showk( )
k: 30
{
super.showij(); Sum of i, j and k in subob:
System.out.println("k: " + k); i+j+k: 60
}
void sum()
{
System.out.println("i+j+k: " + (super.i+super.j+k));
}
}
CREATING A MULTILEVEL HIERARCHY

• we have been using simple class hierarchies that consist of only a


superclass and a subclass.
• It is perfectly acceptable to use a subclass as a superclass of another.
For example, given three classes called A, B, and C, C can be a
subclass of B, which is a subclass of A.
• When this type of situation occurs, each subclass inherits all of the
traits found in all of its superclasses.
• In this case, C inherits all aspects of B and A.
CONTINUE …..

• The subclass BoxWeight is used as a superclass to create the subclass


called Shipment.
• Shipment inherits all of the traits of BoxWeight and Box, and adds a
field calledcost, which holds the cost of shipping such a parcel
class Box { // Add weight.
private double width; class BoxWeight extends Box {
private double height; double weight; // weight of box
private double depth;
// construct clone of an object
// construct clone of an object
Box(Box ob) { // pass object to constructor BoxWeight(BoxWeight ob) { // pass object to constructor
width = ob.width; super(ob);
height = ob.height; weight = ob.weight;
depth = ob.depth; }
}constructor used when all dimensions specified //constructor when all parameters are specified
Box(double w, double h, double d) { BoxWeight(double w, double h, double d, double m) {
width = w;
height = h;
super(w, h, d); // call superclasS constructor
depth = d; weight = m;
}constructor used when no dimensions specified }//default constructor
Box() { BoxWeight() {
width = -1; // use -1 to indicate super();
height = -1; // an uninitialized weight = -1;
depth = -1;// box }
}constructor used when cube is created
Box(double len) {
// constructor used when cube is created
width = height = depth = len; BoxWeight(double len, double m) {
}compute and return volume super(len);
double volume() { weight = m;
return width * height * depth; }
} }
}
// Add shipping costs.
class Shipment extends BoxWeight {
double cost;
// construct clone of an object
Shipment(Shipment ob) { // pass object to constructor
super(ob);
cost = ob.cost;
}//constructor when all parameters are specified
Shipment(double w, double h, double d,double m, double c) {
super(w, h, d, m); // call superclass constructor
cost = c;
}//default constructor
Shipment() {
super();
cost = -1;
}
// constructor used when cube is created
Shipment(double len, double m, double c) {
super(len, m);
cost = c;
}
}
class DemoShipment {
public static void main(String args[]) {
Shipment shipment1 =new Shipment(10, 20, 15, 10, 3.41);
Shipment shipment2 =new Shipment(2, 3, 4, 0.76, 1.28);
double vol;
vol = shipment1.volume();
System.out.println("Volume of shipment1 is " + vol);
System.out.println("Weight of shipment1 is " + shipment1.weight);
System.out.println("Shipping cost: $" + shipment1.cost);
System.out.println();
vol = shipment2.volume();
System.out.println("Volume of shipment2 is " + vol);
System.out.println("Weight of shipment2 is " + shipment2.weight);
System.out.println("Shipping cost: $" + shipment2.cost);
}
}
OUTPUT
Volume of shipment1 is 3000.0
Weight of shipment1 is 10.0
Shipping cost: $3.41
Volume of shipment2 is 24.0
Weight of shipment2 is 0.76
Shipping cost: $1.28
• Because of inheritance, Shipment can make use of the previously
defined classes of Box and BoxWeight, adding only the extra
information it needs for its own, specific application.
• This is part of the value of inheritance; it allows the reuse of code.
• In a class hierarchy, if asuperclass constructor requires parameters,
then all subclasses must pass those parameters “up the line.”
When Constructors Are Executed
• For example, given a subclass called B and a superclass called A,
is A’s constructor executed before B’s, or vice versa?
• The answer is that in a class hierarchy, constructors complete their
execution in order of derivation, from superclass to subclass.
• Since super( ) must be the first statement executed in a subclass’
constructor, this order is the same whether or not super( ) is used.
• If super( ) is not used, then the default or parameterless constructor
of each superclass will be executed.
Demonstrate when constructors are executed.
Create a super class.
class A {
A() {
System.out.println("Inside A's constructor.");
}
}
//Create a subclass by extending class A.
class B extends A {
B() {
System.out.println("Inside B's constructor.");
}
}
//Create another subclass by extending B.
class C extends B {
C() {
System.out.println("Inside C's constructor.");
}
}
class CallingCons { Inside A's constructor
public static void main(String args[])
{ Inside B's constructor
C c = new C();

}
} Inside C's constructor
• It makes sense that constructors complete their execution in order of
derivation.
• Because a superclass has no knowledge of any subclass, any
initialization it needs to perform is separate from and possibly
prerequisite to any initialization performed by the subclass.
• Therefore, it must complete its execution first.
Method Overriding

• when a method in a subclass has the same name and type signature as
a method in its superclass, then the method in the subclass is said
to override the method in the superclass.
• When an overridden method is called from within its subclass, it will
always refer to the version of that method defined by the subclass.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// display k – this overrides show() in A
void show() {
System.out.println("k: " + k);
}
}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}
• When show( ) is invoked on an object of type B, the version of show(
) defined within B is used.
• That is, the version of show( ) insideB overrides the version declared
in A.
• Method overriding occurs only when the names and the type
signatures of the two methods are identical.
• If they are not, then the two methods are simply overloaded.
Dynamic Method Dispatch
• Dynamic method dispatch is the mechanism by which a call to an
overridden method is resolved at run time, rather than compile time.
• Dynamic method dispatch is important because this is how Java
implements run-time polymorphism.
class A {
void callme() {
System.out.println("Inside A's callme method");
}
}
class B extends A { // override callme()
void callme() {
System.out.println("Inside B's callme method");
}
}
class C extends A { // override callme()
void callme() {
System.out.println("Inside C's callme method");
}
}
class Dispatch {
public static void main(String args[]) {
A a = new A(); // object of type A
B b = new B(); // object of type B
C c = new C(); // object of type C
A r; // obtain a reference of type A
r = a; // r refers to an A object
r.callme(); // calls A's version of callme
r = b; // r refers to a B object
r.callme(); // calls B's version of callme
r = c; // r refers to a C object
r.callme(); // calls C's version of callme
}
}
• This program creates one superclass called A and two subclasses of it,
called B and C.
• Subclasses B and C override callme( )declared in A.
• Inside the main( ) method, objects of type A, B, and C are declared.
• Also, a reference of type A, called r, is declared.
• The program then in turn assigns a reference to each type of object
to r and uses that reference to invoke callme( ).
• As the output shows, the version of callme( ) executed is determined
by the type of object being referred to at the time of the call.
• Had it been determined by the type of the reference variable, r, you
would see three calls to A’s callme( ) method.
Why Overridden Methods?
• Overridden methods allow Java to support run-time polymorphism.
• Polymorphism is essential to object-oriented programming for one
reason: it allows a general class to specify methods that will be
common to all of its derivatives, while allowing subclasses to define
the specific implementation of some or all of those methods.

• Overridden methods are another way that Java implements the “one
interface, multiple methods” aspect of polymorphism.
• Polymorphism is understanding that the superclasses and subclasses
form a hierarchy which moves from lesser to greater specialization.
• Used correctly, the superclass provides all elements that a subclass
can use directly.
• It also defines those methods that the derived class must implement
on its own.
• This allows the subclass the flexibility to define its own methods, yet
still enforces a consistent interface.
• Thus, by combining inheritance with overridden methods, a
superclass can define the general form of the methods that will be
used by all of its subclasses.
Using Abstract Classes

• There are situations in which you will want to define a superclass that
declares the structure of a given abstraction without providing a
complete implementation of every method.
• That is, sometimes you will want to create a superclass that only
defines a generalized form that will be shared by all of its subclasses,
leaving it to each subclass to fill in the details.
• Such a class determines the nature of the methods that the subclasses
must implement.
• One way this situation can occur is when a superclass is unable to
create a meaningful implementation for a method.
• Consider the class Triangle. It has no meaning if area( ) is not
defined.
• In this case, you want some way to ensure that a subclass does,
indeed, override all necessary methods.
• Java’s solution to this problem is the abstract method.
• we can require that certain methods be overridden by subclasses by
specifying the abstract type modifier.
• These methods are sometimes referred to as subclasser
responsibility because they have no implementation specified in the
superclass.
• Thus, a subclass must override them—it cannot simply use the
version defined in the superclass. To declare an abstract method, use
this general form:
• abstract type name(parameter-list);
• Any class that contains one or more abstract methods must also be
declared abstract.
• To declare a class abstract, you simply use the abstract keyword in
front of the class keyword at the beginning of the class declaration.
• There can be no objects of an abstract class. That is, an abstract class
cannot be directly instantiated(CREATED) with the new operator.
• Such objects would be useless, because an abstract class is not fully
defined.
• Also, you cannot declare abstract constructors, or abstract static
methods.
• Any subclass of an abstract class must either implement all of the
abstract methods in the superclass, or be declared abstract itself.
abstract class A {
abstract void callme();
// concrete methods are still allowed in abstract
void callmetoo() {
System.out.println("This is a concrete method.");
}
}
class B extends A {
@Override
void callme() {
System.out.println("B's implementation of callme.");
}
}
class AbstractDemo {
public static void main(String args[])
{ B's implementation of callme.
B b = new B();
b.callme(); This is a concrete method.
b.callmetoo();
}
}
• Notice that no objects of class A are declared in the program. As
mentioned, it is not possible to instantiate an abstract class.
• One other point: class A implements a concrete method
called callmetoo( ).
• This is perfectly acceptable. Abstract classes can include as much
implementation as they see fit.
• It is no longer possible to declare objects of type Figure, since it
is now abstract.
• And, all subclasses of Figure must override area( ).
• To prove this to yourself, try creating a subclass that does not
override area( ). You will receive a compile-time error.
• It is not possible to create an object of type Figure, you can create
a reference variable of type Figure.
• The variable figref is declared as a reference to Figure, which
means that it can be used to refer to an object of any class derived
from Figure.
• As explained, it is through superclass reference variables that
overridden methods are resolved at run time.
Using final with Inheritance
• The keyword final has three uses.
• First, it can be used to create the equivalent of a named constant(next).
• Second, Using final to Prevent Overriding.
• Third,Using final to Prevent Inheritance.
Using final to Prevent Overriding

• To disallow a method from being overridden, specify final as a


modifier at the start of its declaration.
• Methods declared as final cannot be overridden.
class A {
final void meth() {
System.out.println("This is a final method");
}
}
class B extends A {
void meth() { // ERROR! Can't override.
System.out.println("Illegal!");
}
}
Because meth( ) is declared as final, it cannot be
overridden in B. If you attempt to do so, a
compile-time error will result.
• Methods declared as final can sometimes provide a
performance enhancement: The compiler is free to inline calls
Inline refers to them because it “knows” they will not be overridden by a
to a subclass.
computing
term where • When a small final method is called, often the Java compiler
code or data
is inserted
can copy the bytecode for the subroutine directly inline with the
directly into compiled code of the calling method, thus eliminating the costly
its overhead associated with a method call.
appropriate
place within • Inlining is an option only with final methods. Normally, Java
a larger
block of
resolves calls to methods dynamically, at run time.
code,srather • This is called late binding. However, since final methods
than being
called from cannot be overridden, a call to one can be resolved at compile
a separate time. This is called early binding.
location.
Using final to Prevent Inheritance

• Sometimes you will want to prevent a class from being inherited.


• To do this, precede the class declaration with final.
• Declaring a class as final implicitly declares all of its methods
as final, too.
• It is illegal to declare a class as both abstract and final since an
abstract class is incomplete by itself and relies upon its subclasses to
provide complete implementations.
The Object Class
• There is one special class, Object, defined by Java.
• All other classes are subclasses of Object. That is, Object is a
superclass of all other classes.
• This means that a reference variable of type Object can refer to an
object of any other class.
• Also, since arrays are implemented as classes, a variable of
type Object can also refer to any array.
Object defines the following methods, which means that they are available
in every object.
• The methods getClass( ), notify( ), notifyAll( ), and wait( ) are
declared as final.
• The methods may override.
• Two methods now: equals( ) and toString( ).
• The equals( ) method compares two objects. It returns true if the
objects are equal, 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. Doing so allows them to tailor
a description specifically for the types of objects that they create.

You might also like