Submit Your Site To The Web's Top 50 Search Engines for Free!       ExactSeek: Relevant Web Search

Visitors

Flag Counter

Total Pageviews

Saturday, November 17, 2012

About Class on JAVA Programing

In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other animal in existence, all of the same make and model. Each animal was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your animal is an instance of the class of objects known as animals. A class is the blueprint from which individual objects are created.

About Class on JAVA Programing

The following Animal class is one possible implementation of a animal:
 
class Animal {
       String food = "";
       int leg = 0;
       String respirator = "";

       void addFood(String newFood) {
            food = newFood;
       }

       void addLeg(int newLeg) {
            leg = newLeg;
       }

       void addRespirator(String newRespirator) {
            respirator = newRespirator;   
       }
  
       void printOut(){
            System.out.println("Food : "+food+" Leg : "+leg+" respirator : " + respirator);
       } 
 }

 
to execute the class, we have to make the main class. We may have noticed that the Animal class does not contain a main method. That's because it's not a complete application; it's just the blueprint for animals that might be used in an application. The responsibility of creating and using new Animal objects belongs to some other class in your application.

class BicycleDemo {
     public static void main(String[] args) {
          // Create two different Bicycle objects
          Animal animal1 = new Animal();
          Animal animal2 = new Animal();

          // Invoke methods on those objects
          animal1.addFood("tree");
          animal1.addLeg(2);
          animal1.addRespirator("gill"); 
          animal1.printOut(); 
 
          animal1.addFood("water");
          animal1.addLeg(4);
          animal1.addRespirator("lungs"); 
          animal1.addFood("reptil");  
          animal1.printOut();
     }
}

The output of this test prints the ending pedal cadence, speed, and gear for the two bicycles: 
Food : tree Leg : 2 respirator : gill
Food : reptil Leg : 4 respirator : lungs

0 comments

Post a Comment

loading