1 / 19

Concept of Object Orientation and Data Type Conversion Method

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.

grady-reyes
Download Presentation

Concept of Object Orientation and Data Type Conversion Method

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. Concept of Object Orientation and Data Type Conversion Method Yong Choi School of Business CSU, Bakersfield

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

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

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

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

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

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

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

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

  10. 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 ); } }

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

  12. 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 ); } }

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

  14. Two Types of Assignment Statements • The = operator does NOT look at objects!   It only looks at references (information about where an object is located.)

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

  16. 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”);

  17. Converting Strings to Numbers • Visit below online note • http://www.csub.edu/~ychoi2/MIS%20260/NotesJava/chap10/ch10_14.html • Thru to the end

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

More Related