Thursday 21 November 2013

Classes in Java


Classes in Java :

Java class is nothing but a template for object you are going to create or it’s a blue print by using this we create an object. In simple word we can say it’s a specification or a pattern which we define and every object we define will follow that pattern. In the object oriented approach, a class defines a set of properties (variables) and methods. Through methods certain functionality is achieved. Methods act upon the variables and generate different outputs corresponding  to the change in variables. A Java class is a group of Java methods and variables. Each Java source code file can contain one public class. The name of this public class must match the name of the Java source code file. If the public class is called “ballistics,” then the filename would be “ballistics.java.”



What does Java Class Consist?

When we create class in java the first step is keyword class and then name of the class or identifier we can say. Next is class body which starts with curly braces {} and between this all things related with that class means their property and method will come here.

Template is:

Class (name of the class) {

(Here define member of class)

}

Access level of class - Java class has mainly two type of access level:

Default - class objects are accessible only inside the package.

Public - class objects are accessible in code in any package.

Syntax of a Java Program :

package package_name;

//import necessary packages

import package_name.*;

Access Modifier class class_name{

member variables;
member methods;

public static void main(String[] args) {
    ...
    ....

    }

}


Objects in Java:

If we consider the real-world we can find many objects around us, Cars, Dogs, Humans, etc. All these objects have a state and behavior. If we consider a dog, then its state is - name, breed, color, and the behavior is - barking, wagging, running. If you compare the software object with a real world object, they have very similar characteristics. Software objects also have a state and behavior. A software object's state is stored in fields and behavior is shown via methods.
So in software development, methods operate on the internal state of an object and the object-to-object communication is done via methods.

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.

What are members of Class?

When we create a class its totally incomplete without defining any member of this class same like we can understand one family is incomplete if they have no members.

  - Field: field is nothing but the property of the class or object which we are going to create .for example if we are creating a class called computer then they have property like model, mem_size, hd_size, os_type etc

  - Method: method is nothing but the operation that an object can perform it define the behavior of object how an object can interact with outside world .startMethod (), shutdownMethod ().

  - Access Level of members: Access level is nothing but where we can use that members of the class.
Each field and method has an access level:

    private: accessible only in this class
    package or default: accessible only in this package
    protected: accessible only in this package and in all subclasses of this class
    public: accessible everywhere this class is available

Real world example of Class in Java Programming :

In real world if we want to understand about the class everything of same quality  can be visualize as a class e.g. men,women,birds ,bicycles ,cars or  we can say vehicle .
The entire vehicle will make one class they have the property like no_of_wheels, color, model, brand etc.now we can think changeGear () and speedOfvehicle (), applyBreak () etc as a method on that class. Similarly all human being also can be one class now their member will be a men ,women ,child.,isAlive() ,isDeath() can be their method or behavior of that class .again we can make Men or women a separate class and define their property and method accordingly,
In short in java every problem we get solution can be think in terms of class and object.

One Java class example:

Class Stock {

Public commodity;
Public price;
Public void buy (int no_of commodity) {}
Public boolean sale () {}

}

In this example Stock is called Class and commodity, price are field and buy() and sale() are two methods defined inside class. To access elements of Class you need to create an instance of class Stock. You can create instance of Class using keyword new as shown below

Stock highBetaStock = new Stock();

For calling method of Stock just call by using instance.

highBetaStock.buy(1000);
highBetaStock.sell();

Summary:

In short in java everything must be thinking in terms of java class its nothing but a template they have their own members and methods for accessing those members. The entire member has their own visibility which is decided by the developer where they want to use those objects.

Preeti Bagad [BE(CS)] 
SW Engineer Cum Blogger

On Line Assistence :
Y! Messenger : PreetiB.A1Soft@yahoo.com







Best Ground Staff Fly-Crew

Best Cabin Crew Fly-Crew

Best AME Fly-Crew

Best Pilots Fly-Crew





UpLoad Your CV only for Rs 100 or USD $ 10 for 1 Year

We will send you Job Alerts and your CV to all HRs
Mail to, 
Vidhya@aerosoft.co.in
We believe that for
Pessimistic its Aviation Recession
and for
Optimistic it is an Opportunity.

No comments:

Post a Comment