Java classes

Java classes and objects are crucial part of Java programming. You can not create a class in Java without declaring a class variable and even if you wants to store or locate some values to the program method, you can not achieve it without declaring a object in Java. So, it is extremely important to first understand the Java Classes. In the Java Classes article, we explained what is a Java Class and how important it is for any programming language.

Java classes are like a group under which all objects and methods are categorized. All these objects and methods have some same characteristics or properties that link them to one class. Properties that must be available in object and method are already defined in a class.

Class has many other features like creation and implementation of the object, Inheritance etc.

Here are two examples that will help beginners in Java understand the definition of Java classes.

First example is of Human- a group under which all of us fall. Though all have some different characteristics, we still fall under same group due to a defined property. So we all are objects of a class Human sharing some same properties.

The other example is of car. There are different manufacturers that make different types of cars that differ in model, style, shape, etc. But all of them fall under the same group i.e. Car. Hence Audi, BMW, Beetle, etc. all are objects of a group called Car.

After defining a class, it can be used to create objects by instantiating the class. Each object occupies some memory to hold its instance variables. The state of each object is separate from that of the others. After an object is created, it can be used to get the desired functionality together with its class.

Objects are the instance of a class. An object is a bundle of variables and related methods of the special class. Object implements its behavior with methods of its classes.

A method is a function associated with an object.

We have provided an example to further clarify the use of Java classes, object and its methods. Here in order to determine Area and Perimeter of the Square and Rectangle, program calls several objects of different classes.

Two classes have been used except the main class. First class is Square which is using for getting the Area and Perimeter of the square. The other class is rectangle which is used for getting the Area and Perimeter of the Rectangle.

Example of Java Classes:

class square{
int sqarea(int side){
int area = side * side;
int sqpari(int side){
int pari = 4 * side;
class rectangle{
int rectarea(int length,int breadth){
int area = length * breadth;
int rectpari(int length,int breadth){
int pari = 2*(length + breadth);
public class ObjectClass{
public static void main(String args[]){
int sarea1,sarea2,pari1,pari2;
int rectarea1,rectarea2,rectpari1,rectpari2;
square sq = new square();
rectangle rect = new rectangle();
int a=20;
System.out.println("Side of first square = " + a);
sarea1 = sq.sqarea(a);
pari1 = sq.sqpari(a);
System.out.println("Area of first square = " + sarea1);
System.out.println("Parimeter of first square = " + pari1);
a = 30;
System.out.println("Side of second square = " + a);
sarea2 = sq.sqarea(a);
pari2 = sq.sqpari(a);
System.out.println("Area of second square = " + sarea2);
System.out.println("Parimeter of second square = " + pari2);
int x = 10, y = 20;
System.out.println("Length of first Rectangle = " + x);
System.out.println("Breadth of first Rectangle = " + y);
rectarea1 = rect.rectarea(x,y);
rectpari1 = rect.rectpari(x,y);
System.out.println("Area of first Rectangle = " + rectarea1);
System.out.println("Parimeter of first Rectangle = " + rectpari1);
x = 15;
y = 25;
System.out.println("Length of second Rectangle = " + x);
System.out.println("Breadth of second Rectangle = " + y);
rectarea2 = rect.rectarea(x,y);
rectpari2 = rect.rectpari(x,y);
System.out.println("Area of second Rectangle = " + rectarea2);
System.out.println("Parimeter of first Rectangle = " + rectpari2);


C:\chandan>java ObjectClass

Side of first square = 20
Area of first square = 400
Parimeter of first square = 80
Side of second square = 30
Area of second square = 900
Parimeter of second square = 120
Length of first Rectangle = 10
Breadth of first Rectangle = 20
Area of first Rectangle = 200
Parimeter of first Rectangle = 60
Length of second Rectangle = 15
Breadth of second Rectangle = 25
Area of second Rectangle = 375
Parimeter of first Rectangle = 80
Share on Google+Share on Google+

Java classes

Posted on: December 14, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Java classes  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image