190 likes | 345 Views
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.
E N D
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. • 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.
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
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
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
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
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()
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
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:
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=newString( "The Gingham Dog" ); // create an object of String type System.out.println( str ); } }
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
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 ); } }
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!
Two Types of Assignment Statements • The = operator does NOT look at objects! It only looks at references (information about where an object is located.)
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
Declaring a String Object String 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”);
Converting Strings to Numbers • Visit below online note • http://www.csub.edu/~ychoi2/MIS%20260/NotesJava/chap10/ch10_14.html • Thru to the end
Converting Strings to Numbers int 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 Doubleclass