Sunday, September 16, 2018

What is Class in Java Programming with General Example


Class in Java Programming 

When I first saw Class in Java, I thought it was this class in Java and from that date on now. Whenever we talk about Java it's really incomplete without classes, everyone who is familiar with Java knows that it's purely object-oriented language means everything we discussed in java as an object. That is why it is very important for the student or for those who are interested in knowing about java to know about java class, then only they can advance in the world of java.

In this article, we will look at what Java Class, Example of Class in Java is and what a Java class does including members, field and method.


Class in Java Programming Example

What is Java Class?
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.

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 the real world, if we want to understand about the class, anything of the same quality can be visualized as a class, p. Men, women, birds, bikes, cars or we can say vehicle.

The whole vehicle will take a class with no_of_wheels property, color, model, brand, etc. Now we can think of changeGear () and speedOfvehicle (), applyBreak () etc as a method in this class. Likewise, every human being can also be a class, now its member will be a man, a woman, a child., IsAlive (), isDeath () may be his method or behavior of this type. Again, we can turn men or women into a separate class and define their properties and methods accordingly,
In short, in Java, every problem we get may be thinking 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();

In short, in java everything must be thinking in terms of java class, nothing more than a model, they have their own members and methods to access these members. The entire member has its own visibility, which is decided by the developer where they want to use these objects.




No comments:

Post a Comment

From Java 8 to Java 11

Switching from Java 8 to Java 11 is more complicated than most updates. Here are some of my notes on the process. Modules Java 9 i...