CS100J, Fall 2001 Tues, 10/16 Lecture 13 ------------------------------------------------------------------------------- Announcements: + reading: Strings&Chars on Thurs: pp57-58,66-67,79-89,270-278,320-322 $for$ on Thurs and beyond: 167-175 arrays on Thurs-next week: 6.1-6.3, 6.5 (6.4 happens the following week) + T1 regrades due Mon 10/22 (see 10/3 Announcements) + P3 delay: Mon sections-->Carpenter, Tues-->in section + P4 due Thurs 10/18 + T2 on 10/23 ------------------------------------------------------------------------------- Topics: + more AI cases (P3)...if you do not understand the policy, see DIS! + references (a bit more) + encapsulation + static + method overloading + "constructor overloading" ------------------------------------------------------------------------------- Summary: + classes contain members + want to refine classes to make them more useful/easier to write/maintain ------------------------------------------------------------------------------- Information hiding (Sections 4.2, 5.6): + process of abstracting details + split class into accessible and inaccessible portions + prevents objects from improperly using data, which causes program crash + ex) think of CLIENTS (like people) and SERVERS (like a bank) +-----------+ | bank | B | (savings) | +-----------+ A ------------------------------------------------------------------------------- Encapsulation: + use has-a relationships to form class and information hiding for abstraction + "worry" about higher levels of reasoning and not "nitty-gritty" details + how? modify members with $public$, blank, $protected$, $private$ (skip $protected$ for now; $static$ and $final$ are related, too) + effects on software development: security, reliability of code, ease of reuse ------------------------------------------------------------------------------- Encapsulation Modifiers: + $private$: prevents OUTSIDE class from using $.$ operator to access member + $public$: allow any class to $.$ operator to access member + : effectively public (slightly more restrictive: classes outside of PACKAGE cannot access a member with $.$) + syntax: modifier member (can use ONLY ONE modifier) ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- Related Modifiers + $static$: all classes share the SAME member, which may be accessed without using a reference (more after public/private) + may use with any $public$, $private$, or ex) class MyMath { public _________ int myRandom(int low, int high) { return (int) (Math.random()*(high-low+1)) + low; } } + $final$: field: cannot change the field value local variable: cannot change the local variable value methods and classes? Yes, but inheritance involved can use with any other modifier ------------------------------------------------------------------------------- Field Style: + use $private$ for fields that are unique for each object + use $public static$ for fields that are shared for every object (typically, include $final$ also) + use $private static$ for times when every method is $static$, but you do not want outsiders to access field values ------------------------------------------------------------------------------- Method Style: + methods used ONLY in a class are called UTILITY/HELPER/SUPPORT METHODS and should be $private$ (do not need to be shared outside!) + methods accessed by outside class are called SERVICE METHODS - some methods SET VALUES (setters/mutators) - some methods GET VALUES (getters/accessors) + static vs nonstatic? - depends on whether or not you need to access the method without an object see area.java example ------------------------------------------------------------------------------- $static$: + modifier that allows access to member without using a reference to an object + may still access a member through a reference + does NOT mean $final$ and does not override $private$! + syntax: static modifier(s) member // example: class Student { private String name; ___________ ___________ int count; // count of students public static int currentYear; public static final int GRADYEAR = 2005; public Student(String name) { this.name=name; ______________ ; // increment count of students } public static int getCount() { return count; } } public class Course { public static void main(String[] args) { System.out.println(_______________________); // output gradyear Student s1 = new Student("Dani"); Student s2 = new Student("Shagrath"); Student.currentYear = _____ ; // set current year System.out.println( ______________________); // output current year System.out.println(_______________________); // output student count } } + NOTE: why is main static? _______________________________________________ ------------------------------------------------------------------------------- Method overloading: + A method may have the same name as another method if you change order of arguments, types of arguments, number of arguments, or any combination of these. + Note: The following do NOT constitute method overloading: - Changing *just* the return type - Changing *just* the names of the formal parameters ------------------------------------------------------------------------------- Constructor overloading: + no such term, but makes sense if you think of constructor as a method! + follows same rules as method overloading + Big difference: when you write a constructor, Java removes the default (or empty) constructor. ------------------------------------------------------------------------------- class Square { private int x; public Square() { } public Square(int x) { this.x=x; } public Square(int x, int y) { if (x!=y) System.out.println("Error!Using 1st input"); this.x = x; } public int getPerim() { return 4*x; } // public void getPerim() { } // won't work! public void getPerim(String s) { System.out.println(s+getPerim()); } } public class squares { public static void main(String[] args) { Square s1 = new Square(); Square s2 = new Square(1); Square s3 = new Square(2,1); System.out.println(s2.getPerim()); s3.getPerim("The perimeter is "); } } /* output: Error! Using first input. 4 The perimeter is 8 */ -------------------------------------------------------------------------------