Saturday, 26 May 2012

(May 21' 2012 - May 25' 2012)

This whole week, we had brush up sessions to revise all that was done previously  and among the new topics covered, they were Classes and Methods.
             As we know,the class is at the core of Java. It is the logical construct upon which the entire Java
language is built because it defines the shape and nature of an object. As such, the class forms the basis for object-oriented programming in Java. Any concept you wish to implement in a Java program must be encapsulated within a class.
              It defines a new data type. Once defined, this new type can be used to create objects of that type.
Thus, a class is a template for an object, and an object is an instance of a class.
             When we create a class, we will specify the code and data that constitute that class. Collectively, these elements are called members of the class. Specifically, the data defined by the class are referred to as member variables or instance variables. The code that operates on that data is referred to as member methods or just methods.


Example:


public class ClassMethod{
  private String name;
    public void setName(String n){
     
        name = n;
    }
   
    public String getName(){
      
        return name;
    }
   public static void main(String args[]){
   ClassMethod javaClass = new ClassMethod();
      javaClass.setName("Visitor");
      
        System.out.println("Hello " + javaClass.getName());      
   
    }

}




OUTPUT:-


Hello Visitor











No comments:

Post a Comment