Creating Classes and Applications inJavaTheandroid-mania.com
What are Classes? In the real world, youll often find many individual objects allof the same kind. There may be thousands...
Declaring a class class Bicycle {int cadence = 0;int speed = 0;int gear = 1;void changeCadence(int newValue) {cadence = n...
void speedUp(int increment) {speed = speed + increment;}void applyBrakes(int decrement) {speed = speed - decrement;}void p...
 Object-Oriented Programming consists of 3 primary ideas: Data Abstraction and Encapsulation Operations on the data are...
 Inheritance Properties of a data type can be passed down to a sub-type – we canbuild new types from old ones We can bu...
 Consider primitive types Each variable represents a single, simple data value Any operations that we perform on the da...
Objects and Data Abstraction Consider the data In many applications, data is more complicated than just a simplevalue E...
Objects and Data Abstraction Consider the operations Now consider operations that a Polygon can do Note how that is sta...
Objects and Data Abstraction These operations are actually (logically) PART of the Polygon itselfint [] theXs = {0, 4, 4}...
Objects and Data Abstraction• Objects enable us to combine the data and operations of a typetogether into a single entityx...
Encapsulation andData Abstraction Recall that we previously discussed data abstraction We do not need to know the implem...
Encapsulation andData Abstraction As long as we know the method names, params and how touse them, we don’t need to know h...
Instance variables Let’s look again at StringBuffer Instance Variables These are the data values within an object Used...
Instance variables Many instance variables are declared with the keyword private This means that they cannot be directly...
Class Methodsvs. Instance Methods Recall that methods we discussed before were called class methods(or static methods) T...
Class Methodsvs. Instance Methods Class methods have no implicit data to act on All data must be passed into them using ...
Constructors,Accessors and Mutators Instance methods can be categorized by what they aredesigned to do: Constructors Th...
Constructors,Accessors and Mutators Accessors These methods are used to access the object in some way without changing i...
Constructors,Accessors and Mutators Mutators Used to change the object in some way Since the instance variables are usu...
Simple Class Example We can use these ideas to write our own classes Let’s look a VERY simple example: A circle constri...
More on Classes and Objects Classes Define the nature and properties of objects Objects Instances of classes Let’s le...
Wrappers Much useful Java functionality relies on classes / objects Inheritance (Chapter 8) Polymorphic access (Chapter...
Wrappers Wrapper classes allow us to get around this problem Wrappers are classes that “wrap” objects around primitive v...
Wrappers The wrapper classes also provide extra useful functionality forthese types Ex: Integer.parseInt() is a static m...
 However, arithmetic operations are not defined for wrapperclasses So if we want to do any “math” with our wrappers, we ...
 However, arithmetic operations are not defined for wrapper classes So if we want to do any “math” with our wrappers, we...
 In Java 1.4 and before: Programmer had to do the conversions explicitly Painful! In Java 1.5 autoboxing was added Th...
Upcoming SlideShare
Loading in …5
×

Creating classes and applications in java

201 views

Published on

Creating Classes and Applications in Java

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
201
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Creating classes and applications in java

  1. 1. Creating Classes and Applications inJavaTheandroid-mania.com
  2. 2. What are Classes? In the real world, youll often find many individual objects allof the same kind. There may be thousands of other bicycles inexistence, all of the same make and model. Each bicycle wasbuilt from the same set of blueprints and therefore containsthe same components. In object-oriented terms, we say thatyour bicycle is an instance of the class of objects known asbicycles. A class is the blueprint from which individual objectsare created.Theandroid-mania.com
  3. 3. Declaring a class class Bicycle {int cadence = 0;int speed = 0;int gear = 1;void changeCadence(int newValue) {cadence = newValue;}void changeGear(int newValue) {gear = newValue;}Theandroid-mania.com
  4. 4. void speedUp(int increment) {speed = speed + increment;}void applyBrakes(int decrement) {speed = speed - decrement;}void printStates() {System.out.println("cadence:" + cadence + " speed:" +speed + " gear:" + gear); } }Theandroid-mania.com
  5. 5.  Object-Oriented Programming consists of 3 primary ideas: Data Abstraction and Encapsulation Operations on the data are considered to be part of the data type We can understand and use a data type without knowing all of itsimplementation details Neither how the data is represented nor how the operations areimplemented We just need to know the interface (or method headers) – how to“communicate” with the object Compare to functional abstraction with methodsTheandroid-mania.com
  6. 6.  Inheritance Properties of a data type can be passed down to a sub-type – we canbuild new types from old ones We can build class hierarchies with many levels of inheritance We will discuss this more in Chapter 8 Polymorphism Operations used with a variable are based on the class of the objectbeing accessed, not the class of the variable Parent type and sub-type objects can be accessed in a consistent wayTheandroid-mania.com
  7. 7.  Consider primitive types Each variable represents a single, simple data value Any operations that we perform on the data are external to thatdataX + YTheandroid-mania.com
  8. 8. Objects and Data Abstraction Consider the data In many applications, data is more complicated than just a simplevalue Ex: A Polygon – a sequence of connected points The data here are actually: int [] xpoints – an array of x-coordinates int [] ypoints – an array of y-coordinates int npoints – the number of points actually in the Polygon Note that individually the data are just ints However, together they make up a Polygon This is fundamental to object-oriented programming (OOP)Theandroid-mania.com
  9. 9. Objects and Data Abstraction Consider the operations Now consider operations that a Polygon can do Note how that is stated – we are seeing what a Polygon CAN DO rather thanWHAT CAN BE DONE to it This is another fundamental idea of OOP – objects are ACTIVE rather thanPASSIVE Ex: void addPoint(int x, int y) – add a new point to Polygon boolean contains(double x, double y) – is point (x,y) within the boundaries of thePolygon void translate(int deltaX, int deltaY) – move all points in the Polygon by deltaX anddeltaYTheandroid-mania.com
  10. 10. Objects and Data Abstraction These operations are actually (logically) PART of the Polygon itselfint [] theXs = {0, 4, 4};int [] theYs = {0, 0, 2};int num = 2;Polygon P = new Polygon(theXs, theYs, num);P.addPoint(0, 2);if (P.contains(2, 1))System.out.println(“Inside P”);elseSystem.out.println(“Outside P”);P.translate(2, 3); We are not passing the Polygon as an argument, we are calling the methodsFROM the PolygonTheandroid-mania.com
  11. 11. Objects and Data Abstraction• Objects enable us to combine the data and operations of a typetogether into a single entityxpoints [0,4,4,0]ypoints [0,0,2,2]npoints 4addPoint()contains()translate()Thus, the operationsare always implicitlyacting on theobject’s dataEx: translate meanstranslate the pointsthat make up PTheandroid-mania.com
  12. 12. Encapsulation andData Abstraction Recall that we previously discussed data abstraction We do not need to know the implementation details of a data type inorder to use it This includes the methods AND the actual data representation of the object This concept is exemplified through objects We can think of an object as a container with data and operations inside We can see some of the data and some of the operations, but others are kepthidden from us The ones we can see give us the functionality of the objectsTheandroid-mania.com
  13. 13. Encapsulation andData Abstraction As long as we know the method names, params and how touse them, we don’t need to know how the actual data isstored Note that I can use a Polygon without knowing how the data is storedOR how the methods are implemented I know it has points but I don’t know how they are stored Data Abstraction!Theandroid-mania.com
  14. 14. Instance variables Let’s look again at StringBuffer Instance Variables These are the data values within an object Used to store the object’s information As we said previously, when using data abstraction we don’t need to knowexplicitly what these are in order to use a class For example, look at the API for StringBuffer Note that the instance variables are not even shown there In actuality it is a variable-length array with a counter to keep track of howmany locations are being used and is actually inherited fromAbstractStringBuilderTheandroid-mania.com
  15. 15. Instance variables Many instance variables are declared with the keyword private This means that they cannot be directly accessed outside the class itself Instance variables are typically declared to be private, based on the dataabstraction that we discussed earlier Recall that we do not need to know how the data is represented in order touse the type Therefore why even allow us to see it? In AbstractStringBuilder the value variable has no keyword modifier This makes it private to the packageTheandroid-mania.com
  16. 16. Class Methodsvs. Instance Methods Recall that methods we discussed before were called class methods(or static methods) These were not associated with any object Now, however we WILL associate methods with objects (as shownwith Polygon) These methods are called instance methods because they areassociated with individual instances (or objects) of a classStringBuffer B = new StringBuffer(“this is “);B.append(“really fun stuff!”);System.out.println(B.toString());Theandroid-mania.com
  17. 17. Class Methodsvs. Instance Methods Class methods have no implicit data to act on All data must be passed into them using arguments Class methods are called using:ClassName.methodName(param list) Instance methods have implicit data associated with an Object Other data can be passed as arguments, but there is always an underlyingobject to act upon Instance methods are called using:VariableName.methodName(param list)Theandroid-mania.com
  18. 18. Constructors,Accessors and Mutators Instance methods can be categorized by what they aredesigned to do: Constructors These are special instance methods that are called when an object is firstcreated They are the only methods that do not have a return value (not even void) They are typically used to initialize the instance variables of an objectStringBuffer B = new StringBuffer(“hello there”);B = new StringBuffer(); // default constructorB = new StringBuffer(10); // capacity 10Theandroid-mania.com
  19. 19. Constructors,Accessors and Mutators Accessors These methods are used to access the object in some way without changing it Usually used to get information from it No special syntax – categorized simply by their effectStringBuffer B = new StringBuffer(“hello there”);char c = B.charAt(4); // c == ‘o’String S = B.substring(3, 9); // S == “lo the”// note that end index is NOT inclusiveint n = B.length(); // n == 11 These methods give us information about the StringBuffer without revealing theimplementation detailsTheandroid-mania.com
  20. 20. Constructors,Accessors and Mutators Mutators Used to change the object in some way Since the instance variables are usually private, we use mutators to change theobject in a specified way without needing to know the instance variablesB.setCharAt(0, ‘j’); // B == “jello there”B.delete(5,6); // B == “jello here”B.insert(6, “is “); // B == “jello is here”; These methods change the contents or properties of the StringBuffer object We use accessors and mutators to indirectly access the data, sincewe don’t have direct access – see ex12.javaTheandroid-mania.com
  21. 21. Simple Class Example We can use these ideas to write our own classes Let’s look a VERY simple example: A circle constricted to an integer radius IntCircle Instance variable: private int radius Cannot directly access it from outside the class Constructor: take an int argument and initialize a new circle with the givenradius Accessors:public double area();public double circumference();public String toString(); Mutator:public void setRadius(int newRadius);Theandroid-mania.com
  22. 22. More on Classes and Objects Classes Define the nature and properties of objects Objects Instances of classes Let’s learn more about these by developing another exampletogether Goal: Write one or more classes that represent a CD (compact disc) Write a simple driver program to test itTheandroid-mania.com
  23. 23. Wrappers Much useful Java functionality relies on classes / objects Inheritance (Chapter 8) Polymorphic access (Chapter 9) Interfaces (Chapter 6) Unfortunately, the Java primitive types are NOT classes, andthus cannot be used in this way If I make an array of Object or any other class, primitive types cannotbe stored in itTheandroid-mania.com
  24. 24. Wrappers Wrapper classes allow us to get around this problem Wrappers are classes that “wrap” objects around primitive values, thus makingthem compatible with other Java classes We cant store an int in an array of Object, but we could store an Integer Each Java primitive type has a corresponding wrapper Ex: Integer, Float, Double, Boolean Ex: Integer i, j, k;i = new Integer(20);j = new Integer(40);Theandroid-mania.com
  25. 25. Wrappers The wrapper classes also provide extra useful functionality forthese types Ex: Integer.parseInt() is a static method that enables us to convert from aString into an int Ex: Character.isLetter() is a static method that tests if a letter is a characteror notTheandroid-mania.com
  26. 26.  However, arithmetic operations are not defined for wrapperclasses So if we want to do any “math” with our wrappers, we need to get theunderlying primitive values If we want to keep the wrapper, we then have to wrap the result back up Logically, to do the following:k = i + j; The actual computation being done isk = new Integer(i.intValue() + j.intValue()); In words: Get the primitive value of each Integer object, add them, thencreate a new Integer object with the resultTheandroid-mania.com
  27. 27.  However, arithmetic operations are not defined for wrapper classes So if we want to do any “math” with our wrappers, we need to get theunderlying primitive values If we want to keep the wrapper, we then have to wrap the result back up Logically, to do the following:k = i + j; The actual computation being done isk = new Integer(i.intValue() + j.intValue()); In words: Get the primitive value of each Integer object, add them, then create anew Integer object with the resultTheandroid-mania.com
  28. 28.  In Java 1.4 and before: Programmer had to do the conversions explicitly Painful! In Java 1.5 autoboxing was added This does the conversion back and forth automatically Saves the programmer some keystrokes However, the work STILL IS DONE, so from an efficiency point of view we arenot saving Should not use unless absolutely needed We will see more on how wrappers are useful after we discussinheritance, polymorphism and interfacesTheandroid-mania.com

×