concept of object orientation and data type conversion method

19
Concept of Object Orientation and Data Type Conversion Method Yong Choi School of Business CSU, Bakersfield

Upload: grady-reyes

Post on 01-Jan-2016

32 views

Category:

Documents


2 download

DESCRIPTION

Concept of Object Orientation and Data Type Conversion Method. Yong Choi School of Business CSU, Bakersfield. Object vs. Class. The Class is the written code; it serves as the definition of the properties and methods, or the " cookie cutter ", for a class of Objects. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Concept of Object Orientation and Data Type Conversion Method

Concept of Object Orientation and Data Type Conversion Method

Yong ChoiSchool of BusinessCSU, Bakersfield

Page 2: Concept of Object Orientation and Data Type Conversion Method

Object vs. Class The Class is the written code; it serves as the definition

of the properties and methods, or the "cookie cutter", for a class of Objects.

Class is a sort of template. To create an object, the class must be instantiated.

If the Class is the job description, the Object is the individual employee with that job title.

Page 3: Concept of Object Orientation and Data Type Conversion Method
Page 4: Concept of Object Orientation and Data Type Conversion Method

Defining Classes Programmers often use a class diagram to illustrate class features A class diagram below contains a rectangle divided into three sections.

Generic Class Diagram Employee Class Diagram

Page 5: Concept of Object Orientation and Data Type Conversion Method

Object-Oriented Programming (OOP) In OOP, there must be a class. A class is a category of things and an object is a specific

item that belongs to a class; an object is an instance of a class

With OOP you can create multiple methods with the same name, which will act differently and appropriately when used with different types of objects

Another important concept in OOP is inheritance, which is the process of acquiring the traits of one’s predecessors

Page 6: Concept of Object Orientation and Data Type Conversion Method

Understanding Inheritance The PartTimeEmployee, child

classes can inherit all of the attributes and methods from the original class - Employee (or parent class). That is, attributes or methods for descendent does not need to be created from the scratch.

Visit Java API: Math Another example can be use of

Math.length() method for many different objects.

PartTimeEmployee object

Page 7: Concept of Object Orientation and Data Type Conversion Method

Understanding Polymorphism 1 Methods need to operate differently depending on

the context Traditional way vs. OO way of GO Home Method OO way: Go home method (walk, drive a car, and riding

a bicycle). OO programs use the feature polymorphism to allow

the same operation to be carried out differently depending on the context.

Not available for non-OO programs

Page 8: Concept of Object Orientation and Data Type Conversion Method

Understanding Polymorphism 2 Method overloading occurs when different methods exist with the

same name but different argument lists Below shows an Inventory class that contains several versions of a

changeData() method

Program that uses all different versions of changeData()

Page 9: Concept of Object Orientation and Data Type Conversion Method

A Primitive Variable Here is a program that uses a primitive data type:

public class egLong {

public static void main ( String[] args ) { long value; value = 18234;

System.out.println( value ); } }

The variable value is holding “value = 18234;” Object reference variables do not work this way

Page 10: Concept of Object Orientation and Data Type Conversion Method

Two Kinds of Variables An object reference does not contain the actual data,

just a way to find it. There are two kinds of variables in Java:

Variable Type Characteristics

Primitive variables Contains the actual data.

Reference variables Contains information on how to find the object.

Page 11: Concept of Object Orientation and Data Type Conversion Method

Instantiation of an “EgString” Object

Instantiation of an object of String type: You DO NOT automatically get an object when you declare an

object reference variable. All you get is a name of an object.

public class EgString { public static void main ( String[] args ) {

String str; // str is an object reference variable that refers to an object, // but the object does not exist yet. str = new String( "The Gingham Dog" ); // create an object of String type

System.out.println( str ); } }

Page 12: Concept of Object Orientation and Data Type Conversion Method

Find an Object The object is a chunk of main memory

a reference to the object is a way to get to that chunk of memory.

The variable str does not actually contain the object, but contains information about where the object is.

Each object has a unique object reference, which is used to find it.

Very confusing concept Practical Way: think as a regular variable

Page 13: Concept of Object Orientation and Data Type Conversion Method

Larger Example – EgString2 How many different type variables are there?

public class egString2 { public static void main ( String[] args ) {

String str; Int value;

str = new String( "The Gingham Dog" ); value = 32;

System.out.println( str ); System.out.println( value ); }

}

Page 14: Concept of Object Orientation and Data Type Conversion Method

New Values in Reference Variables

public class EgString3 { public static void main ( String[] args ) { String str;

str = new String("The Gingham Dog"); System.out.println(str);

str = new String("The Calico Cat"); System.out.println(str); } }

The program does exactly what you would expect. Try out!

Page 15: Concept of Object Orientation and Data Type Conversion Method

Two Types of Assignment Statements The = operator does NOT look at objects!   It only looks at

references (information about where an object is located.)

Variable type

Information it Contains

When on the left of "= "

Primitive variables

Fixed number of bits. Contains the actual data.

Previous data is replaced with new data.

Reference variables

Contains information on how to find the object.

Old reference is replaced with a new reference

Page 16: Concept of Object Orientation and Data Type Conversion Method

Example Program for Equality of Reference Variable Contents

Here is a section from the previous program, with an additional if statement: String strA; // reference to the first object String strB; // reference to the second object strA = new String( "The Gingham Dog" ); // create the first object and

// Save its reference. System.out.println( strA ); // follow reference to first

// object and println its data. strB = new String( "The Calico Cat" ); // create the second object and

// Save its reference. System.out.println( strB ); if ( strA = = strB ) System.out.println( “Is the will be printed?");

Try out! – need to include class name and method header

Page 17: Concept of Object Orientation and Data Type Conversion Method

Declaring a String ObjectString variable An object of the class String

The class String is defined in java.lang.String and is automatically imported into every program

java.lang.Object >> java.lang.String The string itself is distinct from the variable you use to

refer to it Create a String object by using the keyword new and

the constructor method String aGreeting = new String(“Hello”);

Page 18: Concept of Object Orientation and Data Type Conversion Method

Converting Strings to Numbers

Visit below online note

http://www.csub.edu/~ychoi2/MIS%20260/NotesJava/chap10/ch10_14.html

Thru to the end

Page 19: Concept of Object Orientation and Data Type Conversion Method

Converting Strings to Numbersint cost;double interest;

String strCost = “178”;String strInterest = “0.00987”;

cost = Integer.parseInt(strCost);interest = Double.parseDouble(strInterest);

System.out.println(cost);System.out.println(interest);

To convert a String object to a double value you must use the Double class