Methods
The member methods are mostly designed inside the class to access the instance variable.
- Syntax:
- return_type method_name([arguments])
- {
- ………………………..
- body of the method
- ………………………..
- [return expression;]
- }
Return type:
Specifies the type of value returned by the method. If the method does not return any value specify the return type as void, which means nothing.
Arguments:
If the method returns a value, you need to use this clause for returning the value, expression can be variable or a mathematical expression.
Accessing methods: object_name.method([values]);
- Methods without return_type and without arguments:
- Example:
class Box{
- double width;
- double height;
- double depth;
- void input()
- {
- width=6;
- height=5;
- depth=7;
- }
- void compute()
- {
- double vol=width*height*depth;
- System.out.println(“The volume of the box is : “+vol);
- }
}class App15{
- public static void main(String yck[])
- {
- Box b=new Box();
- b.input();
- b.compute();
- }
} |
- output:
- \>javac App15.java
- \>java App15
- The volume of the box is : 210.0
Methods with return_type and without arguments:
- Example:
class Box{
- double width;
- double height;
- double depth;
- void input()
- {
- width=6;
- height=5;
- depth=7;
- }
- double compute()
- {
- double vol=width*height*depth;
- return vol;
- }
}class App16{
- public static void main(String yck[])
- {
- Box b=new Box();
- b.input();
- double volume=b.compute();
- System.out.println(“The volume of the box is : “+volume);
- }
} |
- output:
- \>javac App16.java
- \>java App16
- The volume of the box is : 210.0
Methods without return_type and with arguments:
- Example:
class Box{
- double width;
- double height;
- double depth;
- void input(double w,double h,double d)
- {
- width=w;
- height=h;
- depth=d;
- }
- double compute()
- {
- double vol=width*height*depth;
- return vol;
- }
}class App17{
- public static void main(String yck[])
- {
- Box b=new Box();
- b.input(10,20,30);
- double volume=b.compute();
- System.out.println(“The volume of the box is : “+volume);
- }
} |
- output:
- \>javac App17.java
- \>java App17
- The volume of the box is : 6000.0
Overloading methods:
A class can have any number of methods with the same name but different formats. This is commonly referred as method overloading. This concept depicts, POLYMORPHISM.
- Example:
class Box{
- double width;
- double height;
- double depth;
- void input()
- {
- width=10;
- height=20;
- depth=30;
- }
- void input(double l)
- {
- width=height=depth=l;
- }
- void input(double w,double h,double d)
- {
- width=w;
- height=h;
- depth=d;
- }
- void compute()
- {
- double vol=width*height*depth;
- System.out.println(“The volume of the box is : “+vol);
- }
}class App18{
- public static void main(String yck[])
- {
- Box b1=new Box();
- b1.input();
- b1.compute();
- Box b2=new Box();
- b2.input(10);
- b2.compute();
- Box b3=new Box();
- b3.input(5,6,7);
- b3.compute();
- }
} |
- output:
- \>javac App18.java
- \>java App18
- The volume of the box is : 6000.0
- The volume of the box is : 1000.0
- The volume of the box is : 210.0
Methods with objects as arguments:
- Example:
class Box{
- double width;
- double height;
- double depth;
- void input()
- {
- width=10;
- height=20;
- depth=30;
- }
- void input(Box ob)
- {
- width=ob.width;
- height=ob.height;
- depth=ob.depth;
- }
- void compute()
- {
- double vol=width*height*depth;
- System.out.println(“The volume of the box is : “+vol);
- }
}class App19{
- public static void main(String yck[])
- {
- Box b1=new Box();
- b1.input();
- b1.compute();
- Box b2=new Box();
- b2.input(b1);
- b2.compute();
- }
} |
- output:
- \>javac App19.java
- \>java App19
- The volume of the box is : 6000.0
- The volume of the box is : 6000.0
Nesting of member method:
- Example:
class Box{
- double width;
- double height;
- double depth;
- void input()
- {
- width=10;
- height=20;
- depth=30;
- }
- void compute()
- {
- input();
- double vol=width*height*depth;
- System.out.println(“The volume of the box is : “+vol);
- }
}class App20{
- public static void main(String yck[])
- {
- Box b1=new Box();
- b1.compute();
- }
} |
- output:
- \>javac App20.java
- \>java App20
- The volume of the box is : 6000.0