1 / 36

Object Oriented Programming

Object Oriented Programming. Class Methods & Attributes. ClassName. att 1 : dataType 1 … att i : dataType i. Attributes. Methods (Services). + m 1 (…): dataType 1 + ... + m j (…): dataType j. Declaring a Class with Java. public class ClassName { // Attributes

guang
Download Presentation

Object Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented Programming Class Methods & Attributes

  2. ClassName • att1: dataType1 • … • atti: dataTypei Attributes Methods (Services) + m1(…): dataType1 + ... + mj(…): dataTypej Declaring a Class with Java public class ClassName { // Attributes // Methods (services) } Dr. S. GANNOUNI & Dr. A. TOUIR

  3. Attribute • An attribute is an abstraction of a single characteristic possessed by all objects of the same class. • An attribute has a nameunique within the class. • There are two types of attributes: • Class attributes (static variables) • Independent of any object and their values are shared by all objects of the class. • Instance attributes • Dependent to the objects and their values are associated with and accessed through objects.

  4. Class and Instance Attributes • Instance attributes (and methods) are: • associated with an instance (object) of the class. • and accessed through an object of the class. • each object of the class has its own distinct copy of instance 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. Dr. S. GANNOUNI & Dr. A. TOUIR

  5. Class Attributes and Objects • A class attribute is in one fixed location in memory. • Every object of the class shares class attributes with the other objects. • Any object of the class can change the value of a class attribute. • Class attributes (and methods) can also be manipulated without creating an instance of the class. Dr. S. GANNOUNI & Dr. A. TOUIR

  6. <modifiers> <data type> <attribute name> ; Class Attributes Declaration • The class attributes (and methods) are declared as instance attribute but with the static modifier in addition. Modifiers Data Type Name • public static int studentNumber ; Dr. S. GANNOUNI & Dr. A. TOUIR

  7. <class name>.<attribute name> Class Attributes Access • Class attributes (and methods) can also be manipulated without creating an instance of the class. Class Name Attribute Name • Course.studentNumber = 0 ; Dr. S. GANNOUNI & Dr. A. TOUIR

  8. class Course { // attributes public String studentName; public String courseCode ; publicstatic int studentNumber; } public class CourseRegistration { public static void main(String[] args) { Course course1, course2; //Create and assign values to course1 course1= new Course( ); Course.studentNumber = 1; course1.courseCode= new String(“CSC112“); course1.studentName= newString(“Majed AlKebir“); //Create and assign values to course2 course2 = new Course( ); Course.studentNumber ++; course2.courseCode= new String(“CSC107“); course2.studentName= newString(“Fahd AlAmri“); System.out.println(course1.studentName+ " has the course “+ course1.courseCode + “ ” + course1.studentNumber); System.out.println(course2.studentName + " has the course “+ course2.courseCode + “ ” + course2.studentNumber); } } Dr. S. GANNOUNI & Dr. A. TOUIR

  9. <modifiers> <data type> <attribute name> ; Declaring Instance Attributes With Java Modifiers Data Type Name • public String studentName ; Dr. S. GANNOUNI & Dr. A. TOUIR

  10. Example of a Class Declaration with Java public class Course { // Attributes public String studentName; public String courseCode ; // No method Members } Dr. S. GANNOUNI & Dr. A. TOUIR

  11. <modifiers> <data type> <attribute name> ; Declaring Private Attributes Modifiers Data Type Name • private String studentName ;

  12. Example of a Class withPrivate attributes ClassName • studentName: String • courseCode: String public class Course { // Attributes private String studentName; private String courseCode ; // No method Members }

  13. 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.

  14. Accessible • Inaccessible object:X public private Accessibility from Inside(the same class) All members of an instance are accessible from the instance itself.

  15. Accessible • Inaccessible :Y(client) object:X Accessibility from The Client class. public private Accessibility froman Instance in another Class Only public members Are visible from outside. All else is hidden from Outside.

  16. class Course { // Data Member private String studentName; private String courseCode ; } public class CourseRegistration { public static void main(String[] args) { Course course1, course2; //Create and assign values to course1 course1= new Course( ); course1.courseCode= “CSC112“; course1.studentName= “Majed AlKebir“; //Create and assign values to course2 course2 = new Course( ); course2.courseCode=“CSC107“; course2.studentName=“Fahd AlAmri“; System.out.println(course1.studentName+ " has the course “+ course1.courseCode); System.out.println(course2.studentName + " has the course “+ course2.courseCode); } }

  17. class Service { public int memberOne; private int memberTwo; • public void doOne() { • … • } private void doTwo() { • … • } • } • … • Service obj = new Service(); • obj.memberOne = 10; • obj.memberTwo = 20; • obj.doOne(); • obj.doTwo(); • … Accessibility Example Client Service

  18. How Private Attributes could be Accessed • Private attributes are not accessible from outside. • Except from objects of the same class. • They are accessible: • From inside: from the object containing the data itself. • From objects of the same class. • They are accessible from outside using accessor methods: • Getters • Setters

  19. public class CourseRegistration { public static void main(String[] args) { Course course1, course2; //Create and assign values to course1 course1= new Course( ); course1.courseCode= “CSC112“; course1.studentName= “Majed AlKebir“; //Create and assign values to course2 course2 = new Course( ); course2.courseCode=“CSC107“; course2.studentName=“Fahd AlAmri“; System.out.println(course1.studentName+ " has the course “+ course1.courseCode); System.out.println(course2.studentName + " has the course “+ course2.courseCode); } } class Course { // Data Member private String studentName; private String courseCode ; }

  20. Class Common Methods Constructor method: to initiate an object by sending its values as parameters Accessor method (getters): to get information about an object Mutator methods (setters):to mutate (change) an object’s state Printing method: to specify how to print an object

  21. 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. • ClassName x = new ClassName(); • A class contains at least one constructor. • A class may contain more than one constructor.

  22. Constructors • Constructors create and initialize an object to some initial state • They must have the same name as their class • They store, or assign, initial values into the fields • They often receive external parameter values for this (e.g. personName) • Some classes may have more than one constructor to initialize objects in different ways • But all constructors will still have the same name as the class

  23. Constructors (continued) • Constructors have the following properties: • - The name of a constructor is the same as the name of the class • - A constructor, even though it is a method, has no type • - A class can have more than one constructor; all constructors of a class have the same name • - If a class has more than one constructor, any two constructors must have different signatures • - Constructors are automatically executed when a class object is instantiated • - If there are multiple constructors, which constructor executes depends on the type of values passed to the class object when the class object is instantiated Java Programming: From Problem Analysis to Program Design, 4e

  24. Constructors • Two types of constructors • - With parameters • - Without parameters (default constructor) Java Programming: From Problem Analysis to Program Design, 4e

  25. 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. • ClassName x = new ClassName();

  26. public <constructor name>(<parameters>){ • <constructor body> • } Class Constructors Declaration • 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.

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

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

  29. Person Class Example Public class Person { string name; int age; // Constructor public Person( string n , int a) { name = n; age = a; } } public static void Main(string[] args) { Person P1 = new Person(”John”, 15); Person P2 = new Person (“Adam”, 27); }

  30. Are operations performed by the object returning to outsiders data retrieved from the object state. Are services called from outside allowing to retrieve data from the object state. object:X :Y (Client) public private • Getters are: • Public • With no parameters • With return value Data Data Getters Getters Methods The object point of view The user point of view

  31. public class ClassName { privatedataType1attribute1; . . . privatedataTypenattributen; . . . public dataType1getAttribute1() { returnattribute1; } . . . public dataTypengetAttributen() { return attributen; } . . . } Template for Getters

  32. Getter Example return type access modifier method name parameter list (empty in this case) public int GetAge() { return age; } return statement start and end of method body (block) Because accessor methods have a return type they must have at least one return statement

  33. Are operations performed by the object allowing to receive and store in the object state the data provided by outsiders. Are services used by outsiders allowing to provide to the object the data that should be stored in the object state. object:X :Y (Client) public private Data Data Setters • Setters are: • Public • With 1 parameter • With no return value Setters Methods The object point of view The user point of view

  34. Template for Setters public class ClassName { privatedataType1attribute1; . . . privatedataTypenattributen; . . . public void setAttribute1(dataType1param){ attribute1 = param; } . . . public void setAttributen(dataTypenparam) { attributen = param; } . . . }

  35. Class Course public class Course { // Attributes private String studentName; private String courseCode ; public Course(String n, String c) { studentName = n; courseCode =c; } public String getStudentName() { return studentName; } public String getCourseCode() { return courseCode; } ... public void setStudentName(String val) { studentName = val; } public void setCourseCode(String val) { courseCode = val; } }

  36. public class CourseRegistration { public static void main(String[] args) { Course course1, course2, course3; //Create and assign values to course1 course1= new Course( ); course1.setCourseCode(“CSC112“); course1.setStudentName(“Majed AlKebir“); //Create and assign values to course2 and course3; course2 = new Course( “Ahmed Salim”,“CSC111” ); course3 = new Course( ); course3.setCourseCode(“CSC107“); course3.setStudentName(“Fahd AlAmri“); System.out.println(course1.getStudentName()+ " has the course “ + course1.getCourseCode()); System.out.println(course2.getStudentName() + " has the course “ + course2.getCourseCode()); System.out.println(course3.getStudentName() + " has the course “ + course3.getCourseCode()); } }

More Related