Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Java - Class, Object and Methods in Java 
 

In this program you will be see how to use the class, object, and method's in java.

 

Java - Class, Object and Methods in Java

                         

Class : Whatever we can see in this world all the things are a object. And all the objects are categorized in a special group. That group is termed as a class. All the objects are direct interacted with its class that mean almost all the properties of the object should be matched with it's own class. Object is the feature of a class which is used for the working on the particular properties of the class or its group. We can understand about the class and object like this : we are all the body are different - different objects of the human being class. That means all the properties of a proper person relates to the human being. Class has many other features like creation and implementation of the object, Inheritance etc.

In this Program you will see how to use the class, object and it's methods. This program uses the several values of several defined variables for getting the Area and Perimeter of the Square and Rectangle  by calling the different functions from the different classes through the several objects created for the several class. In this program there are two classes has been used except the main class in which the main function is declared. First class is square which is using for getting the Area and Perimeter of the square and another class is rectangle which is using for getting the Area and Perimeter of the Rectangle. All the functions in the square and rectangle class are calling with different - different arguments two times for getting the Area and Perimeter of square and rectangle for two different sides. This program gives us the Area and Perimeter for the different sided Square and Rectangle separately. Full running code is given with the example : 

Here is the code of the program : 

class square{
  int sqarea(int side){
    int area = side * side;
    return(area);
  }
  int sqpari(int side){
      
int pari = 4 * side;
      return(pari);
  }
}

class rectangle{
  int rectarea(int length,int breadth){
   
 int area = length * breadth;
    return(area);
  }
  int rectpari(int length,int breadth){
    int pari = 2*(length + breadth);
    return(pari);
  }
}
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);
  }
}

Descriptions of the program:

Object : Objects are the basic run time entity or in other words object is a instance of a class . An object is a software bundle of variables and related methods of the special class. In the above example the sq is the object of square class and rect is the object of the rectangle class. In real-world objects share two characteristics: They have all state and behavior. For example, The squares have state such as : sides and behaviors such as its areas and perimeters. Rectangles have state such as: length, breadth and behavior such as its areas and perimeters. A object implements its behavior with methods of it's classes. A method is a function (subroutine) associated with an object. 

Syntax for the Object : 

class_name object_name = new class_name();

Output of the program : 

C:\chandan>javac ObjectClass.java

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

Download this example.

                         

» View all related tutorials
Related Tags: c file array class list ui lists method get name using this oo root example where to exam drive store

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

3 comments so far (
post your own) View All Comments Latest 10 Comments:

any sample programs using the concepts of this() and this reference ??

Posted by Mercy Monilla on Tuesday, 10.14.08 @ 15:34pm | #81082

i wrote a program that determines whether a number inputed is prime or not, but now i want to make an object that if its not prime, then it will print in the product of prime.. e.g 6 = 2 * 3 or 8 = 2 * 4;

Posted by Uchai on Wednesday, 09.24.08 @ 19:11pm | #80671

what is the differences between class and object?

Posted by usha on Sunday, 08.3.08 @ 19:25pm | #70768

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.