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

Slides:



Advertisements
Similar presentations
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Program Terms Up until now we have focused on application programs written in procedural oriented.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
Road Map Introduction to object oriented programming. Classes
Chapter 14: Overloading and Templates
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology- George Koutsogiannakis.
Defining Classes and Methods Chapter 4.1. Key Features of Objects An object has identity (it acts as a single whole). An object has state (it has various.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4_1 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology- George Koutsogiannakis 1.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 12 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 4 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Class Fundamentals BCIS 3680 Enterprise Programming.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes (Part 1) Lecture 3
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING I LECTURE 12 GEORGE KOUTSOGIANNAKIS
Yanal Alahmad Java Workshop Yanal Alahmad
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Lecture 4 Using Classes Richard Gesick.
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
Chapter 3 Introduction to Classes, Objects Methods and Strings
Classes Variables That Are Not of a Built-in Type Are Objects
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II LECTURE 13_2 GEORGE KOUTSOGIANNAKIS
Building Java Programs
Midterm Exam Information
Object Oriented Programming in java
Classes, Objects and Methods
Classes and Objects Object Creation
Chapter 7 Objects and Classes
CS 240 – Advanced Programming Concepts
String Objects & its Methods
Presentation transcript:

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

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 2

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

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

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

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

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

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: 8

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 9

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

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 { 11

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? 12

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

Objects Terminology Object reference – 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. 14

Class Members Members of a class – 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 15

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) 16

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

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; 18

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: 19

Example Of Java Class Constructor Methods 20 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; }

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). 21

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? 22

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) 23

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

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; } 25

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) 26

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

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? 28

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) 29

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? 30

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

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). 32

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) or People peo1, peo2, peo3; (3 object references are declared) 33

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) 34

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? 1.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? 2.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? 35

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 36

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

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 38

Objects After Instantiation

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

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”); 41

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? 42

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? 43

Another Example 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? 44

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!! 45

Study Guide Chapter 3 Section 3.2, 3.3, 3.4,