JAVA – Classes and Objects

Object and classes can be considered as the building blocks of the Java programming language. Just like the real world, larger objects are made up of different kind of smaller objects. Not only this, the objects can be combined with other objects too, which is a general characteristic of the object-oriented programming language.

Not only this, object-oriented programming language provides many different features such as classes and objects. These features make the programming language not only easy to use but also very flexible.

A class can be defined as a template for large number of objects having similar features. A particular set of objects having similar features can be embodied in a class. In an object-oriented programming language a programmer defines class of objects instead of actual classes.

For example, a Tree class can define the common characteristics of all the trees, such as their roots, stems, leaves; they grow, give oxygen and take in CO2 and so on.  This Tree class serves as an abstract instance of a Tree. Not only this, the Tree class can be used to create different instances of a Tree such as small bushy Tree, new leaves growing in the Sp[ring season, and so on. However all these classes not only originate from the original Tree class but also have the characteristics of the original Tree class.

Continue reading JAVA – Classes and Objects

Java File Structure

Information in a java program is organized in a specific way. The smallest and simplest java program can only execute if it is coded as a .java file. When a .java file is created, it must contain at least one class. A java class contains specific information such as variables and methods. A single java file is generally structured similarly:

public class Greeting

{

int someVar = 0;

public static void main(String[] args) {

System.out.println(“Hello, world!”);

}

}

In this classic “Hello, world!” output, the class Greeting is defined. A .java file must contain and define at least one class. A class can be controlled by methods defined within it. In this example, a special method main will try to show the output “Hello, world!” when the class is loaded. A class may contain fields such as the integer type of variable declared as someVar, with the value of zero. As each .java file should have one class, it should also have one class that has the main method. Greeting, someVar, and main are called identifiers. An identifier labels entities in a java program such as a class, a variable, or a method. The class Greeting is a public class. Each java file can have many classes but only one can be declared public. Note also that the method main is public, and static.

Many .java files may be actually needed to run a more complex program. Similarly, some methods and classes are meant to be reused in some other way for various programming applications. In this case, it is best to organize .java files according to their use, and group programs that generally work for the same kind of output. If a greetprogram.java file contains:

Continue reading Java File Structure