Object Oriented Programming - Lesson 3: Classes and Objects presents Class, Object, more on class, Enum types, Package and the class path, Documentation comments for class, Number and String. | Object-Oriented Programming Lecture 3: Classes and Objects Dr. Lê H!ng Ph"#ng -- Department of Mathematics, Mechanics and Informatics, VNUH July 2012 1 Tuesday, July 31, 12 Content ✦ Class ✦ Object ✦ More on class ✦ Enum types ✦ Package and the class path ✦ Documentation comments for class ✦ Number and String 2 Tuesday, July 31, 12 More on classes ✦ More aspects of classes that depend on using object references and the dot operator: ✦ Returning values from methods ✦ The this keyword ✦ Class vs. instance members ✦ Access control 3 Tuesday, July 31, 12 Returning a value from a method ✦ A method returns to the code that invoke it when it ✦ completes all the statement in the method, ✦ reaches a return statement, or ✦ throws an exception (covered later), ✦ Any method declared void does not return a value. It does not need to contain a return statement but we can use return; to break a control flow block and exit the method. ✦ Any method that is not declared void must contain a return statement with a corresponding return value: return value; 4 Tuesday, July 31, 12 Returning a value from a method ✦ The getArea method return a primitive type (int): public int getArea() { return width * height; } ✦ A method can also return a reference type. In a program to manipulate Bicycle objects, we might have a method like this: public Bicycle seeWhosFastest(Bicycle myBike, Bicycle yourBike, Environment env) { Bicycle fastest; // code to calculate which bike is // faster, given each bike's gear // and cadence and given the // environment (terrain and wind) return fastest; } 5 Tuesday, July 31, .