Thinking Object and Classes


Class for Object

– Class is a template of the object, which defines the properties of objects, and provides constructors to create objects and methods to manipulate objects. 
– Object is a representation of the class. 
– Many objects that can be created using only a single class.  
– An object of the class have: 
Unique identity.
State – data field, properties.
Behavior – one or a set of method or function.
– Example : 
Class : Circle
Data fields : Radius
Methods : getArea
– Use the new operator to create objects and operator dot (.) To access the member object
– Example : 
Circle obj = new Circle();
 System.out.println(“area radius of the circle is ” + obj.getArea());

 

Constructor

– Used to create objects
– First flight at the time the object was created using the new operator.
– has the same name with its class name.
– there is no return type nor the data type void.
2013-11-10_194101

 

Access Modifier

•Private : hide the class members so not accessible from outside the class
•Protected : allows members of the class can be accessed by the class or its derivatives in the classroom several similar packages.
•Public : allows the member class can be accessed by other classes class
•Package : used to organize classes. If the class that was created without using the package statement, the class will be placed in the default package. 

 

Methods

•Ability of objects
•In a programming language commonly known as a function / procedure
•Specification of header method :
  modifier return_value_type method_name(parameters){
  // Function contain
}
•Passing parameter by value is sending the value of a function that is in a class.
•A function allowing for the return value. Only void that does not return value from method. 
2013-11-10_194738
•A function will return the value in accordance with a data type functions. 

2013-11-10_194923
•Advantage of Method :

– Reusable.
Function can be used to reduce repetitive coding and the same coding can be used repeatedly by invoking the name of the function and can be used in other programs.
– Modularize.
Function makes the code into modules, so easy to search its mistakes and improve the quality of the program.
– Maintainable.
Easy to maintain. 

 

Overloading Method

•Have two or more names for the same function in a class with a sequence of different parameters.

public class TestOverloading{
public static int max( int a, int b, int c ){
if ( a > b )
return a;
else
return b;
  }
  public static double max( double a, double b )
{
  if ( a > b )
  return a;
  else
 return b;
  }
public static double max( double a, double b, double c )
{
return max( max( a , b ) , c );
}
}

 

Abstract Method

•Dummy Code Method.
•Always be defined in abstract class.
•Abstract Class : Classes which are declared abstractly, can not be real, but can be used in subclasses.
•No implementation.
•Has no content of coding in the function
•End in a semicolon (;)
– Example :
public abstract class Circle{
  private double radius;
  public void setRadius ( double x ){
  radius = x;
  }
  public abstract double getArea(); // Abstract method which return value type double
  public abstract void setRadius(); // Abstract method without returning of value And Overloading method
}

 

Static Variables and Static Methods

•Variable radius in class Circle is an instance variable
•If you want to use all of data of the entire event (instance) in a class, use static variables.
•Static Variables store value in local memory.
•Static Methods can be called without creating objects from the class. 
– Example :
public class Circle{
private double radius;
private static int numberofObject = 0;  //static variable
public static int getArea() { //static method
return numberofObject++;
}
}

 

Constants

•Used by all objects in class
•Must be declared with the keywordfinal static
 – Example :
PI constant in class Math
final static double PI = 3.14159265358979323846;

 

Inner Class

•Known as nested class.
•Defined in a class called Outer Class.
•Can access all class members which has been defined in the Outer Classes
•Create a program to be simple and concise
•Can be declared with access modifier
•Can be declared with static variables.
•A static inner class could not be accessed by non-static member from Outer Class

•Example for Inner Class :
public class OuterClass{
  private int data;
  public void m()   //functions that exist in the outer class
 {
  //statement
  }
  //an inner class which exist in the outer class
  class InnerClass
{
public void mi()  //function that exist in inner class
{
data++;  //could directly access the outer class member
m();
  }
  }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s