Classes and Objects How can one design a
Classes and Objects
How can one design a program? Top-down structured design: uses algorithmic decomposition where each module denotes a major step in some overall process Object-oriented design: divides the problem into a set of objects that interacts to solve the problem. Your program’s properties and behaviors are modelled based upon real objects like cars, books, houses, etc.
Why OOD? Software is complex (too many people is doing too many things – the mess is inevitable ) One of the main goals in programming is to avoid the redundancy and objects can help to do this (inheritance) Objects can help increase modularity through data hiding (encapsulation)
Object You can look around you now and see many examples of real- world objects: your cat, your desk, your television set, your bicycle. These real-world objects share two characteristics: they all have state and they all have behavior For example, dogs have state (name, color, breed, hungry) and dogs have behavior (barking, fetching).
Object Software objects are modeled after real-world objects in that they, too, have state and behavior. A software object maintains its state in variables and implements its behavior with methods. An object combines data and operations on the data into a single unit.
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. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 6
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 7
Declaring Attributes With Java <modifiers> Modifiers public Introduction to OOP <data type> <attribute name> ; Data Type String Name student. Name ; Dr. S. GANNOUNI & Dr. A. TOUIR Page 8
Example of a Class Declaration with Java public class Course { // Attributes public String student. Name; public String course. Code ; // No method Members } Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 9
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 Introduction to OOP State of Memory Dr. S. GANNOUNI & Dr. A. TOUIR Page 10
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. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 11
Assigning Objects’ References to the same Instance Variable crs Course A Course crs; B A. The variable is allocated in memory. crs = new Course ( ); B. The reference to the crs = new Course ( ); new object is assigned to crs C. The reference to C Code Introduction to OOP another object overwrites the reference in crs. State of Memory Dr. S. GANNOUNI & Dr. A. TOUIR Page 12
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 Introduction to OOP State of Memory Dr. S. GANNOUNI & Dr. A. TOUIR Page 13
Assigning an Object Reference From One Variable to Another A. Variables are allocated in memory. crs 1 crs 2 B. Variables are assigned references of objects. A C. The reference in crs 2 Course crs 1, crs 2, B is assigned to crs 1 = new Course( ); crs 2 = new Course( ); crs 1 Course crs 1 = crs 2; C Introduction to OOP Course crs 2 Dr. S. GANNOUNI & Dr. A. TOUIR Course Page 14
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 Majed Al. Kebir course. Code Dr. S. GANNOUNI & Dr. A. TOUIR Page 15
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); } } Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 16
Practical hint Class Course will not execute by itself It does not have method main Course. Registration uses the class Course. Registration, which has method main, creates instances of the class Course and uses them. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 17
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> <data type> <attribute name> ; Modifiers Data Type public static Introduction to OOP int Name student. Number ; Dr. S. GANNOUNI & Dr. A. TOUIR Page 19
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 ; Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 20
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); } } Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 21
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 private modifier guarantee the information hiding. 23
Accessibility from Inside (the Instance itself) object: X public private - Accessible - Inaccessible All members of an instance are accessible from the instance itself. 24
Accessibility from an Instance of another Class object: X Accessibility from The Client class. : Y(client) public private - Accessible - Inaccessible Only public members Are visible from outside. All else is hidden from Outside. 25
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. • # : introduced later. 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> <data type> <attribute name> ; Modifiers Data Type private String Name student. Name ; 27
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 Service 30
Methods
Method Declaration Method declaration is composed of: • Method header. • Method body <method header> { <method body> } 32
Method Declaration (cont. ) <modifiers> <return type> <method name> ( <parameters> ){ <method body> } Modifier public Return Type void Method Name set. Owner. Name owner. Name = name; Parameters ( String name ) { Method body } 33
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[] 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 Passing Parameters if exist Return result if any The method body starts The method body finishes The client 36
Example of a Method with Return value public class Student { // Attributes public String student. Name; public 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; … } total = st. compute. Total. Marks(); System. out. println(total); } 37
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) { } . . . Account acct = new Account(); . . . public void add(double amt) { acct. add(400); . . . } balance = balance + amt; . . . } class Account { Formal parameter Argument (Actual Parameter) } 38
Getter, Setter and Constructor Information Hiding 39
How Private Attributes could be Accessed Private attributes are accessible from outside using accessor operations. • Getters • Setters 40
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); } } 41
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; } } 42
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()); } } 43
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 x = new Class. Name(); A class contains at least one constructor. A class may contain more than one constructor. 44
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 x = new Class. Name(); 45
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. 46
Example of a Constructor with No-Parameter public class rectangular{ private int width; A. The instance x variable is allocated in memory. private int length; Object: Kasree public rectangular() { width= 0; length=0; B. The object is width created with initial state length 0 0 }. . . } A rectangular x; x = new B ; rectangular( C ) Code State of Memory 47
Class with Multiple Constructors public class rectangular{ A. The constructor private int width; declared with no-parameter is used to create the object private int length; Object: Kasree public rectangular() { maquam } public rectangular(int a, int b) B. The constructor width = a; length=b; }. . . 0 1 bast width= 0; length=0; { x declared with parameters is used to create the object y } Kasree x , y; A x = new Kasree() y = new Kasree(4, 3); Code Object: Kasree bast B maquam 4 3 State of Memory 48
- Slides: 48