basic concepts of classes by AmarMaarufGhafar

VIEWS: 2 PAGES: 24

									Basic Concepts of Classes
Chapter 3
Chapter 3

   3.0 Basic Concepts of Classes
     3.1 Class Concept

     3.2 Class Defination

     3.3 Data Members

     3.4 Basic Types of Methods

     3.5 Methods Defination

     3.6 Static Fields

     3.7 Predefined classes and wrapper classes
Class Concept

   A class consists of
     data members (attributes)

     methods that perform on that data (behaviour)
Class Diagram



        class name
        instance variables

        methods
Class Defination

class classname {                Class declaration
  //declare instance variables
  type var1;                        Variables declaration
  type var2;


  //declare methods
  type method1(parameters){
      //body of method
  } //end of method1                 Methods

  type method2(parameters){
      //body of method
  } //end of method2
} //end of class
Access Modifier / Access Specifier

   Through encapsulation, you can control what parts of a program
    can access the members of a class.
   How a member of a class can be accessed is determined by the
    access modifier that modifies its declaration.
   Java’s access modifiers are:
     public

     private

     protected
Access Modifier / Access Specifier


Visibility                    public   protected   private

From the same class            YES      YES        YES
From any class in the same
                               YES      YES         NO
package
From any class outside the
                               YES       NO         NO
package
From a subclass in the same
                               YES      YES         NO
package
From a subclass outside the
                               YES      YES         NO
package
Defining a Class

General syntax:           public modifier is
                           commonly used
  <modifier> class <Classname>
  {
       …….body of class
  }


                             first letter is
                              uppercase
Variables
                                 private modifier is
                                       used
   Instance variables
      Syntax:
      <modifier> <primitive / reference data
      type><variable> ;


   Class variables
     Syntax:
      <modifier> static <primitive data type/ reference
      data type><variable> ;
Methods                       public modifier is
                               commonly used

   General syntax :
                                             Primitive/reference

    <modifier> <returntype> <nameOfMethod>( parameter)
    {
         ……..body of method
    }




                                  The identifier starts with a
                                       lowercase letter
Common Methods in a Class
Example 1:

Q: Write a program to input the length and width of a rectangle and
   calculate and print the perimeter and area of the rectangle.


Step 1 : Identify all the relevant nouns.
             Length
             Width
             Rectangle
             Perimeter
             Area
Example 1: continue…

Step 2 : Identify the class(es).
   It is clear that:
              Length – length for the rectangle
              Width – width for the rectangle
              Rectangle
              Perimeter – perimeter for the rectangle
              Area – area for the rectangle
   Therefore, Rectangle is suitable to be a class.
Example 1: continue..

Step 3 : Identify the data members for each of the classes.
  Determine the information that is essential to describe each class.

             Perimeter – is not needed. It can be computed from length &
              width.
             Area – is also not needed. It can be computed from length &
              width.
             Length – is required . It is one of the data member
             Width – is required. It is one of the data member
Example 1: continue..

Step 4 : Identify the operations for each of the classes.
  The operations can be determine by looking at the verbs. For
  example: input, calculate & print

      “Write a program to input the length and width of a rectangle and
        calculate and print the perimeter and area of the rectangle”
Example 1: continue..

Therefore, you need at least the following operations.


           Set the Length
           Set the Width
           Calculate the Perimeter
           Calculate the Area
           Print the perimeter
           Print the area
Example 1: continue..

Other operations that are customary to include:


           Get the Length
           Get the Width
Example 1: continue..


                        Rectangle
                   length
                   width
                   setLength
                   setWidth
                   getLength
                   getWidth
                   calculatePerimeter
                   calculateArea
Class Diagram      print
Example 1: Defining a Class and Variables

 public class Rectangle
 {
     private double length;
     private double width;
 }
Example 1: Constructor

 public class Rectangle
 {
     private double length;
     private double width;
                              Set initial values for
                                  all variables
     public Rectangle ( )
     {
           length=0.0;
           width=0.0;
     }
 }
Example 1: Mutator methods

 //continue..


 public void setLength( double len)
 {                                  Assign value len to
     length=len;                          length
 }
 public void setWidth(double wid)
 {                            Assign value wid
     width=wid;                   to width
 }
Example 1: Accessor methods

 //continue..


 public double getLength()
 {                            return value length
     return length;
 }
 public double getWidth()
 {
     return width;            return value width

 }
Example 1: Processor methods

 //continue..


 public double calculatePerimeter()    Calculate & return
 {                                      value perimeter
     double perimeter=(2*length)+2*width;
     return perimeter;
 }
 public double calculateArea()        Calculate & return
                                          value area
 {
     return length*width;
 }
Example : Printer method

 //continue..
                                     Return all values
                                        as String
 public String     toString()
 {
     String out= “Length = “ + length + “\nWidth =
 “ + width;
     return out;
 }

								
To top