{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chapter5_2-Methods - Chapter 5 Classes and Objects in Depth...

Info icon This preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to methods Chapter 5: Classes and Objects in Depth
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 2 What are Methods Objects are entities of the real-world that interact with their environments by performing services on demand . Objects of the same class have: the same characteristics: store the same type of data . And the same behavior : provide the same services to their environment. Services that objects provide are called methods.
Image of page 2
Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 3 Why Methods Information hiding prevent the data an object stores from being directly accessed by outsiders. Encapsulation allows objects containing the appropriate operations that could be applied on the data they store. So, the data that an object stores would be accessed only through appropriate operations.
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 4 Method Declaration Method declaration is composed of: Method header. Method body <method header> { <method body> }
Image of page 4
Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 5 Method Declaration (cont.) public void setOwnerName ( String name ) { ownerName = name; } Method body Method body Modifier Modifier Return Type Return Type Method Name Method Name Parameters Parameters <modifiers> <return type> <method name> ( <parameters> ){ <method body> }
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 6 Method Header The modifiers represent the way the method is accessed. The return type indicates the type of value (if any) the method returns. If the method returns a value, the type of the value must be declared. Returned values can be used by the calling method. Any method can return at most one value. If the method returns nothing , the keyword void must be used as the return type . The parameters represent a list of variables whose values will be passed to the method for use by the method. They are optional. A method that does not accept parameters is declared with an empty set of parameters inside the parentheses. <modifiers> <return type> <method name> ( <parameters> ){ <method body> }
Image of page 6
Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 7 Types of methods There 3 different criteria defining types of methods: Modifiers: this criteria is also composed of 3 sub- criteria: Visibility: public or private (or protected in csc 113) Shared between all instances or not: class member (static) or instance method. Override able or not (final): to be discussed in CSC 113. Return type: method with or without (void) return value. Parameters: with or without parameters.
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 8 Example of Methods with No-Parameters and No-Return value import java.util.Scanner; public class Course { // Attributes private String studentName; private String courseCode ; private static Scanner input = new Scanner(System.in); //Class att.
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}