DOC PREVIEW
Berkeley COMPSCI 61B - Lecture Notes

This preview shows page 1-2-22-23 out of 23 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1AnnouncementsTodayObjectObjectFrom Lecture 3:equals()equals()equals() in SubclassA Common Mistake@OverridetoString()PackagesExamples of PackagesUsing PackagesNaming ConflictStatic ImportAdding a Class to a PackageHow does javac and java Locate Files?javacFrom Day 4Package VisibilityNext TimeCS 61B Data Structures and Programming Methodology July 3, 2008David SunAnnouncements•Project 1 is out! Due July 15th. Check the course website.•Reminder: the class newsgroup ucb.class.cs61b should be considered as required reading.Today•Object•equals()•PackagesObject •Object class is the ultimate ancestor – every class in Java extends from the Object class.StudentEmployeeHumanObjectclass Human extends Objectclass Employee extends Human class Employee extends HumanObject•A variable of class Object can be used as a generic placeholder for a reference to an object of any type.Object obj = new Employee(“Hacker Joe”, 35000);•To do anything specific with obj, you need to cast it to some type:((Employee)obj).raiseSalary(0.4);•All array types extend the object class:Employee[] staff = new Employee[10];obj = staff;obj = new int[10];From Lecture 3:•Key characteristics:–Object’s Behavior: what can you do with the object, what methods can you call?–Object’s State: how does the object react to methods applied to it? What does it look like?–Object’s Identity: how to distinguish the object from others with same behavior and state?equals()•A method in the Object class that tests whether one object is considered equal to another.xyEmployeeReference based equality (default implementation in Java)xyEmployeename = “dave”id = 101name = “dave”id = 101State-based equalityequals()class Employee { . . .public boolean equals(Object otherObject){//a quick test to see if two objects are equalif (this == otherObject) return true;//must return false if the explicit parameter is nullif(otherObject == null) return false;//if the classes don’t match they can’t be equalif (getClass() != otherObject.getClass())return false;//now we know otherObject is a non-null employeeEmployee other = (Employee) otherObject;//test wehther the fields have identical valuesreturn name.equals(other.name) && salary == other.salary && hireDate.equals(hireDate);}}equals() in Subclass•When defining the equals method of a subclass, first call the superclass equals, then compare the instance fields of the subclass:class Manager{ . . .public boolean equals(Object otherObject){if (!super.equals(otherObject)) return false;//super.equals checked that this and otherObject are in the same//subclassManager other = (Manager) otherObject;return bonus = other.bonus;}}A Common Mistakepublic class Employee {public boolean equals(Employee other){return name.equals(other.name) &&salary == other.salary &&ID == other.ID;}}•The explicit parameter other is declared to be Employee, not Object. So public boolean equals(Employee other) will not override the methodpublic boolean equals(Object other)•The compiler and the run time will not pick this up because equals(Employee other) is treated using method overloading (not overriding).@Override•Since Java 5.0, you can protect yourself against this type of error with the metadata tag @Override:@Override public boolean equals(Object other)•If you make a mistake and the new method does not override any method in the superclass then the compiler will pick up the error. @Override public boolean equals(Employee other)toString()•Another method in Object that returns a string representation of the value of this object. •When you do :Employee x = new Employee(…);System.out.println(x); //calls the toString() //method on x returns a //string object to //Systme.out.println•Default implementation in Java prints the class name and the hash code of the object:System.out.println(System.out)java.io.PrintStream@2f6684Packages•Java organizes “related” of classes into collections called pa cka g es. •Packages are used to separate your work from code provided by others:.–Guarantee the uniqueness of class names. –Packages can contain hidden classes that are used by the package but are not visible or accessible outside the package. –Classes in packages can have fields and methods that are visible by all classes inside the package, but not outside.Examples of Packages•The Java Standard Library is contained in java and javax;•In the standard library:–java.lang;–java.util,–java.net–A nesting/hierarchy of classes like the nested subdirectories on your harddrive.•Package names are hierarchical. java.awt.image.Model refers to the class Model inside the package image inside the package awt inside the package java.Using Packages•A class can use all classes from its own package and all public classes from other packages. •Two ways to access public classes from other packages:1. Using a fully qualified name:java.lang.System.out.println(“Here is an example”);2. Using the import statement to give you a shorthand way of referring to files in the packages. import java.util.ArrayList; import java.util.*; //now you can refer to everything //in .util•Every Java program implicitly imports java.lang.*, so you don't have to import it explicitly to use System.out.println().Naming Conflict•If you import multiple packages that contain classes with the same name, you’ll get an error:import java.util.*;import java.sql.*Date today; //ERROR•Two solutions:1. import java.util.*;import java.sql.*import java.util.date.*;2. java.util.Date birthday= new java.util.Date();Static Import• Java 5.0 introduced syntactic sugar to abbreviate calls to Static (class) methods such as System.out and Math.pow: –import static java.lang.System.out; means “within this file, you can use out as an abbreviation for System.out.– import static java.lang.System.*; means “within this file, you can use any static method in System without mentioning the package.Adding a Class to a Packagepackage cs61b.class.day8public class Employee { … }•If you don’t specify the package statement–The classes in that source belongs to the default package.–The default package has no package name. •If you specify the package statement–You must place files in a package into a subdirectory that matches the full package name. –Employee.class must appear in the directory cs61b/class/day8 (or cs61b\class\day8 on Windows).How does javac and java


View Full Document

Berkeley COMPSCI 61B - Lecture Notes

Documents in this Course
Lab

Lab

4 pages

Matrix

Matrix

3 pages

Numbers

Numbers

14 pages

Lectures

Lectures

12 pages

Project 1

Project 1

24 pages

Exam

Exam

8 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?