Classes and Objects Object vs Class A class

Classes and Objects

Object vs. Class �A class could be considered as a set of objects having the same characteristics and behavior. � An object is an instance of a class. Page 2 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Declaring a Class with Java Class. Name - att 1: data. Type 1 -… - atti: data. Typei + m 1(…): data. Type 1 +. . . + mj(…): data. Typej Attributes Methods (Services) public class Class. Name { // Attributes // Methods (services) } Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 3

Declaring Attributes With Java <modifiers> Modifiers public Page 4 <data type> <attribute name> ; Data Type String Name student. Name ; Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Example of a Class Declaration with Java public class Course { // Attributes public String student. Name; public String course. Code ; // No method Members } Page 5 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Object Creation A. The instance variable is allocated in memory. crs Object: Course B. The object is A created course. Code B Course crs; C. The reference of the crs = student. Name new Course ( ) ; object created in B is assigned to the variable. C crs Object: Course student. Name course. Code Page 6 State of Memory Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Object Creation A. The instance variable is allocated in memory. crs Object: Course B. The object is A created course. Code B Course crs; C. The reference of the crs = student. Name new Course ( ) ; object created in B is assigned to the variable. C crs Object: Course student. Name course. Code Page 7 State of Memory Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Instance VS. Primitive Variables �Primitive variables hold values of primitive data types. �Instance variables hold references of objects: the location (memory address) of objects in memory. Page 8 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Assigning Objects’ References to the same Instance Variable crs Course A Course crs; B crs = new Course ( ); A. The variable is allocated in memory. B. The reference to the new object is assigned to crs = new Course ( ); C. The reference to C Code Page 9 another object overwrites the reference in crs. State of Memory Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Assigning an Object Reference From One Variable to Another crs 1 crs 2 A Course crs 1, crs 2, Course B A. Variables are allocated in memory. crs 1 = new Course( ); crs 2 B. The reference to the new = crs 1; object is assigned to crs 1. C C. The reference in crs 1 is assigned to crs 2. Code Page 10 State of Memory Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Assigning an Object Reference From One Variable to Another crs 1 A. Variables are allocated in memory. A. Variables are assigned references of objects. A Course crs 1, crs 2, B crs 1 = new Course( ); crs 2 = new Course( ); crs 1 is assigned to crs 1. = crs 2; crs 2 Dr. S. GANNOUNI & Dr. A. TOUIR crs 1 Course C. The reference in crs 2 C Page 11 crs 2 Course crs 1 Course Introduction to OOP Course

Accessing Instance Attributes � In order to access attributes of a given object: � use the dot (. ) operator with the object reference (instance variable) to have access to attributes’ values of a specific object. instance. Variable. Name. attribute. Name course 1. Student. Name= “Sara Al. Kebir“; course 2. Student. Name= “Maha Al. Saad“; course 2 course 1 Object: Course student. Name Fahd Al. Amri course. Code Introduction to OOP Object: Course student. Name course. Code Dr. S. GANNOUNI & Dr. A. TOUIR Page 12 Majed Al. Kebir

class Course { // Instance attributes public String student. Name; public String course. Code ; } public class Course. Registration { public static void main(String[] args) { Course course 1, course 2; //Create and assign values to course 1 = new Course( ); course 1. course. Code= new String(“CSC 112“); course 1. student. Name= new String(“Sara Al. Kebir“); //Create and assign values to course 2 = new Course( ); course 2. course. Code= new String(“CSC 107“); course 2. student. Name= new String(“Maha Al. Saad“); System. out. println(course 1. student. Name + " has the course “+ course 1. course. Code); System. out. println(course 2. student. Name + " has the course “+ course 2. course. Code); } } Page 13 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Practical hint � Class � It Course will not execute by itself does not have method main � Course. Registration uses the class Course. � Course. Registration, which has method main, creates instances of the class Course and uses them. Page 14 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


output

Modifiers • Static Public+ Private - Protected #

Static �It used to define class attributes and methods. �Class attributes (and methods): � live in the class � can also be manipulated without creating an instance of the class. � are shared by all objects of the class. � do not belong to objects’ states. <modifiers> Modifiers public static Page 18 <data type> <attribute name> ; Data Type int Name student. Number ; Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Class Attributes Access � Class attributes (and methods) can also be manipulated without creating an instance of the class. <class name>. <attribute name> Class Name Attribute Name Course. student. Number = 0 ; Page 19 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

class Course { // attributes public String student. Name; public String course. Code ; public static int student. Number; } public class Course. Registration { public static void main(String[] args) { Course course 1, course 2; //Create and assign values to course 1 = new Course( ); Course. student. Number = 1; course 1. course. Code= new String(“CT 1513“); course 1. student. Name= new String(“Sara Al. Kebir“); //Create and assign values to course 2 = new Course( ); Course. student. Number ++; course 2. course. Code= new String(“CSC 107“); course 2. student. Name= new String(“Maha Al. Saad “); System. out. println(course 1. student. Name + " has the course “+ course 1. course. Code + “ ” + course 1. student. Number); System. out. println(course 2. student. Name + " has the course “+ course 2. course. Code + “ ” + course 2. student. Number); } } Page 20 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

output

public and private modifiers �Let’s consider a class X. �Let’s consider Y a client class of X. �Y is a class that uses X. �Attributes (and methods) of X declared with the public modifier are accessible from instances of Y. � The public modifier does not guarantee the information hiding. �Attributes (and methods) of X declared with the private modifier are not accessible from instances of Y. � The 22 private modifier guarantee the information hiding.

Accessibility from Inside (the Instance itself) object: X public private All members of an instance are accessible from the instance itself. 23 - Accessible - Inaccessible

Accessibility from an Instance of another Class object: X Accessibility from The Client class. : Y(client) Only public members Are visible from outside. All else is hidden from Outside. 24 public private - Accessible - Inaccessible

UML Representation of a Class (UML Class Diagram) �UML uses three symbols to represent the visibility of the class’ members. �+ : mentions that the member is public. � - : mentions that the member is private. Class. Name - att 1: data. Type 1 -… - atti: data. Typei + m 1(…): data. Type 1 +. . . + mj(…): data. Typej Attributes Methods (Services) 26

Declaring Private Attributes <modifiers> 27 <data type> <attribute name> ; Modifiers Data Type private String Name student. Name ;

Example of a Class with Private attributes Class. Name - student. Name: String - course. Code: String public class Course { // Attributes private String student. Name; private String course. Code ; // No method Members } 28

class Course { // Data Member private String student. Name; private String course. Code ; } public class Course. Registration { public static void main(String[] args) { Course course 1, course 2; //Create and assign values to course 1 = new Course( ); course 1. course. Code= “Ct 1513“; course 1. student. Name= “Sara Al. Kebir“; //Create and assign values to course 2 = new Course( ); course 2. course. Code= “CSC 107“; course 2. student. Name= “Maha Al. Saad“; System. out. println(course 1. student. Name + " has the course “+ course 1. course. Code); System. out. println(course 2. student. Name + " has the course “+ course 2. course. Code); } } 29

Accessibility Example … Service obj = new Service(); class Service { public int member. One; private int member. Two; public void do. One() { obj. member. One = 10; … obj. member. Two = 20; } private void do. Two() { obj. do. One(); … obj. do. Two(); } } … Client 30 Service

Methods

Method Declaration � Method declaration � Method header. � Method body <method header> { <method body> } 32 is composed of:

Method Declaration (cont. ) <modifiers> <return type> <method name> ( <parameters> <method body> } Modifier public Return Type void Method Name set. Owner. Name owner. Name = name; } 33 Parameters ( String name ) { Method body ){

Example of Methods with No-Parameters and No-Return value import java. util. Scanner; public class Course { // Attributes private String student. Name; private String course. Code ; private static Scanner input = new Scanner(System. in); //Class att. // Methods public void enter. Data. From. Key. Board() { System. out. print (“Enter the student name: ”); student. Name = input. next(); System. out. print (“Enter the course code: ”); course. Code = input. next(); } public void display. Data() { System. out. println (“The student name is: ” + student. Name); System. out. println (“The the course code is: ”+ course. Code); } } 34

Method Invocation � Invoking a method of a given object requires using: � the instance variable that refers to this object. � the dot (. ) operator as following: instance. Variable. method. Name(arguments) public class Course. Registration { public static void main(String[] args) { Course course 1, course 2; //Create and assign values to course 1 = new Course( ); course 1. enter. Data. From. Key. Board(); course 1. display(); //Create and assign values to course 2 = new Course( ); course 2. enter. Data. From. Key. Board(); course 2. display(); } } 35
![Method Invocation Execution Schema class Client { class X { public static void main(String[] Method Invocation Execution Schema class Client { class X { public static void main(String[]](http://slidetodoc.com/presentation_image_h2/04f8ce006f7cb74a35135a948e5b7578/image-35.jpg)
Method Invocation Execution Schema class Client { class X { public static void main(String[] arg) { } . . . X obj = new X(); // Block statement 1 public void method() { obj. method(); // Block statement 2 } // Method body . . . } The client Block statement 1 executes The method Invocation Block statement 2 starts The client 36 Passing Parameters if exist Return result if any The method body starts The method body finishes

Example of a Method with Return value public class Student { // Attributes private String student. Name; private int mid. Term 1, mid. Term 2, lab, final ; // Methods public int compute. Total. Marks() { int value = mid 1 + mid 2 + lab + final; } return value; } public class Test. Student { public static void main (String [] args) { Student st = new Student(); int total; … } 37 total = st. compute. Total. Marks(); System. out. println(total); }

Template for Methods with Return value public class Class. Name { // Attributes. . . // Methods. . . public return. Type method. Name(…) { return. Type variable. Name; // 1 - calculate the value to return // 2 - assign the value to variable. Name return variable. Name; } } public class Client. Class { public static void main (String [] args) { Class. Name instance. Variable = new Class. Name(); return. Type receiving. Varaiable; . . . receiving. Varaiable = instance. Variable. method. Name(…); } 38 . . . }

Arguments and Parameters � An argument is a value we pass to a method. � A parameter is a placeholder in the called method to hold the value of the passed argument. class Sample { public static void main(String[] arg) { } 39 . . . Account acct = new Account(); . . . public void add(double amt) { acct. add(400); . . . } balance = balance + amt; . . . } class Account { Formal parameter Argument (Actual Parameter) }

Programming Example: Largest Number � Input: set of 10 numbers � Output: largest of 10 numbers � Solution � Get numbers one at a time � Method largest number: returns the larger of two numbers � For loop: calls method largest number on each number received and compares to current largest number 40 Java Programming: From Problem Analysis to Program Design, 4 e

Solution: Largest Number public static double larger (double x, double y) { if (x >= y) return x; else return y; } public static void main(String[] args) { double num; double max; int count; System. out. println("Enter 10 numbers. "); num = console. next. Double(); max = num; for (count = 1; count < 10; count++) { num = console. next. Double(); max = larger(max, num); } System. out. println("The largest number is " + max); Java Programming: From Problem Analysis to Program Design, 4 e 41

Sample Run: Largest Number • Sample Run Enter 10 numbers: 10. 5 56. 34 73. 3 42 22 67 88. 55 26 62 11 The largest number is 88. 55 Java Programming: From Problem Analysis to Program Design, 4 e 42

Getter, Setter and Constructor Information Hiding 43

How Private Attributes could be Accessed � Private attributes are accessible from outside using accessor operations. � Getters � Setters 44

class Course { // Data Member private String student. Name; private String course. Code ; } public class Course. Registration { public static void main(String[] args) { Course course 1, course 2; //Create and assign values to course 1 = new Course( ); course 1. course. Code= “CT 1513“; course 1. student. Name= “Sara Al. Kebir“; //Create and assign values to course 2 = new Course( ); course 2. course. Code= “CT 1413“; course 2. student. Name= “Maha Al. Saad“; System. out. println(course 1. student. Name + " has the course “+ course 1. course. Code); System. out. println(course 2. student. Name + " has the course “+ course 2. course. Code); } } 45

Template for Getters public class Class. Name { private data. Type 1 attribute 1; . . . private data. Typen attributen; . . . public data. Type 1 get. Attribute 1() { } return attribute 1; . . . public data. Typen get. Attributen() { return attributen; }. . . } 46

Template for Setters public class Class. Name { private data. Type 1 attribute 1; . . . private data. Typen attributen; . . . public void set. Attribute 1(data. Type 1 param){ } attribute 1 = param; . . . public void set. Attributen(data. Typen param) { attributen = param; }. . . } 47

public class Course { // Attributes private String student. Name; private String course. Code ; . . . public String get. Student. Name() { return student. Name; } public String get. Course. Code() { return course. Code; }. . . public void set. Student. Name(String val) { student. Name = val; } public void set. Course. Code(String val) { course. Code = val; } } 48
![public class Course. Registration { public static void main(String[] args) { Course course 1, public class Course. Registration { public static void main(String[] args) { Course course 1,](http://slidetodoc.com/presentation_image_h2/04f8ce006f7cb74a35135a948e5b7578/image-48.jpg)
public class Course. Registration { public static void main(String[] args) { Course course 1, course 2; //Create and assign values to course 1 = new Course( ); course 1. set. Course. Code(“CT 1513“); course 1. set. Student. Name(“Sara Al. Kebir“); //Create and assign values to course 2 = new Course( ); course 2. set. Course. Code(“CT 1413“); course 2. set. Student. Name(“Maha Al. Saad“); System. out. println(course 1. get. Student. Name() + " has the course “ + course 1. get. Course. Code()); System. out. println(course 2. get. Student. Name() + " has the course “ + course 2. get. Course. Code()); } } 49

Class Constructors �A class is a blueprint or prototype from which objects of the same type are created. � Constructors define the initial states of objects when they are created. � Class. Name �A x = new Class. Name(); class contains at least one constructor. � A class may contain more than one constructor. 50

The Default Class Constructor � If no constructors are defined in the class, the default constructor is added by the compiler at compile time. � The default constructor does not accept parameters and creates objects with empty states. � Class. Name 51 x = new Class. Name();

Class Constructors Declaration public <constructor name> ( <parameters> ){ <constructor body> } � The constructor name: a constructor has the name of the class. � The parameters represent values that will be passed to the constructor for initialize the object state. � Constructor declarations look like method declarations— except that they use the name of the class and have no return type. 52

Example of a Constructor with No-Parameter public class Kasree { A. The instance private int bast; private int maquam; public Kasree() { }. . . new Kasree ( ) ; C 53 maquam x B Kasree x; = created with initial state object created in B is assigned to the variable. A Code 0 1 bast C. The reference of the } x Object: Kasree B. The object is bast = 0; maquam =1; x variable is allocated in memory. Object: Kasree bast maquam 0 1 State of Memory

Class with Multiple Constructors public class Kasree { private int bast; private int maquam; A. The constructor declared with no-parameter is used to create the object public Kasree() { bast = 0; maquam =1; } public Kasree(int a, int b) { bast = a; if (b != 0) maquam = b; else maquam = 1; }. . . x Object: Kasree 0 1 bast maquam B. The constructor declared with parameters is used to create the object y } Kasree x , y; A x = y = new Kasree(4, 3); 54 new Kasree() Code Object: Kasree bast B maquam 4 3 State of Memory

Copy Constructor � � Makes a new copy of an object Used in some object declarations, when an object is used as a parameter to the constructor of a class of the same type:

Class with copy Constructors public class Kasree { private int bast; private int maquam; A. The constructor declared with no-parameter is used to create the object public Kasree() { bast = 0; maquam =1; } public Kasree(Kasree k) { bast = k. bast; maquam =k. maquam; } } Object: Kasree x = y = new Kasree(x); 56 new Kasree() Code 0 1 bast k maquam B. The copy constructor declared with refrence parameters is used to create the object Kasree x , y; x y Object: Kasree A bast B maquam 0 1 State of Memory
- Slides: 55