Lecture 4
Lecture 4
Programming II
2 – Inheritance and
Polymorphism(2)
1
2– Inheritance and Polymorphism-
Outline
Instance Variables and Methods
Scope of Variables
Wrapper Classes
2
Instance Instance variables
Variables, belong to a specific
instance.
and Instance methods are
Methods invoked by an instance
of the class.
3
Static
members/methods
Static have only one copy
regardless of how
Variables, many instances are
Constants, created so a static
variable
and A static method can
Methods be invoked without
the need for creating
an instance of a class
4
Static variables are
shared by all the
instances of the
Static class.
Variables, Static methods are
Constants, not tied to a specific
object.
and
Static constants are
Methods final variables shared
by all the instances
of the class.
5
To declare static
Static variables, constants,
and methods, use the
Variables, static modifier.
Constants, A variable or method
and that is not dependent
on a specific instance
Methods, of the class should be a
cont. static variable or
method.
6
Example of Using Instance and
Class Variables and Method
Circle
The - sign indicates
private modifier -radius: double The radius of this circle (default: 1.0).
-numberOfObjects: int The number of circle objects created.
7
Static Variables, Constants,
and Methods, cont.
instantiate Memory
circle1
radius = 5 5 radius
UML Notation: numberOfObjects = 2
+: public variables or methods
underline: static variables or methods
8
Since static variables and methods do not
Static belong to any instance:
9
Static Variables, Constants,
and Methods, cont.
What’s wrong with the code below?
public class Foo {
int i=5;
static int k=2;
public static void main(String[] args) {
int j=i;
m1();
}
public void m1() {
i = i + k + m2(i,k);
}
public static int m2(int i, int j) {
return (int) (Math.pow(i,j));
}
}
10
Static Variables, Constants,
and Methods, cont.
What’s wrong with the code below?
public class Foo {
int i=5;
static int k=2;
public static void main(String[] args) {
// ERROR: non-static variable i cannot be referenced from a static context
int j=i;
m1();
}
public void m1() {
i = i + k + m2(i,k);
}
public static int m2(int i, int j) {
return (int) (Math.pow(i,j));
}
} 11
Given the following class:
public class F {
static int j;
Static double d;
void xmethod() {
Variables, }
static void ymethod() {
Constants, }
}
Methods, 1.
2.
f.d = f.j;
f.j = (int) f.d;
cont. 3.
4.
F.d = F.j;
F.j = (int) F.d;
5. F.xmethod();
6. F.ymethod();
12
Given the following class:
public class F {
static int j;
Static double d;
void xmethod() {
Variables, }
static void ymethod() {
Constants, }
}
Methods, 1.
2.
f.d = f.j;
f.j = (int) f.d;
cont. 3.
4.
F.d = F.j;
F.j = (int) F.d;
5. F.xmethod();
6. F.ymethod();
13
The scope of instance and
static variables is the entire
class. They can be declared
anywhere inside a class.
Scope of The scope of a local variable
starts from its declaration and
Variables continues to the end of the
block that contains the variable.
A local variable must be
initialized explicitly before it
can be used.
14
The Object Class - Method
common to all objects
Although Object is a concrete class, it is designed primarily for
extension.
All of its non-final methods have explicit general contracts because they
are designed to be overridden.
protected Object clone() throws CloneNotSupportedException
public boolean equals(Object obj)
public int hashCode()
public String toString()
It is the responsibility of any class overriding these methods to obey
their general contracts; failure to do so will prevent other classes that
depend on the contracts (such as HashMap and HashSet) from
functioning properly in conjunction with the class.
15
Two kinds of object equality
16
When not to override equals
17
Override the equals method
20
Need of Wrapper Classes
Boolean Integer
Character Long
Short Float
Byte Double
NOTE: The wrapper classes do not have no-arg constructors.
NOTE: The instances of all wrapper classes are immutable,
i.e., their internal values cannot be changed once the
objects are created.
22
The toString, equals, and
hashCode Methods
Each wrapper class overrides
the toString(), equals(), and hashCode() methods
defined in the Object class.
Since all the numeric wrapper classes and
the Character class
implement the Comparable interface,
the compareTo() method is implemented in these
classes.
23
The Integer and Double Classes
java.lang.Integer java.lang.Double
-value: int -value: double
+MAX_VALUE: int +MAX_VALUE: double
+MIN_VALUE: int +MIN_VALUE: double
24
The Integer Class
and the Double Class
Class Constants
Conversion
Constructors MAX_VALUE,
Methods
MIN_VALUE
25
Numeric Wrapper Class
Constructors
You can construct a wrapper object either from a
primitive data type value or from a string representing
the numeric value. The constructors for Integer and
Double are:
public Integer(int value)
public Integer(String s)
public Double(double value)
public Double(String s)
26
Numeric Wrapper Class
Constants
Each numerical wrapper class has the constants
MAX_VALUE and MIN_VALUE.
MAX_VALUE represents the maximum value of the corresponding
primitive data type.
For Byte, Short, Integer, and Long, MIN_VALUE represents the
minimum byte, short, int, and long values.
For Float and Double, MIN_VALUE represents the
minimum positive float and double values.
The following statements display the maximum integer (2,147,483,647),
the minimum positive float (1.4E-45), and the maximum double floating-
point number (1.79769313486231570e+308d).
System.out.println(Integer.MAX_VALUE);
System.out.println(Float.MIN_VALUE);
System.out.println(Double.MAX_VALUE);
27
Conversion Methods
28
The Static valueOf Methods
29
Automatic Conversion
between Primitive Types and
Wrapper Class Types
Converting a primitive value to a wrapper object is called
boxing. The reverse conversion is called unboxing. The
compiler will automatically box a primitive value that
appears in a context requiring an object, and will unbox an
object that appears in a context requiring a primitive
value.
30
Consider implementing
Comparable
Unlike the other methods discussed, the compareTo() method
is not declared in Object. Rather, it is the sole method in the
Comparable interface.
By implementing Comparable, you allow your class to
interoperate with all of the many generic algorithms and
collection implementations that depend on this interface.
If you are writing a value class with an obvious natural
ordering, such as alphabetical order, numerical order, or
chronological order, you should strongly consider
implementing the interface.
public interface Comparable 〈T 〉 {
int compareTo(T y)
}
The compareTo() method compares this object with the
specified object for order and returns a negative integer,
zero, or a positive integer as this object is less than, equal
to, or greater than the specified object.
31
Contract of the compareTo
method
x.compareTo(y) > 0 if and only if y.compareTo(x) < 0.
x.compareTo(y) == 0 if and only if y.compareTo(x) == 0.
(transitivity) x.compareTo(y) > 0 && y.compareTo(z) > 0 implies
x.compareTo(z) > 0.
x.compareTo(y) == 0 implies that x.compareTo(z) agrees with
y.compareTo(z).
It is strongly recommended, but not strictly required, that
(x.compareTo(y) == 0) == (x.equals(y)).
32
Example: compareTo method
33