Monday, February 12, 2018

How to declare class in Java

In this post you will learn how class is declared in java which is basic knowledge for beginners.an object is a specific combination of data and the methods that can process and communicate that data. Classes define the types for objects; hence, objects are sometimes referred to as instances of their defining class, because they take on the name of that class as their type.

Let's understand how to define a class with example. In this example a counter class for a simple counter,which can be accessed,incremented and decremented.

Example:

public class Counter{
protected int count;//a simple integer instance variable
Counter(){ //the default constructor for a counter object
count=0;
}
/** An accessor method to get the current count*/
public int getCount(){
return count;
}
/** A modifier method for incrementing the count */
public void incrementCount()
{
count++;
}
/**A modifier method for decrementing the count */
public void decrementCount()
{
return count--;
}

In the above example,notice that the class definition is delimited by braces, that is, it begins with a "{" and ends with a "} ". In Java, any set of statements between the braces "{" and "}" define a program block.

The Counter class is public, which means that any other class can create and use a Counter object. The Counter has one instance variable—an integer called count. This variable is initialized to 0 in the constructor method, Counter, which is called when we wish to create a new Counter object (this method always has the same name as the class it belongs to). This class also has one accessor method, getCount, which returns the current value of the counter. Finally, this class has two update methods—a method, incrementCount, which increments the counter, and a method, decrementCount, which decrements the counter.

Class modifiers are optional keywords that precede the class keyword. We have already seen examples that use the public keyword. 

In general,the different class modifiers and their meaning is as follows:

abstract: It is a class modifier describes a class that has abstract methods.Abstract methods are declared with the abstract keyword and are empty that means without defining a body of code for this method.

final: final class modifier describes a class that can have no subclasses.

public: public class modifier describes a class that can be instantiated or extended by anything in the same package or by anything that imports the class.

Public classes are declared in their own separate file called classname. java, where "classname" is the name of the class.

If the public class modifier is not used, the class is considered friendly. This means that it can be used and instantiated by all classes in the same package. This is the default class modifier

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...