Home

      Hosting      Domains       Support       Contacts

Java Tutorial Home 2 3 4 5 6 7 8 9

Object Oriented Programming: User-Defined Classes



Why user-defined classes?

Primitive data types are not sufficient. In the real world, we have much more complicated objects. Object oriented programming allows us to model real-world objects. User defined classes combine the data and methods that operate on that data.



Advantages

Class is responsible for the validity of the data.
Implementation details can be hidden
Class can be reused

The client of a user-defined class is the program that instantiates instances of that class, and calls the methods of that class.

Data within user-defined classes should be private to that class, and clients of that class should use the setter and getter methods to access that data.

Syntax for defining a class:

Affordable Garage Doors Services
BidoBee.com
RushRash Inc
Garage Door Repair

accessModifiener class ClassName {

           //class definition goes here;

}

Access modifiers control access to this class. There are many ways in which a class can be accessed, such as:

  • methods of the user-defined class
  • methods of other classes, outside of the user-defined class’s package
  •  methods of subclasses that inherit the user-defined class
  • methods of classes in the same package


Key terms:

Field a variable that resides within the class, and whose scope is to the entire class (i.e., can be used in all methods within the object)
instance variables: data for each object
class data: static data that all objects shared
Class members, fields and methods within the class
Access modifiers; control the accessibility (who can reference) to the class

Access modifiers:

public – methods of the same class and methods of other classes
private – methods of the same class only
protected – methods of the same class, methods of subclasses, and methods of classes in the same package
none – methods in the same package only

public vs. private

Classes are usually defined as public
Instance variables are usually declared to be private
Methods that will be called by the client of the class are usually declared to be public
Methods that will be called only by other methods of the same class are usually declared to be private
APIs of methods are published (made known) so that clients will know how to instantiate objects and call the methods of the class.
Access modifiers that are defined within the scope of a class, control the access to that member within the scope of the class.

public class Student {
     private int gpa;
     //… more stuff here
     public int getGpa() {
     return this.gpa; // THIS IS OK, because access is private to scope of class.
          }
     }

public class StudentClient {
          public void main(object[] args) {
               Student student = new Student();
               student.gpa = 2.9; //NO, THIS IS NOT ALLOWED, we are
                                             //outside scope of Student class.
          //…


Syntax for defining instance variables
     accessmodifier type name = value;

The auto class
     public class Auto
     {
          private String model;
          private int milege;
          private double year;
     }


Writing Methods:
Syntax for defining methods:

accessmodifier returnType methodName(parameter list) { //method header
        //method body
}


Methods have scope, for instance, the objects in the parameter list have a scope that is limited to this method, additionally, all fields defined within that method, will have scope that is limited to that method. For instance:

Method Return Types

The return type of methods is the data type of the value that the method returns to the caller. The return type can be any of Java’s primitive types, class types or void (nothing returned). Methods with a return type of void do not return a value to the caller.

main is a method, if we look at main’s signature in detail:

public – main can be called from outside the class (The JVM calls main)
static – main can be called by the JVM without instantiation
void – main does not return a value;
String[] args – main’s parameter is a string array

Value-returning methods

Use a return statement to return the value i.e.,
         return expression;

Constructors

Special methods that are called when an object is instantiated using the new keyword.
A class can have several constructors. The job of the class constructors is to initialize the instance variables of the new object.


Syntax of a constructor:
      public ClassName( parameter list) {
            //constructor body;
      }

No return value needed for Constructors, not even void.
Default constructor is the constructor that has no parameters provided.
If you don’t create a default constructor, the compiler will create one for you


Defaults for values:

Primitive types have default values so you don’t need to provide a value when they are declared. For example a field that is defined as integer will be defaulted to 0. While a double field default value is 0.0. Objects are defaulted to null and a boolean is defaulted to false.

Summary of scope

A method in a class can access:

- The instances variables of its class
- any parameters sent to the method
- any variable the method declares
- other methods in the class

Accessor methods
Allow clients read access private data.

Mutator methods
Allow clients write access to private data

The object reference this
this is an implicit parameter that is available within the class. this a reference to the current instance of the class. this is useful when your code needs to differentiate like-named methods, with different scopes, i.e.,
             this.model = model;
this is not available within a static method, because this has no instance.


The toString() method

Returns a string representing the data of an object. Client can call toString explicity to get a string representation of the object. Framework methods, such as System.out.println(), call the toString on the objects that are provided when it prints the object to the output stream (implicitly called).


The equals method
Determines if the data in another object is equal to the data in this object

Static methods
also called class methods
often defined to access and change static variables
static methods cannot access instance variables
static methods are associated with the class, not with any object
static methods can be called before any object is instantiated, so it is possible that there will be no instance variables to access.

Creating Packages

- A package is a collection of related classes that can be imported into a program.
- Packages allow reuse of classes without needing the class in the same directory as other source files
- To include a class in a package, precede the class definition with the package statement
        package packageName;

Naming packages
To avoid name collisions, which can occur when multiple programmers define packages, Java 5 Illuminated authors uses this naming convention Use the reverse of the domain name, excluding www, for example, for a domain name:

www.hostitwise.com
• the package name would begin with
com.hostitwise
• then add the package name (unique name: java hosting for example)
com. hostitwise.javahosting
for the package, create three directories, com, hostitwise, and javahosting. Then the class file

Classpath
The CLASSPATH environment variable tells the compiler where to look for
Packages. Set the CLASSPATH to the include directory in which you created the com
directory.
        Windows c:\directory
To use the class in the package, you use the import statement .

Java Documentation (JavaDoc)

- Reads comments from code and compiles into html documentation
- JavaDoc reads comments that begin with /**
- The @param tag indicates the text next to it will be documented as a parameter to
- the method
- The @return tag indicates the text next to it will be documented as the return
- To execute run the javadoc.exe program and provide your class name as the argument. It will output the html to the working directory

 

Java Tutorial Home 2 3 4 5 6 7 8 9

 


Java Tutorial


| Home | Hosting | Domains | Support |  Contacts |
|
Terms & Condition | Privacy Policy |


 

Copyright © 2005 by HostItWise.com Read our Copyright. All rights reserved.

 RushRash Inc