1 / 20

IT108 Objects and Classes Part I

IT108 Objects and Classes Part I. George Mason University Revised 4/3/2012. The Qualities/State and Behavior of Objects. Qualities/State- adjectives that describe the object Examples: color, radius, position Instance variables (also known as properties or fields ) that contain values

Download Presentation

IT108 Objects and Classes Part I

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. IT108Objects and Classes Part I George Mason University Revised 4/3/2012

  2. The Qualities/State and Behavior of Objects • Qualities/State- adjectives that describe the object • Examples: color, radius, position • Instance variables (also known as properties or fields) that contain values • Behavior- verbs that denote what tasks an object can do (or can be done to it) • Examples: calculate, close, show • Usually initiated through a set of methods

  3. Class Encapsulation and Abstraction • Encapsulation- the qualities/state and behaviors are bound together into the same class • The detail of implementation is hidden from the user • Class abstraction- to separate class implementation from the use of the class • The creator of the class provides a description of the class which the user can then use • The user of the class does not need to know how the class is implemented • Real-life example: Computer hardware

  4. Classes to Objects • Class is a template/recipe that defines the qualities/state and behaviors that an actual object can have • Object is a particular instance of a class • Example: • A class of people- Children • Instances of the Child class- Billy, Julie, Peggy, Bobby • A Class is a datatype (Reference) as it defines what type of values the object can hold

  5. Classes to Objects Visualization An implementation will create an object, request information to be put inside of it, then use the special purpose methods to accomplish the data manipulation of an object

  6. Java Implementation of OOP • How is Object-Oriented Programming done in in Java? • Datatypes of variables • 4 types of methods • Usually separate classes for class definition and use of class

  7. Datatypes within Objects • Can be of primitive data types (int, boolean, char, etc.) • Can be of reference types (String, etc.) • Example: The following class contains a mix of data types (the default values depends on the datatype) • Poor programming practice to rely on default values; makes code harder to understand public class Student { String name; // name has default value null int age; // age has default value 0 boolean isScienceMajor; // isScienceMajor has default value false char gender; // gender has default value '\u0000' }

  8. OOP Behavior Design Four categories of behavior (methods): • Constructors – create objects of the class • Special purpose methods – relating to the object (e.g. find area of a circle, deposit into a checking account, …) • Accessor (get) methods – returns quality/state value of an attribute to an external class • Mutator (set) methods – change the quality/state value of an attribute based upon information given by an external class

  9. Class Definition

  10. Constructors • Default Constructor • No parameters • Used when no state values specific to the particular object are known at object creation time • Example: Circle() { radius = 1; } Note difference in approach from previous slide for default constructor; either approach is appropriate • Specific Constructor • At least one parameter • Used when state values specific to a particular object are known by the application at object creation time • Example: Circle(double newRadius) { radius = newRadius; }

  11. Constructor Notes • Constructors must have the same name as the class itself • Constructors do not have a return type… not even void • Unlike other methods, constructors can only be invoked with the new keyword/operator

  12. Constructor Notes Classes have a default constructor available without having to define it unless there are other constructors specified, in which case the default constructor must be specified for it to be available

  13. Creating Objects (through Invoking Constructors) • Declare type of variable: ClassnameobjectRefVar; • Example: Circle myCircle; • Create object and assign variable to object: objectRefVar = new ClassName(); • Examples: myCircle = new Circle(); myOtherCircle = new Circle(5.0); • In one step… ClassNameobjectRefVar = new ClassName(); • Example: Circle myCircle = new Circle();

  14. Accessing Data and Methods through Objects • Referencing the object’s data: objectRefVar.datafieldname Example:myCircle.radius • Invoking the object’s method: objectRefVar.methodName(arguments) Example:myCircle.findArea()

  15. This is a different way of invoking methods than so far in the course • Previously we called methods using notations like: Class.methodName(arguments) (e.g., Math.pow(3, 2.5)) Those methods had the keyword static in their method definition header. Although static methods can be invoked through an object, the preferred way is through the name of the class, as shown above • However, class methods are often non-static. Non-static methods must be invoked from an object using the notation: objectRefVar.methodName(arguments) (e.g., myCircle.getArea()) NOT Circle1.getArea()

  16. Instance vs. Static- Identifiers & Methods • Instance: • Variables (qualities/state) belong to a specific instance • Methods (behaviors) are called by an instance of the class • Static: • Variables values are shared by all the instances of the class • Methods are not tied to a specific object • Constants are final variables shared by all the instances of the class, therefore usually static • Declared using the static modifier • Examples: • static int numberOfObjects = 0; • int getNumberOfObjects() { … }  • static int getNumberOfObjects() { … }

  17. Instance vs. Static- Identifiers & Methods • Methods (behaviors) and Qualities/State (variables) values that are particular to a specific object should be created as instance, those common to all items of a class should be static • Example: • Each child has a particular height (instance) • however all children are under the age of 13 • (static)

  18. Class definition and implementation; Constructors and special purpose method public class TestSimpleCircle { public static void main(String[] args) { // Create a circle with radius 5.0 SimpleCircle myCircle = new SimpleCircle(5.0); System.out.println("The area of the circle of radius " + myCircle.radius + " is " + myCircle.findArea()); // Create a circle with radius 1 SimpleCircle yourCircle = new SimpleCircle(); System.out.println("The area of the circle of radius " + yourCircle.radius + " is " + yourCircle.findArea()); // Modify circle radius yourCircle.radius = 100; System.out.println("The area of the circle of radius " + yourCircle.radius + " is " + yourCircle.findArea()); } } // Define the circle class with two constructors class SimpleCircle { double radius; /** Construct a circle with radius 1 */ SimpleCircle() { radius = 1.0; } /** Construct a circle with a specified radius */ SimpleCircle(double newRadius) { radius = newRadius; } /** Return the area of this circle – note we don’t need to pass values into the method, because the necessary data is already in the object that is invoking the method */ double findArea() { return radius * radius * 3.14159; } }

  19. Placement of Classes into Java Files The definition of the class will often have no main method; the main method will be in the class that uses the class definition Each of these two classes are often placed in their own .java file; one can compile the class definition file, and then compile the other file and then run the latter; alternatively one can compile the latter and the class definition is automatically compiled, however under this latter approach one needs to pay attention to which file produces a particular compilation error

  20. Placement of Classes into Java Files If one places both classes in the same .java file, both of the classes can't be declared public; declare the class public that matches the name of the java file; this will be the one with the main method since you want it to execute

More Related