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 Design Abstraction allows a programmer to concentrate on the important features of an object Interfaces define how classes interact Encapsulation makes objects self-contained Hiding information protects an objects data from meddling Generality allows a class to be used for several related tasks Extensibility allows the re-use of a class 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? Using just the standard classes will not meet all of our needs. We need to be able to define our own classes customized for our applications. Learning how to define our own classes is the first step toward mastering the skills necessary in building large programs.

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

5 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 5 Example: Bicycle Class A class to be used in a program to register bicycles on campus Data – name of owner –what else? Methods –need to be able to give a value to owner name –need to be able to find out what the owner name is –Any others?

6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 6 Class Diagram for Bicycle

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

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

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

10 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 10 Syntax for Data Member Declaration ; private String ownerName ; Modifiers Data Type Name Declaration needs a name and a type –Usually use modifier private for data members –We'll see other modifiers later

11 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 11 Instance Methods 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 –The interface a class presents to the outside world is defined by its public methods Class methods are independent of any particular object –Qualify class methods with the name of the class

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

13 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 13 Parameters Parameters describe the information needed by the method –Think of a parameter as a placeholder for the information that needs to be sent to the method when it is called. The parameter list consists of zero or more parameter declarations –parameters are separated by commas A parameter declaration consists of a type followed by a name

14 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 14 Calling a method with Parameters When we call a method that has parameters, we need to 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

15 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 15 Calling Methods The number or arguments and the parameters must be the same Arguments and parameters are paired left to right The matched pair must be assignment- compatible (e.g. you cannot pass a double argument to a int parameter)

16 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 16 Creating Objects We use new to create an object Bicycle bike = new Bicycle(); Bicycle() is an example of a constructor –A constructor is a special method that is executed when a new instance of the class is created. –A constructor has no return type –A constructor can only be called with new Every class has at least one constructor –The default constructor has no parameters It is useful to be able to provide an object's data when it is created –Define a constructor with parameters to do this

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

18 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 18 Programs with Multiple classes BicycleRegistrationBicycle BicycleRegistration uses Bicycle –instances are created in the main method Once a class is defined, it can be used in more than one program SecondMainBicycleAccount

19 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 19 Information Hiding and Visibility Modifiers The modifiers public and private designate the accessibility of data members and methods. If a class component (data member or method) is declared private, client classes cannot access it. If a class component is declared public, client classes can access it. Internal details of a class are declared private and hidden from the clients. This is information hiding.

20 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 20 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() { … }

21 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 21 Guideline for Visibility Modifiers Guidelines in determining the visibility of data members and methods: –Declare the class and instance variables private. –Declare the class and instance methods private if they are used only by the other methods in the same class. –Declare the class constants public if you want to make their values directly readable by the client programs. If the class constants are used for internal purposes only, then declare them private.

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

23 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 23 Object Diagrams Once the Bicycle class is defined, we can create multiple instances. Bicycle bike1, bike2; bike1 = new Bicycle( ); bike1.setOwnerName("Adam Smith"); bike2 = new Bicycle( ); bike2.setOwnerName("Ben Jones");

24 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 24 Local Variables Local variables are variables declared in the body of a method –They are used for storing intermediate results that don't need to be saved.

25 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 25 Local, Parameter & Data Member An identifier appearing inside a method can be a local variable, a parameter, or a data member. The rules are –If there’s a matching local variable declaration or a parameter, then the identifier refers to the local variable or the parameter. –Otherwise, if there’s a matching data member declaration, then the identifier refers to the data member. –Otherwise, it is an error because there’s no matching declaration.

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

27 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 27 Calling Methods of the Same Class So far, we have been calling a method of another class (object). It is possible to call method of a class from another method of the same class. –in this case, we simply refer to a method without dot notation

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

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

30 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 30 Sharing an Object We pass the same Student object to card1 and card2 Since we are actually passing a reference to the same object, it results in the owner of two LibraryCard objects pointing to the same Student object

31 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 31 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