The Java compiler will
insert the
default constructor
Example:
public class A public class A
{ {
public A() // inserted
// Class has ===> {
// no constructor // "default" constructor
}
.... ....
} }
Things that the
Java compiler
does
automatically
Recall:
Java has
anotherrule:
Every
subclass objectmust be
initialized,
therefore:
A subclass constructormustinvoke super(...)
as its first statement
Otherwise:
The Java compiler will
insertsuper();
at the start
Example:
public class B extends A public class B extends A
{ {
public B() public B()
{ {
// No super(...) call super(); // inserted
... ...
} }
.... ....
} }
There is
one more
automatic insertion that the
Java compiler will do...
The designers of the
Java programming language wants
to achieve the
followinginheritance hierachy:
I.e.:
Every class
in Java is
descended from
onespecial class
called the
Objectclass
I.e.: the
Objectclass
is the parent class of
everyclass in
Java
How
Java makes sure
every class will
inherit from the
Object class
Rule:
ifno inheritance is
specified when a
class is
defined:
I.e.:
the classdefinition does
not contains
extends
How
Java makes sure
every class will
inherit from the
Object class
Rule:
the Java compiler will
automatically insert:
extends
Object
in the class definition:
Result:
Every class in
Javainherits
from the
Object class
Therefore:Every object in
Java will
have all the methods defined
in the
Object class !
Summary: one more thing
that the
Java compiler
will do
automatically
Java has the
followingrule:
Everyobject
that you definemust have
a
parentobject,
therefore:
Every classmustextendanotherclass
If a class does
not extendanyclass:
The Java compiler will
insert:
extends
Object
Example:
public class A public class A extends Object
{ {
.... ....
.... ....
} }
I.e.:
Everyobject in
Java will inherits from
the Objectclass
The
Object class
in
Java's library
The online documentation on
the Object class
can be found here:
Somemethods in the
Object class:
clone( ):
creates and
returns a
copy of
thisobject.
equals(Object obj):
test if
this object is
equal to
the Object obj
toString( ):
returns
a String representation
of thisobject.
Every class in
Java will
inherit these
methods !!
Example on
howJava programs
make use of the
methods in
the Object class:
The System.out.print(obj)method
will invoke the
toString( ) method
to obtain its
String representation
to print out the
object obj
Therefore:
We can control the
print out of
an object by
overriding the
toString() in
a class
Using and
overriding the inherited
toString() method
Example that
shows that
the
GeometricObject classinherited the
toString() method
from the Object class
public class GeometricObject
{
private String color;
GeometricObject( String col )
{
color = col;
}
public String getColor()
{
return color;
}
public double getArea() // Dummy method
{
return 0; // Some default value
}
// No "toString()" method defined !// Inherits "toString()" from Object !
}
public class myProg
{
public static void main(String[] args)
{
GeometricObject a =
new GeometricObject("red");
// We can call the toString() method // using a GeometricObject object
System.out.println( a.toString( ) );
}
}
DEMO:
demo/04-inheritance/15-toString
Using and
overriding the inherited
toString() method
Example that
shows that
the
Circle classalsoinherited the
toString() method:
public class Circle extends GeometricObject
{
private double radius;
Circle(String col, double r)
{
super(col);
radius = r;
}
public double getRadius()
{
return radius;
}
public double getArea()
{
return 3.14159*radius*radius;
}
// Inherits "toString()" from GeomObject
}
public class myProg
{
public static void main(String[] args)
{
Circle a =
new Circle("red", 2.0);
// We can call the toString() method // using a Circle object
System.out.println( a.toString( ) );
}
}
DEMO:
demo/04-inheritance/16-toString
Using and
overriding the inherited
toString() method
We can override the
inheritedtoString()
method
to print out an
object in a
more suitableformat:
public class Circle extends GeometricObject
{
private double radius;
Circle(String col, double r)
{
super(col);
radius = r;
}
...
// Override the "toString()" method
public String toString()
{
return "Color = " + getColor()
+ " : "
+ "radius = " + radius;
}
}
public class myProg
{
public static void main(String[] args)
{
Circle a =
new Circle("red", 2.0);
// We can call the toString() method // using a Circle object
System.out.println( a.toString( ) );
System.out.println( a ); // Same !
}
}