Selasa, 11 Maret 2014

What is Class in Java Programming with General Example




When I first about Class in Java I just thought what is this Class in Java and from that date to now Whenever we talk about java its really incomplete without classes, every one who are little bit familiar with java knows it’s purely object oriented language means every thing we discuss in java as object .so its very important for learner or anyone who is keen to know about java should know about java class then only they can move forward on java world.


In this article we will see what Java Class, Example of Class in Java is and what makes a Java Class including members, field and method.If you have just started programming in Java or learning Please check my article How to Set Path for Java and How to set ClassPath for Java covering two important concept PATH and CLASSPATH.





Class in Java Programming Example




What is Java Class?





What is Class in Java Program with ExampleJava 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.





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.









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.


Related Java Tutorials





































Source:http://javarevisited.blogspot.com/2011/10/class-in-java-programming-general.html

Tidak ada komentar:

Posting Komentar