Java class methods

We learned previously that a class contains data members and methods. The methods are functions which perform operations on the class’s data members. We already saw one method which is main method, which is best example for a method and what it does. Generally a method comprises of return type, name, comma separated parameters list, and function body. The syntax of a method is given below:

returntype name (parameterlist) {
    // body of method
}

Let’s first understand a method without return type and parameters which is the simplest form of a method. Let us add a new data member to our taxi class mileage of the vehicle. Now we will create a create a method which prints all the information about a vehicle. In the given program we defined a method getDetails which uses println function to print all the information of vehicle.

/*
This is a simple Java program about Class.
Call this file KH_ClassMethods1.java.
*/

class taxi {

    int passengers;
    int capacity;
    byte type;
    double mileage;
	
	public taxi()
	{
		passengers = 4;
		capacity = 30;
		type = 0;//Desel
		mileage = 13;
	}
	
	public taxi(int p, int c, byte t, double m)
	{
		passengers = p;
		capacity = c;
		type = t;
		mileage = m;
	}
	
	void getDetails()
	{
		System.out.println("Passengers capacity: " + passengers);
		System.out.println("Fuel capacity: " + capacity);
		System.out.println("type of fuel: " + type);
		System.out.println("Mileage: " + mileage);
	}
    
}

public class KH_ClassMethods1 {
	// A Java program begins with a call to main().
	public static void main(String args[]) {
		taxi minivan = new taxi();
		taxi suv = new taxi(5, 55, (byte)1, 8);
		
		minivan.getDetails();
		System.out.println("");
		suv.getDetails();
	}
}

Download the code Run the code

Output:

Passengers capacity: 4
Fuel capacity: 30
type of fuel: 0
Mileage: 13.0

Passengers capacity: 5
Fuel capacity: 55
type of fuel: 1
Mileage: 8.0

Returning a value:

The getDetails() method above does not send any value back to its caller. But in many cases we need to send some data back to method’s caller. This can be achieved by using return values from a method.

We need to first define what type of data the method is going to return. In previous example we have used void type, which means it does not return any value. In place of void if you specify any data type then it denotes thhat this method is going to return a value of that type. Within the method body we need to use return keyword to send some data. This return keyword is also acts as exit point of that method. Once this return statement gets executed this method will be finished and cleared from the stack. Any statements after return statement will not be executed.

In our taxi class let’s write a new method which calculates the range of the vehicle Range is defined as “how much distance this vehicle can travel with full fuel tank”. This can be calculated by multiplying capacity and mileage. The range is a floating number so our getRange() method will have return type as double. At the end of the method body, we will use return statement to send calculated range value.

/*
This is a simple Java program about Class.
Call this file KH_ClassMethods2.java.
*/

class taxi {

    int passengers;
    int capacity;
    byte type;
	double mileage;
	
	public taxi()
	{
		passengers = 4;
		capacity = 30;
		type = 0;//Desel
		mileage = 13;
	}
	
	public taxi(int p, int c, byte t, double m)
	{
		passengers = p;
		capacity = c;
		type = t;
		mileage = m;
	}
	
	void getDetails()
	{
		System.out.println("Passengers capacity: " + passengers);
		System.out.println("Fuel capacity: " + capacity);
		System.out.println("type of fuel: " + type);
		System.out.println("Mileage: " + mileage);
	}
	
	double getRange()
	{
		double range = 0;
		range = capacity * mileage;
		
		return range;
	}
    
}

public class KH_ClassMethods2 {
	// A Java program begins with a call to main().
	public static void main(String args[]) {
		taxi minivan = new taxi();
		taxi suv = new taxi(5, 55, (byte)1, 8);
		
		System.out.println("Range of minivan: "+ minivan.getRange());
		System.out.println("");
		System.out.println("Range of suv: "+ suv.getRange());
		
	}
}

Download the code Run the code

Output:

Range of minivan: 390.0

Range of suv: 440.0

Method parameters

In some cases we need to send additional details to the function or method to do some operation on class data. These details can be sent by using parameters of the method. As you have already seen the parameterized constructor which takes some parameters as input, in the same manner you can define a parameterized method. For example, in our taxi classe I want to add a method which calculates fuel needed for a specific journey. Every journey will have an estimated distance to travel. So this distance is input to our method and our method calculates fuel needed for this journey. The below program demonstrates how to send parameters to a method.

/*
This is a simple Java program about Class.
Call this file KH_ClassMethods3.java.
*/

class taxi {

    int passengers;
    int capacity;
    byte type;
	double mileage;
	
	public taxi()
	{
		passengers = 4;
		capacity = 30;
		type = 0;//Desel
		mileage = 13;
	}
	
	public taxi(int p, int c, byte t, double m)
	{
		passengers = p;
		capacity = c;
		type = t;
		mileage = m;
	}
	
	void getDetails()
	{
		System.out.println("Passengers capacity: " + passengers);
		System.out.println("Fuel capacity: " + capacity);
		System.out.println("type of fuel: " + type);
		System.out.println("Mileage: " + mileage);
	}
	
	double getRange()
	{
		double range = 0;
		range = capacity * mileage;
		
		return range;
	}
	
	double getFuelNeeded (double distance)
	{
		double fuel = 0;
		fuel = distance / mileage;
		
		return fuel;
	}
    
}

public class KH_ClassMethods3 {
	// A Java program begins with a call to main().
	public static void main(String args[]) {
		taxi minivan = new taxi();
		taxi suv = new taxi(5, 55, (byte)1, 8);
		
		System.out.println("Fuel needed for minivan: "+ minivan.getFuelNeeded(100));
		System.out.println("");
		System.out.println("Fuel needed for suv: "+ suv.getFuelNeeded(100));
		
	}
}

Download the code Run the code

Output:

Fuel needed for minivan: 7.6923076923076925

Fuel needed for suv: 12.5

Method overloading

Method overloading allows us to reuse method names but have different functionality based on parameter types and combinations. For example I want to print all parameters passed to a method, but I need to have the user send no parameters or 1 integer parameter or 1 integer and 1 double parameter. If we write all these functions with different names then it might be not so user friendly as developer has to remember all different method names but all do the same operation. To avoid this problem we have method overloading in Java. Multiple methods can have same method name but  different parameters. The methods can differ in parameter data types or number of parameters.

In the below example, we are creating three methods which differ in number of parameters and type of parameters but have the same name. The first function takes no arguments, the second function takes one integer argument and the third function takes one integer and one double type. And from main function I am calling all these 3 functions with same name but sending the data appropriately. The output of this particular program explains everything about function overloading

/*
This is a simple Java program about Class.
Call this file KH_MethodOverloading.java.
*/

class example {

    void display()
	{
		System.out.println("No arguments passed.");
	}
	
	void display(int a)
	{
		System.out.println("One argument passed.");
		System.out.println("Passed Value 1: " + a);
	}
	
	void display (int a, double b)
	{
		System.out.println("Two arguments passed.");
		System.out.println("Passed Value 1: " + a);
		System.out.println("Passed Value 2: " + b);
	}
    
}

public class KH_MethodOverloading {
	// A Java program begins with a call to main().
	public static void main(String args[]) {
		example obj = new example();
		
		obj.display();
		obj.display(10);
		obj.display(34, 8.96534);
	}
}

Download the code Run the code

Output:

No arguments passed.
One argument passed.
Passed Value 1: 10
Two arguments passed.
Passed Value 1: 34
Passed Value 2: 8.96534
<<< Java garbage collection and access modifiersJava class inheritance >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .