Presentation is loading. Please wait.

Presentation is loading. Please wait.

OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS

Similar presentations


Presentation on theme: "OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS"— Presentation transcript:

1 OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS
CS 201 OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS Copyright: FALL Illinois Institute of Technology- George Koutsogiannakis

2 PREVIOUS TOPICS Mixed Type Arithmetic Typecasting-Implicit/Explicit
Classes – What is a class? What is an object of a class? Template Classes Attributes (also called Fields or Instance Variables) Constructor methods Accessor methods Mutator Methods

3 New Topics Client Classes for Service (Template) classes.
What is an object? Object instantiation (creation) How we use a template class in a client class. Method invocation.

4 Classes A class represents a category i.e The category Student.
The class (the category) has attributes that characterize the category i.e. Student can be characterized in general by: first name , last name , student id etc. The attributes can take values and thus identify a specific participant of the class. Attributes are also called instance variables of the class.

5 What is an Object? New terms:
Instance We say that an object of a class is a particular instance of that class i.e.

6 What is an Object? Consider class Student. Class Student can have many objects. A particular object represents a set of data for the attributes of the class. The data is particular to a specific student that we can identify with an identifier (the name we give to the specific object of that class) i.e we may use the identifier st1 for a specific student whose attributes have the values: firstName=“John” lastName=“Brown” studentID= The term instance is used to indicate that we have created a particular object of a class.

7 What is an Object? Therefore when we say object of the class , it is the same as saying: A particular member of the category and the data that goes with that member. That particular member is represented by certain data. What is the data for object identified as st1 in previous slide? A class has methods. The methods of a class can operate on the data (for example the values of the various attributes). The methods, for example, can be responsible for accessing the values of the instance variables of the class and returning those values to whoever called for them. Or They can be responsible for changing the values of the instance variables of the class when called to do so.

8 What is an Object? Therefore, a particular object of a class can use the methods of that class to manipulate (access or change) its data. We say that the “object invokes a method”. Objects of the class (category) will be created in another class other than the one that defines the instance variables of the category under consideration:

9 Example Let us take the category Student:
We are going to need two classes: One that defines the attributes (instance variables) of a student and provides methods to access or change the values of those attributes. The general name for a class of that type is : SERVICE CLASS. We are going to need a second class that uses the Student class to create objects of Student and give values to each of the attributes of an object. This general name for a class of this type that use a Service type class is: CLIENT CLASS

10 Example Therefore, we are going to have to separate files (two programs). One for the Service class and one for the Client class. Each needs to be compiled by itself. Remember that because the client class uses the Service class it is required that the Service class be compiled first. Keep in mind that Service classes DO NOT require a main method. They are not intended to be run by themselves. They have other methods (like the accessor and mutator methods – the methods that change values of attributes- we discussed). Keep in mind that the Client class needs a main method (it can have more methods ). That is because the Client class needs to be run.

11 Example In our Student example we want to name each of the two classes with names that remind us of the kind of data that we are tracking. We could name the Service class: Student public class Student { We could name the client class: StudentClient public class StudentClient

12 Questions I have created a service class People and inside it I have declared 3 instance variables: String firstName String lastName int socialSecNum I have also created a class PeopleClient. Inside the main method of the class I have created an object of class People using the identifier george for it. What kind of data am I tracking for object : george? Where do I get the data for the object? In other words how do the values appear in memory and how they relate to george object only?

13 Answers I am tracking data pertaining to object’s george : first name, last name and social security number. These attributes will have specific values for george only i.e: firstName=“George” lastName=”Kay” socSecNum=1234 When I creat ed the object george in the main method of the Client class PeopleClient I had to also set the value of each one of the attributes (each one of the instance variables).

14 Objects Terminology Object reference Instantiating an object
an identifier of the object. Objects need to be identified by a name that we choose (similar to identifiers for the instance variables in the initial programs that you have done thus far). Instantiating an object creating an object of a class; assigns initial values to the object data. Objects need to be instantiated before being used. Instance of the class an object after instantiation.

15 Class Members Members of a class Fields fields can be: Methods
the class's fields (attributes or instance variables as otherwise can be called) and methods Fields instance variables and static variables (we'll define static later) Instance variables variables defined in the class and given a value in each object fields can be: any primitive data type (int, double, etc.) objects of the same or another class Methods the code to manipulate the object data

16 Object Oriented Programming
The previous concepts of class, objects, members of class etc. constitute the foundation principles for a particular technique of programming called: Object Oriented Programming (abreviated: OOP)

17 How are the OOP Concepts used in a Java Program?
First: Every Java program must represent a class (a category) Second: Every Java program will have: Data represented by : Identifiers for one or more data types that characterize the class (we called them attributes or instance variables earlier) Third: Every Java program can have one or more methods that can manipulate the data of the class.

18 Example Of Java Class Attributes
Let us say that we want to create a Java program that represents the category People. We will have to decide on the attributes that we want to use to represent the category i.e. public class People { //The attributes I chose are (with initial values of empty Strings and zero for int and double data types in this example): String firstName=" "; String lastName=" "; int socialSecurity=0; double height=0.0;

19 Example Of Java Class Next we will have to include various methods that allow the manipulation of data First we need methods that allow the creation of participants of the class (creation of objects) These methods are called constructors. There can be more than one constructor method. Their primary function is to reinitialize the values of the attributes to some know value Continuing our example of People class, we can show two constructors:

20 Example Of Java Class Constructor Methods
public People() { firstName="AnyName"; lastName="AnyName"; socialSecurity= ; height=5.8; } public People(String fn, String ln, int ssn, double ht) firstName=fn; lastName=ln; socialSecurity=ssn; height=ht;

21 Example Of Java Class Constructor Methods
Observations: Constructor methods must have the same name as the class. Constructor methods do not have a term like void between the keyword public and the name of the constructor method. The first constructor method takes no arguments (in other words it is followed by the two parentheses () with nothing written between). This is called the default constructor. The second constructor method takes arguments which means that there is something written between the two parentheses. This is called the non-default constructor (another name is: overloaded constructor).

22 Example Of Java Class Constructor Methods
Let us take another look at the two constructor methods and try to answer the following questions: What is the default constructor doing with the attributes of the class? What is the non-default constructor doing with the attributes of the class?

23 Answers The default constructor initializes the attributes (instance variables) of the class with some pre determined values that are fixed by the programmer in the Service class. The non default constructor initializes the attributes (instance variables) with values that the Client program dictates when the object is created (instantiated)

24 Answer A constructor does two tasks:
Reserves space in memory for the data of a particular object of the class under a specific identifier that the programmer chooses when he/she creates the object in the Client class. Initializes the data with some initial values for that object. These values can be changed to actual values later on by the Client program.

25 Example Of Java Class Accessor Methods
The class People can have other methods besides the constructors i.e. public String getFirstName() { return firstName; } public String getLastName() return lastName; public int getSocialSecurity() return socialSecurity;

26 Example Of Java Class Accessor Methods
The methods in the previous slide are called accessor methods. Other accessor methods can follow, They are called accessor methods because all they do is to retrieve the value of a particular attribute. Notice the get part of the name of the method followed by the name of the instance variable (attribute)

27 Example Of Java Class Mutator Methods
public void setFirstName(String fina) { firstName=fina; } public void setLastName(String lana) lastName=lana; public void setSocialSecurity(int sosec) socialSecurity=sosec; They are called mutator methods because they can change the values of the attributes.

28 Example Of Java Class There can be other methods in the class besides the accessor and mutator methods. For example we can have a method that calculates the salary of a People object based on the number of hours worked. Let us look at one of the accessor method like: public String getFirstName() This line of code is called the signature of the method. What is the meaning of String data type in the signature of the method?

29 Example Of Java Class What is the meaning of the keyword return?
What is the signature of the mutator method that can change the values of the attribute lastName ? What is the meaning of void and what is meaning of (int sosec) in : public void setSocialSecurity(int sosec)

30 Example Of Java Class Now we can finish our class People by inserting
} at the end of the class. Notice that we can compile this Java program even though we did not include a main method. We CAN NOT, however, Interpret a compiled Java class (program) that does not have a main method. Now that we have a Java program that represents the class People what do we do with it? How do we create objects of People and where?

31 Example Of Java Class Well, we can try to create another class ( a new Java program in a different file) that uses the class People. In other words this second class will be responsible for creating objects (instances) of the class People. Let us call this new class (the new Java program) PeopleClient We will create objects of People in this new class and use them.

32 Example Of Java Class This new class can have a main method like in the first Java programs we created in Lab 1. Inside the main method of the class PeopleClient, for example, we can try to create objects of class People. We need to create object references by giving names (identifiers) to the various objects that we want to create (remember that the term is instantiate).

33 FIRST Declare an Object Reference
An object reference holds the address of the object in memory. That address is the beginning in memory where the data for the fields (attributes) of the object are stored. Syntax to declare an object reference: ClassName objectReference; or ClassName objectRef1, objectRef2…; Example: People peo1; (one object reference is declared) People peo1, peo2, peo3; (3 object references are declared)

34 SECOND Instantiate an Object
Objects must be instantiated before they can be used Call a constructor using the new keyword. Remember that a constructor: special method that creates an object and assigns initial values to data Constructor has the same name as the class Syntax: objectReference = new ClassName( arg list ); (Arg list (argument list) is comma-separated list of initial values to assign to the object data)

35 Object Instantiation Example
. public class PeopleClient{ public static void main (String [] args) { People p1=new People(); People p2=new People(); People p3=new People(“George”, “Kay”, 345, 5.9); What have we coded thus far? We used the default constructor of People class to create two objects of that class: p1 and p2. Is there a difference in the two objects p1 and p2? What is the differnces if any? The non default constructor of People class was used to create a third People object p3 What data do the objects p1 , p2, p3 are encapsulating as of now? Can that data be changed? How?

36 Memory for Objects In our example we have created objects p1,p2 and p3. The system allocates memory space for each of the objects under the specific identifier name, and stores the values of the attributes for each object. The keyword new is responsible for creating the memory space for each object. The constructor sets the values of the attributes to be stored in each object’s allocated memory

37 Memory for Objects P1 -> AnyName 123456789 5.8 P2 -> AnyName
George Kay 345 5.9 Why these values for each object? See slides 20 and 35!!

38 Graphical Explanation (What happens in memory)
Suppose that we have a class called Date The Date class has attributes: month day year Suppose that in a DateClient class we instantiate 3 objects of Day class using the non default constructor independenceDay graduationDay defaultDay

39 Objects After Instantiation

40 Question What are the arguments of the non default constructor of Date class?

41 Invoking Methods An object can be used from the Client class to invoke (call) a method of the template class. Suppose Student class has an accessor method called setFirstName Suppose that in StudentClient class we instantiate an object of Student class: Student stud=new Student(); We can now can use the identifier for the object (stud) and an operator called the dot (.) to invoke the method setFirstName of Student class stud.setFirstName(“George”);

42 Method Return Values Can be a primitive data type, class type, or void
A value-returning method The method returns a value. Thus the method call is used in an expression. When the expression is evaluated, the return value of the method replaces the method call. Methods with a void return type Have no value Method call is complete statement (ends with ;) What is the method setFirstName called? Which class is that method a member of? What is the argument data type for setFirstName? What does the methdo firstName return?

43 Client Class Example Suppose that we have the class People as in your lab 3. Now we want to use it in another class. We assume that People.class and the new file PeopleClient.class are in the same folder. public class PeopleClient { public static void main(String[] args) People peo=new People(); People anotherpeo=new People(“Helen”, “Moore”, 5.5) peo.setFirstName(“George”); String fstnm=peo.getFirstName(); System.out.println(“The first name of the first person is:”+fstm); } What is the output?

44 Another Example What is the output now? public class PeopleClient {
public static void main(String[] args) People peo=new People(); People anotherpeo=new People(“Helen”, “Moore”, 5.5) peo.setFirstName(“George”); String anotherfstnm=anotherpeo.getFirstName(); String fstnm=peo.getFirstName(); System.out.println(“The first name of the first person is:”+anotherfstnm); } What is the output now?

45 More terminology The class People and some of the other classes in this presentation have no main method. They are called template or service classes. They are intended to be used by another class that has a main method!!

46 Study Guide Chapter 3 Section 3.2, 3.3, 3.4, 3.5


Download ppt "OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS"

Similar presentations


Ads by Google