Predefined Classes in Java Ellen Walker CPSC 201 Data Structures Hiram College.

26
Predefined Classes in Java Ellen Walker CPSC 201 Data Structures Hiram College

Transcript of Predefined Classes in Java Ellen Walker CPSC 201 Data Structures Hiram College.

Predefined Classes in Java

Ellen WalkerCPSC 201 Data Structures

Hiram College

Classes, Abstract Classes and Interfaces

Number: A Predefined Abstract Class

• Wrapper classes in Java - allow base types to be treated as “first-class objects” (subclasses of Object)

Object is the Root of the Hierarchy

• Every class in Java extends Object… directly, if no “extends” keyword

• All classes inherit methods in class Object… and can (sometimes should) override them

Wrapper Classes

• Allow Object’s methods to be applied to base types– Example: Integer (for int), Double (for double)

• Allow base types to benefit from polymorphism– Example: list of Objects (including Integers and Doubles)

Number

• Provides abstract methods intValue, doubleValue, etc.

• Provides one concrete method: byteValue– Returns the underlying bits of the number

Casting

• Methods can only be used with the right type of object (members of the appropriate class)

• For non-abstract methods, the type must match at compile time– Abstract methods have polymorphism; type matches at runtime

• Casting tells the compiler to assume that an object is a member of a given class

• To cast, put the class in parentheses before the object

• ClassCastException - the object doesn’t match the class that it was cast to

Casting Example

// This is a silly example to show casting

Object[] myObjects = new Object[2];myObjects[0] = new Integer(17);

myObjects[1] = new String (“hello”);

int magicnumber = myObjects[0].intValue() + myObjects[1].length(); //error

int magicnumber = ((Integer)myObjects[0]).intValue() + ((String)myObjects[1]).length();

System.out.println(magicnumber + “ should be 22”);

More on Casting

• Casting does not change the object referenced; it creates an anonymous reference to that object

• Downcast: cast a higher type (superclass) to a lower type (subclass)

• The instanceof operator can guard against ClassCastException errors

• You can downcast an interface reference to the specific implementation type

Java 5.0 Reduces Need for Casting

• Two new features that reduce the need for casting:– Autoboxing/unboxing– Generics

• Autoboxing/unboxing eases the conversion between a primitive type and its corresponding wrapper type

Object’s Methods

toString

• Represents the object as a string• Usually used for printing• Object’s toString creates a string consisting of the class name and the reference (hash code)– This is almost never what you want!– But if you don’t define your own toString, your class inherits Object’s toString

equals

• The Object.equals method has a parameter of type Object

• Compares two objects to determine whether they are equal (“this” to the parameter)

• You must override the equals method if you want to be able to compare two objects of a class

• Object’s method compares hash codes (unlikely to be useful)

The Shallow Copy Problem

• The statement e1.setAddressLine1("Room 224"); creates a new String object that is referenced by e1.address.line1 and e2.address.line1

The clone method

• Java provides the Object.clone method to help solve the shallow copy problem

• First make a shallow copy, copying the current object’s fields– myType cloned = super.clone()

• Then make a deep copy by cloning all non-primitive objects– cloned.myPart = myPart.clone();– (do this as many times as needed for every part of the object)

• Replace myType and myPart with your own object’s type and parts.

The Object.clone method (continued)

• After e1.setAddressLine1("Room 224"); only e1.address.line1 references the new String object.

Employee.clone()

Address.clone()

Multiple Inheritance, Multiple Interfaces, and

Delegation• Multiple inheritance: the ability to extend more than one class

• Multiple inheritance is a language feature that is difficult to implement and can lead to ambiguity– Therefore, Java does not allow a class to extend more than one class

Using Multiple Interfaces to Emulate Multiple

Inheritance

Using Multiple Interfaces to Emulate Multiple

Inheritance (continued)

Implementing Reuse Through Delegation

• You can reduce duplication of modifications and reduce problems associated with version control through a technique known as delegation

• In delegation, a method of one class accomplishes an operation by delegating it to a method of another class

Packages

• The Java API is organized into packages – Packages usually contain multiple classes

• Declare a package with “package packagename” at the beginning of the file

• One package per folder– All classes in the same package are stored in the same directory or folder

– All the classes in one folder must declare themselves to be in the same package

The No-Package-Declared Environment and Package

Visibility• If there’s no package statement, your files are in the default package– There is only one default package

• Package visibility sits between private and protected

• Classes, data fields, and methods with package visibility are accessible to all other methods of the same package but are not accessible to methods outside of the package

• Classes, data fields, and methods that are declared protected are visible to all members of the package

Visibility and Packages