Sunday, October 13, 2013

Java Class

Insurance Loans Mortgage Attorney Credit Lawyer Donate Degree Hosting Claim Conference Call Trading Software Recovery Transfer Gas/Electricity Classes Rehab Treatment Cord Blood

What is a java Class

A class is a blueprint or prototype from which objects are created. This section defines a class that models the state and behavior of a real-world object. It intentionally focuses on the basics, showing how even a simple class can cleanly model state and behavior.

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



The following Car class is one possible implementation of a Cars


public class Car {
    
   private String modelNo;
  private double speed;
  private int gear;

    public void setModelNo(String modelNo) {
        this.modelNo = modelNo;
    }


  public void changeGear(int newGear){
      this.gear = newGear;
  }


  public void speedUp(double increment){
      this.speed = this.speed+increment;
  }
  
  public void speedDown(double decrement){
      this.speed = this.speed-decrement;
  }
  

  public void displayCarDetails(){
      System.out.println("Car Model :"+ this.modelNo);
      System.out.println("Gear :" + this.gear);
      System.out.println("Speed :" +this.speed);
  }


}


The syntax of the Java programming language will look new to you, but the design of this class is based on the previous discussion of Car objects. The fields modelNo, speed, and gear represent the object's state, and the methods (setModelNo, changeGear, speedUp etc.) define its interaction with the outside world.

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


Car Class Demo

class DemoCars{
    
    public static void main(String[] args) {
        Car carOne = new Car();
        carOne.setModelNo("ModelOne");
        carOne.changeGear(1);
        carOne.speedUp(50);
        carOne.displayCarDetails();
        
        Car carTwo = new Car();
        carTwo.setModelNo("ModelTwo");
        carTwo.changeGear(4);
        carTwo.speedUp(70);
        carTwo.displayCarDetails();
        
        
    }
    

}

The Out Put is


Car Model :ModelOne
Gear :1
Speed :50.0

Car Model :ModelTwo
Gear :4
Speed :70.0

A class can contain any of the following variable types.


Local variables: Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.

Instance variables: Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.

Class variables: Class variables are variables declared with in a class, outside any method, with the static keyword.

A class can have any number of methods to access the value of various kinds of methods. In the above example, barking(), hungry() and sleeping() are methods.






No comments:

Post a Comment