Tuesday, June 29, 2010

Add two numbers using java

This program will compute two numbers and get the sum.
a=90, c=80

program code:


import java.io.*;

public class compute
{
public static void main(String args[])
{
//Declaration of variables
int a=90;
int b=80;
int sum=0;

//compute the sum
sum=a+b;

System.out.print("The sum is" + sum);
}
}

Friday, June 25, 2010

Encapsulation in Java

Encapsulation - refers to the hiding of data and methods within an objects. and also protects data from corruption.
                       - easier to modify programs since one object type is modified at a time.

Abstraction in java

Abstraction refers to the ability to make a class abstract in OOP.

An abstract class is one that cannot be instantiated. All other functionality of the class still exists, and its fields, methods, and constructors are all accessed in the same manner. You just cannot create an instance of the abstract class.

Thursday, June 24, 2010

Creating an object in java program

Creating an object in java program 
class Puppy{
   public Puppy(String name){
      // This constructor has one parameter, name.
      System.out.println("Your Name is :" + name ); 
   }
public static void main(String []args){
      // Following statement would create an object myPuppy
      Puppy myPuppy = new Puppy( "Jaypee" );
   }
}
sample output:
Your Name is : Jaypee

Creating an Object in Java

Creating an Object:

As mentioned previously a class provides the blueprints for objects. So basically an object is created from a class. In java the new key word is used to create new objects.
There are three steps when creating an object from a class:
  • Declaration . A variable declaration with a variable name with an object type.
  • Instantiation . The 'new' key word is used to create the object.
  • Initialization . The 'new' keyword is followed by a call o a constructor. This call initializes the new object.

java tutorial

http://www.tutorialspoint.com/java/index.htm

Objects and Classes in Java

Objects - represent "things: from the real world.

the objects is made up of attributes and methods.
Attributes - define the object.
methods - describes the mechanism that actually perform its tasks.

Classes - describes a group or collection of objects with common properties and also define a type of objects
  
Example of classes:  
        - employee
       - car   
       -chair