Object Oriented programming Chapter 8 Objects and Classes

  • Slides: 37
Download presentation
Object Oriented programming Chapter 8 Objects and Classes Instructors: Dr. Rasha Orban Dr. Neven

Object Oriented programming Chapter 8 Objects and Classes Instructors: Dr. Rasha Orban Dr. Neven Abdelaziz

2 Motivations After learning the preceding chapters, you are capable of solving many programming

2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections, loops, methods, and arrays. However, these Java features are not sufficient for developing graphical user interfaces and large scale software systems. Suppose you want to develop a graphical user interface as shown below. How do you program it?

3 OOP Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an

3 OOP Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world. For example, a student, a desk, a circle, a button can all be viewed as objects. An object has a unique identity, state, and behaviors. The state of an object consists of a set of data fields (also known as properties) with their current values. The behavior of an object is defined by a set of methods.

4 Objects An object has both a state and behavior. The state defines the

4 Objects An object has both a state and behavior. The state defines the object, and the behavior defines what the object does.

5 Objects of the same type are defined using a common class. A class

5 Objects of the same type are defined using a common class. A class is a template, blueprint, or contract that defines what an object’s data fields and methods will be. An object is an instance of a class. You can create many instances of a class. The terms object and instance are often interchangeable.

6 Classes are constructs that define objects of the same type. A Java class

6 Classes are constructs that define objects of the same type. A Java class uses variables to define data fields and methods to define behaviors. Additionally, a class provides a special type of methods, known as constructors, which are invoked to construct objects from the class.

7 Classes

7 Classes

8 Constructors are a special kind of methods that are invoked to construct objects.

8 Constructors are a special kind of methods that are invoked to construct objects. Circle() { } Circle(double new. Radius) { radius = new. Radius; }

9 Constructors, cont. A constructor with no parameters is referred to as a no-arg

9 Constructors, cont. A constructor with no parameters is referred to as a no-arg constructor. · Constructors must have the same name as the class itself. · Constructors do not have a return type—not even void. · Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects.

Creating Objects Using Constructors new Class. Name(); Example: new Circle(); new Circle(5. 0); 10

Creating Objects Using Constructors new Class. Name(); Example: new Circle(); new Circle(5. 0); 10

11 Default Constructor A class may be declared without constructors. In this case, a

11 Default Constructor A class may be declared without constructors. In this case, a no-arg constructor with an empty body is implicitly declared in the class. This constructor, called a default constructor, is provided automatically only if no constructors are explicitly declared in the class.

12 Declaring Object Reference Variables To reference an object, assign the object to a

12 Declaring Object Reference Variables To reference an object, assign the object to a reference variable. To declare a reference variable, use the syntax: Class. Name object. Ref. Var; Example: Circle my. Circle;

13 Declaring/Creating Objects in a Single Step Class. Name object. Ref. Var = new

13 Declaring/Creating Objects in a Single Step Class. Name object. Ref. Var = new Class. Name(); Example: Assign object reference Create an object Circle my. Circle = new Circle();

14 Accessing Objects • Referencing the object’s data: object. Ref. Var. data e. g.

14 Accessing Objects • Referencing the object’s data: object. Ref. Var. data e. g. , my. Circle. radius • Invoking the object’s method: object. Ref. Var. method. Name(arguments) e. g. , my. Circle. get. Area()

15 Reference Data Fields The data fields can be of reference types. The following

15 Reference Data Fields The data fields can be of reference types. The following Student class contains a data field name of the String type. public class Student { String name; // has default value NULL int age; // has default value 0 boolean science; // has default value false char gender; } // has default value 'u 0000'

Default Value for a Data Field 16 The default value of a data field

Default Value for a Data Field 16 The default value of a data field is NULL for a reference type, 0 for a numeric type, false for a boolean type, and 'u 0000' for a char type. However, Java assigns no default value to a local variable inside a method. public class Test { public static void main(String[] args) { Student student = new Student(); System. out. println("name? " + student. name); System. out. println("age? " + student. age); System. out. println(“sience? " + student. science); System. out. println("gender? " + student. gender); } }

17 Example Java assigns no default value to a local variable inside a method.

17 Example Java assigns no default value to a local variable inside a method. public class Test { public static void main(String[] args) { int x; // x has no default value String y; // y has no default value System. out. println("x is " + x); System. out. println("y is " + y); } } Compilation error: variables not initialized

Differences between Variables of Primitive Data Types and Object Types Every variable represents a

Differences between Variables of Primitive Data Types and Object Types Every variable represents a memory location that holds a value. When you declare a variable, you are telling the compiler what type of value the variable can hold. For a variable of a primitive type, the value is of the primitive type. For a variable of a reference type, the value is a reference to where an object is located. 18

19 Copying Variables of Primitive Data Types and Object Types

19 Copying Variables of Primitive Data Types and Object Types

20 Static Variables, Constants, and Methods The data field radius in the Circle class

20 Static Variables, Constants, and Methods The data field radius in the Circle class in Listing 8. 1 is known as an instance variable. An instance variable is tied to a specific instance of the class; it is not shared among objects of the same class. For example, suppose that you create the following objects: Circle circle 1 = new Circle(); Circle circle 2 = new Circle(5);

21 Classes

21 Classes

22 The radius in circle 1 is independent of the radius in circle 2

22 The radius in circle 1 is independent of the radius in circle 2 and is stored in a different memory location. Changes made to circle 1’s radius do not affect circle 2’s radius, and vice versa.

23 If you want all the instances of a class to share data, use

23 If you want all the instances of a class to share data, use static variables, also known as class variables. Static variables store values for the variables in a common memory location. Because of this common location, if one object changes the value of a static variable, all objects of the same class are affected. Java supports static methods as well as static variables. Static methods can be called without creating an instance of the class.

24 To declare a static variable or define a static method, put the modifier

24 To declare a static variable or define a static method, put the modifier static in the variable or method declaration. The static variable number. Of. Objects and the static method get. Number. Of. Objects() can be declared as follows: static int number. Of. Objects = 0; static int get. Number. Objects() { return number. Of. Objects;

25

25

26 1 public class Foo { 2 int i = 5; 3 static int

26 1 public class Foo { 2 int i = 5; 3 static int k = 2; 5 public static void main(String[] args) { 6 int j = i; // Wrong because i is an instance variable 7 m 1(); // Wrong because m 1() is an instance method 8 } 10 public void m 1() { 11 // Correct since instance and static variables and methods 12 // can be used in an instance method 13 i = i + k + m 2(i, k); 14 } 16 public static int m 2(int i, int j) { 17 return (int)(Math. pow(i, j)); 18 }

27 Visibility Modifiers You can use the public visibility modifier for classes, methods, and

27 Visibility Modifiers You can use the public visibility modifier for classes, methods, and data fields to denote that they can be accessed from any other classes. If no visibility modifier is used, then by default the classes, methods, and data fields are accessible by any class in the same package. This is known as packageprivate or package-access.

28 In addition to the public and default visibility modifiers, Java provides the private

28 In addition to the public and default visibility modifiers, Java provides the private and protected modifiers for class members.

29 The private modifier makes methods and data fields accessible only from within its

29 The private modifier makes methods and data fields accessible only from within its own class. Figure illustrates how a public, default, and private data field or method in class C 1 can be accessed from a class C 2 in the same package and from a class C 3 in a different package.

30

30

31

31

32 Data Field Encapsulation To prevent direct modifications of data fields, you should declare

32 Data Field Encapsulation To prevent direct modifications of data fields, you should declare the data fields private, using the private modifier. This is known as data field encapsulation.

33 A private data field cannot be accessed by an object from outside the

33 A private data field cannot be accessed by an object from outside the class that defines the private field. But often a client needs to retrieve and modify a data field. To make a private data field accessible, provide a get method to return its value. To enable a private data field to be updated, provide a set method to set a new value.

34 NOTE An object cannot access its private members, as shown in (b). It

34 NOTE An object cannot access its private members, as shown in (b). It is OK, however, if the object is declared in its own class, as shown in (a).

Visibility Modifiers and Accessor/Mutator Methods 35 By default, the class, variable, or method can

Visibility Modifiers and Accessor/Mutator Methods 35 By default, the class, variable, or method can be accessed by any class in the same package. F public The class, data, or method is visible to any class in any package. F private The data or methods can be accessed only by the declaring class. The get and set methods are used to read and modify private properties.

36 Assignment (1): Review Questions 8. 4, 8. 5, 8. 6, 8. 15, 8.

36 Assignment (1): Review Questions 8. 4, 8. 5, 8. 6, 8. 15, 8. 18, 8. 20 Programming Exercises 8. 6

Thanks for Attention

Thanks for Attention