Presentation is loading. Please wait.

Presentation is loading. Please wait.

Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.

Similar presentations


Presentation on theme: "Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4."— Presentation transcript:

1 Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 4

2 Rina Zviel-Girshin @ARC2 Overview Defining classes Constructors Methods Modifiers Static methods and variables

3 Rina Zviel-Girshin @ARC3 What Applications are made of? Java application are made of Objects. Objects are constructed from Classes. A Class is made of another class from which it can inherit some interfaces, plus data fields and methods. Data fields and methods are constructed from Java statements. Interface is made of read-only data and empty methods.

4 Rina Zviel-Girshin @ARC4 Class A class defines data, behavior and implementation of a particular type of object. It holds design information. It has name. Class is used to create objects.

5 Rina Zviel-Girshin @ARC5 Defining classes Minimal syntax: class className { //class body } Class body consists of two: data methods

6 Rina Zviel-Girshin @ARC6 Defining classes Class Data is of two major kinds: Static – class data: The values are the same for the whole class. The values exist in the class even before any object of the class is instantiated. Nonstatic instance data: The data is different for each object. Memory is allocated for the data in each object.

7 Rina Zviel-Girshin @ARC7 Examples of class definitions The simplest class is an empty class which has nothing but the className. Lets call it Proletarian. class Proletarian { }

8 Rina Zviel-Girshin @ARC8 Examples of class definitions A more powerful class can have data but no methods. Lets call it Bourgeois (middle class in French). class Bourgeois { int money_in_the_bank; double bank_account_no; }

9 Rina Zviel-Girshin @ARC9 Examples of class definitions A class can have methods but no data. Lets call it AspiringBourgeois. class AspiringBourgeois { void make_money() { // do something }

10 Rina Zviel-Girshin @ARC10 Examples of class definitions A class usually will have both methods and data. Lets call it Capitalist. class Capitalist { int money_in_the_bank; double bank_account_no; void make_money() {money_in_the_bank*=2; }

11 Rina Zviel-Girshin @ARC11 Examples of class definitions A more realistic example – class Rectangle. Data: width height Services: Compute perimeter Computer area

12 Rina Zviel-Girshin @ARC12 Class declaration public class Rectangle { // data int width,height; // methods int perimeter() { return (width+height)*2;} int area() { return width*height;} }

13 Rina Zviel-Girshin @ARC13 Constructors Constructor is a special method which is invoked every time we create a new object. Constructor has no declared return type and cannot return anything. Constructor always used with new operator. Constructor must have exactly the same name as class. Constructors can have parameters.

14 Rina Zviel-Girshin @ARC14 Constructor example public class Rectangle { // data int width, height; // constructs a new rectangle, sets width and height to zero public Rectangle() { width=0; height=0; } // … other methods }

15 Rina Zviel-Girshin @ARC15 Constructor Overloading More than one constructor is allowed. All constructors have the same name. The difference is in the parameters: number of parameters types of parameters both When we construct a new object the compiler decides which constructor to invoke according to the type of actual parameters. This is called overloading.

16 Rina Zviel-Girshin @ARC16 Constructor Overloading public class Rectangle { int width,height; // constructors public Rectangle() { width=0; height=0; } public Rectangle(int width, int height) { this.width = width; this.height = height; } // … other methods }

17 Rina Zviel-Girshin @ARC17 this Keyword In last example we saw this keyword. It is a reference to the current object. Example: We can add another method to Rectangle class. public void set(int width, int height) { this.width = width; this.height = height; }

18 Rina Zviel-Girshin @ARC18 Default constructor The default constructor: –has no parameters –initializes all the instance variables to zero or null (depending on the type) When class does not have a constructor a default constructor is used. Default or zero arguments constructor just clears all instance variables (or sets specified default values).

19 Rina Zviel-Girshin @ARC19 Methods We manipulate data through methods. Methods are implemented as part of class definition. We interact with objects by method invocation. The invocation syntax is: objectName.methodName(Param_List);

20 Rina Zviel-Girshin @ARC20 Method declaration A method declaration specifies the code that gets executed when the method invokes. Syntax: [modifier] returnType MethodName (ParamList) { //methodBody } modifier and ParamList are optinal. returnType and MethodName are required.

21 Rina Zviel-Girshin @ARC21 Local data Data inside the method called local data. Local data is created when method invoked and destroyed when method returned. Garbage collector destroys local data automatically.

22 Rina Zviel-Girshin @ARC22 Method declaration example long factorial (int n) { long result=1; for(int k=n; k>1; k--) result*=k; return result; } Return type Method name Parameter list Return expression with return type variable Local data

23 Rina Zviel-Girshin @ARC23 Method modifier Method modifier is one of: public protected private static We will return to modifiers later.

24 Rina Zviel-Girshin @ARC24 Return Types The return type indicates the type of value that the method return to the client that invoked it. The void type is allowed. It means our method returns no value. The return keyword is used. A method can only return one value.

25 Rina Zviel-Girshin @ARC25 Example public class Rectangle { … int perimeter() { return (width+height)*2; } int area() { return width*height; }

26 Rina Zviel-Girshin @ARC26 Passing parameters The parameter passing mechanism used in Java called “Call-by-value”.This means that the value of a parameter is copied and a parameter variable initialized with the copy. So if any changes are done it have no effect on actual parameter. Objects are not passed as parameters, only references. If changes are made to a reference of an object it affects the original reference as well, because they refer to the same memory location.

27 Rina Zviel-Girshin @ARC27 Class modifiers Method modifier is one of: public protected private static Not all modifiers are applicable to all kinds of class declarations. A compile-time error occurs if the same modifier appears more than once in a class declaration.

28 Rina Zviel-Girshin @ARC28 Visibility modifier A modifier is a Java reserved word that specifies particular characteristics of a method or data value. We used the modifier final to to make a variable “read-only” (constant). Java has three visibility modifiers: public, private, and protected. We will discuss the protected modifier later.

29 Rina Zviel-Girshin @ARC29 Class visibility modifiers Class can only have default or public visibility. Default visibility means that no modifier was used. This visibility called package visibility that means it is visible in its package. Public visibility means that it is visible anywhere. It can be used by any other class. Java predefined classes are public.

30 Rina Zviel-Girshin @ARC30 Another classes Another classes can use My public class My {... } Public visibility

31 Rina Zviel-Girshin @ARC31 Methods visibility modifiers Members of a class that are public accessible from any class that can access the member class. Members of a class that are private accessible from inside the member class (useful for information hiding). We will return to the protected visibility later. Members declared without a visibility modifier have default visibility and can be accessed by any class in the same package.

32 Rina Zviel-Girshin @ARC32 Private visibility Most restrictive. Can only be seen within the class. Is not inherited or seen by sub-classes. Most attributes should be private. Good practice is to provide routines to access them. (If clients are to be allowed to "see" the value). Syntax for variable ‘Name' accessor is getName()

33 Rina Zviel-Girshin @ARC33 Example class Point { // Declare instance variables private double x = 0.0; private double y = 0.0;... Constructors etc.... // Accessors public double getX() { return x; } public double getY(){ return y; } }

34 Rina Zviel-Girshin @ARC34 Method parameters Parameter list is optional in method signature. Parameter list can be empty. Each parameter in list defined by its type and name.

35 Rina Zviel-Girshin @ARC35 Method overloading Method overloading is a process of using the same method name for multiple methods. Methods should have different: –number –type –order of parameters. The compiler must be able to determine which version of the method is being invoked by analyzing the parameters.

36 Rina Zviel-Girshin @ARC36 Method overloading Example void add(int x, int y); void add(double a, double b); add(3,4); // will use (int,int) add (3.5,5.2); // will use (double,double)

37 Rina Zviel-Girshin @ARC37 Method overloading Advantages: Opens up namespace Means shorter names An overloaded constructor provides multiple ways to set up a new object Disadvantages: Can accidentally invoke wrong version as a result of human error (leaving off parameter, forgetting a cast)

38 Rina Zviel-Girshin @ARC38 Class variables and methods The default view is that all the data and methods reside in objects of the class. The class is only a blueprint used as object definition. In the balance of importance the objects are important and the class has only secondary function. This view is not totally correct. There are situations in which data and/or methods are residing in the class itself.

39 Rina Zviel-Girshin @ARC39 Class variables and methods Those are the traits of the class and not of its instances. Those variables/methods are called static and have the following features: only one exists for the whole class the value is one for all instances the variables/methods exist before any objects of the class are instantiated All static data/methods are marked with a static modifier.

40 Rina Zviel-Girshin @ARC40 Class variables and methods The basic idea: one per class instead one per instance. Syntax: static type variable_name; static type function_name(); Examples static int x; static void f(); static public void main(String[] args);

41 Rina Zviel-Girshin @ARC41 Static methods Java provides a mechanism for the user to indicate whether a method belongs to individual objects or the entire class. Static method belongs to the entire class. Amongst the reasons a method might be more suitable to be a class method: - Method reflects a global operation of the class - Method doesn't employ any object attributes - Method is a "helper" method

42 Rina Zviel-Girshin @ARC42 Example class Trig { static float area ( float r ) { return Math.PI * r * r;} …. float A = Trig.area(10); … System.out class has a static method called println.

43 Rina Zviel-Girshin @ARC43 Static Variables By default static variables are initialized to 0 or null. Syntax: static type Name; Frequent usage of a static variable is as a counter holding the number of objects of the class. Simple example class Visitor {static int number_of_visitors; public Visitor() {number_of_visitors++;} }

44 Rina Zviel-Girshin @ARC44 Static Variables Let’s follow the changes in number_of_visitors variable as we instantiate objects of the Visitor class. class Theater {… Visitor avi = new Visitor(); … Visitor beni = new Visitor(); … } number_of_visitors=0 number_of_visitors=1 number_of_visitors=2

45 Rina Zviel-Girshin @ARC45 Static Methods Static method can access only static variables and other static methods. The reference “this” can’t be used as the method is not in any object. The access to static method is not as usually – through an object, but through the class name itself. Examples float i = Math.random(); String s = String.valueOf(i);

46 Rina Zviel-Girshin @ARC46 Instance Variables and Methods (revisited) The default non-static variables/methods have the following features: different one exists for each instance the value is independent for all instances the variables/methods do not exist before the instance is instantiated an independent copy exists in each instance

47 Rina Zviel-Girshin @ARC47 Any Questions?


Download ppt "Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4."

Similar presentations


Ads by Google