Presentation is loading. Please wait.

Presentation is loading. Please wait.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 1 Chapter 4 Defining Your Own Classes.

Similar presentations


Presentation on theme: "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 1 Chapter 4 Defining Your Own Classes."— Presentation transcript:

1 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 1 Chapter 4 Defining Your Own Classes

2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 2 Object-Oriented Terms Abstraction - high-level view Interface - protocol for communication Encapsulation - objects are self-contained Information hiding - protect object data Generality - use class for related tasks Extensibility - add capabilities through inheritance

3 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 3 Why Define Your Own Classes? Standard classes do not meet all of our needs. Classes need to be customized to applications. Classes are used to build large programs.

4 Two Extremes Static classes Only class data and methods You don't create instances (use new) Examples –Math –JOptionPane Instantiable classes Used for defining objects Each object has its own data –methods have access to object's data Examples –DecimalFormat –Date, DateFormat

5 Template for Driver Class

6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 6 main Method Template public static void main( String[ ] args ){ JFramemyWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle(“My First Java Program”); myWindow.setVisible(true); } Method Body Modifier Return Type Method Name Parameter

7 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 7 Generic Class Method Template public static ( ){ /* code for what method does */ return ; } Method Body Modifier Return Type Method Name Parameters

8 Parameter Lists Describe the data method needs Empty if the method doesn't need data public static double random() { /* … */ } Need type and variable name for each public static double sqrt( double x) { /*…*/ } Separate multiple parameters with commas public static double pow( double a, double b) { /* … */ }

9 Return Types Specifies the type of information returned by the method –can be any Java type - a primitive type or a class name A method can only return one value or object –last statement needs to return something with the appropriate type void means the method doesn't return anything –void method does not need a return statement

10 Example method Calculating x coordinate from polar coordinates public static double xCoordinate( double rValue, double thetaR) { return rValue * Math.cos( thetaR); } Calling (using) the method from the same class double x = xCoordinate( r, thetaRad); Calling the method from another class double x = Polar.xCoordinate( r, thetaRad);

11 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 11 Designing a Class What role does the class play in the program? –What data does it need? –What behavior is required? –What can other classes do with it? –What information does it keep to itself? –How do we want to create the object?

12 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 12 Instantiable Class: Bicycle For a program to register bicycles on campus Data –name of owner –what else? Behavior –assign a value to owner name –find out what the owner name is –Any others?

13 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 13 UML Class Diagram Three sections –Class name –Data members –Methods

14 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 14 Template for Class Definition

15 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 15 Data Members Data members (instance variables) –variables that are declared in the class body –store the data needed to describe a particular object –Instance variables are usually private Class data also declared in the body of the class –reserved word static indicates class data –constants are usually static use the modifier final to declare a constant

16 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 16 Syntax for Data Member Declaration ; private String ownerName ; Modifiers Data Type Name Any declaration needs a name and a type –Usually use private modifier for data member

17 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 17 Instance Methods Define the behavior of an object –Instance methods use the object's data –Qualify instance methods with the name of an object –Methods can be either public or private interface to the other classes defined by public methods Class methods are independent of any particular object –Qualify class methods with the name of the class –Access to instance data only through an object

18 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 18 Template for Instance Method ( ){ } public void setOwnerName ( String name ) { ownerName = name; } Statements Modifier Return Type Method Name Parameter

19 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 19 Parameters Parameters describe the information needed by the method –parameter is a placeholder for the information needed by the method The parameter list consists of zero or more parameter declarations –parameters are separated by commas –parameter declaration is a type followed by a name

20 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 20 Calling a method with Parameters When calling a method, provide a "value" for each parameter –An argument is a value we pass to a method. public class Account {... public void add(double amt) { balance = balance + amt; }... } public class Sample { public static void main(String[] arg) { Account acct = new Account();... acct.add(400);... }... } argument parameter

21 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 21 Calling Methods same number of arguments as parameters match arguments and parameters by position types must be assignment- compatible

22 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 22 Creating Objects Use new to instantiate (create) an object Bicycle bike = new Bicycle(); Bicycle() is a constructor –a special method used to create a new instance Every class has at least one constructor –default constructor has no parameters Constructor with parameters initializes data with user- supplied values

23 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 23 Constructor Template public ( ){ } public Bicycle( String name ) { ownerName = name; } Modifier Class Name Parameter Note: no return type

24 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 24 Visibility Modifiers The modifiers public and private designate the visibility of data members and methods. –private members not accessible to client classes –public members can be used by client classes Information hiding : internal details of class hidden from clients.

25 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 25 Accessibility Example ClientService … Service obj = new Service(); obj.memberOne = 10; obj.memberTwo = 20; obj.doOne(); obj.doTwo(); … public class Service { public int memberOne; private int memberTwo; public void doOne() { … } private void doTwo() { … }

26 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 26 Guideline for Visibility Modifiers Instance variables should be private. Methods are public if they are intended for use by client classes. Methods are private if used only by the other methods in the same class. Make class constants public to make them readable by the client programs.

27 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 27 UML Notation for Visibility public – plus symbol (+) private – minus symbol (-)

28 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 28 Types of variables An identifier appearing inside a method can be a local variable, a parameter, or a data member. Search order –local variable declaration or a parameter –data member declaration –no matching declaration causes an error

29 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 29 Local Variables Declared in body of method –used for storing intermediate results –not available outside method –parameters also local to the method

30 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 30 Sample Matching

31 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 31 Calling Methods of the Same Class When calling a method of another class –need to say what object/class it belongs to When calling a method from another method in the same class. –refer to a method with just its name

32 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 32 Passing Objects to a Method Method parameters can be objects or primitive values A primitive value passed to a method is passed by value –value is copied into corresponding parameter variable For an object, we pass the reference (location) of the object by value –objects are not copied –the method has access to the original object

33 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 33 Passing a Student Object

34 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 34 Sharing an Object Can pass the same Student object to card1 and card2 The owner member of both LibraryCard objects refers to the same Student object

35 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 35 Using a class We can create multiple Bicycle instances in any other class. Bicycle bike1, bike2; bike1 = new Bicycle( ); bike1.setOwnerName("Adam Smith"); bike2 = new Bicycle( ); bike2.setOwnerName("Ben Jones");

36 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 36 Using Bicycle BicycleRegistrationBicycle BicycleRegistration uses Bicycle –instances are created in the main method SecondMain uses two classes SecondMainBicycleAccount

37 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 37 High-Level Class Diagrams Large programs use many classes Librarian is a main class –main method creates Student and Library card objects LibraryCard has a Student Student has two Strings

38 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 38 Object-Oriented Design Divide and Conquer is one approach to solving large problems –Break the problem up into smaller tasks –Keep sub-dividing until each task becomes small enough to handle In object-oriented design, identify the objects in the problem –Look for nouns in the problem description –For each object identify data needed to describe the object operations needed to allow the object to do its job


Download ppt "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 1 Chapter 4 Defining Your Own Classes."

Similar presentations


Ads by Google