GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Java method - parameters, hiding, overriding Java methodlast modified January 27, 2024 In this article we cover Java methods. In object oriented programming, we work with objects. Objects are basic building blocks of a program. Objects consists of data and methods. Methods change the state of the objects created. They are the dynamic part of the objects; the data is the static part. Java method definitionA method is a code block containing a series of statements. Methods must be declared within a class. It is a good programming practice that methods do only one specific task. Methods bring modularity to programs. Proper use of methods brings the following advantages:
Java method characteristicsBasic characteristics of methods are:
Access level of methods is controlled with access modifiers. They set the
visibility of methods. They determine who can call the method. Methods may
return a value to the caller. If our method returns a value, we declare its data
type. If not, we use the
Method parameters are surrounded by parentheses and separated by commas. Empty
parentheses indicate that the method requires no parameters. The method block is
surrounded with Java method signatureA method signature is a unique identification of a method for the Java compiler. The signature consists of a method name, and the type and kind (value, reference, or output) of each of its formal parameters. Method signature does not include the return type. Java method namesAny legal character can be used in the name of a method. By convention, method names begin with a lowercase letter. The method names are verbs or verbs followed by adjectives or nouns. Each subsequent word starts with an uppercase character. The following are typical names of methods in Java:
Java method exampleWe start with a simple example. com/zetcode/ShowInfoMethod.java
package com.zetcode;
class Base {
public void showInfo() {
System.out.println("This is Base class");
}
}
public class ShowInfoMethod {
public static void main(String[] args) {
Base bs = new Base();
bs.showInfo();
}
}
We have a class Base {
public void showInfo() {
System.out.println("This is Base class");
}
}
Each method must be defined inside a class. It must have a name. In our case,
the name is public static void main(String[] args) {
...
}
This is the Base bs = new Base(); bs.showInfo();
We create an instance of the Java method parametersA parameter is a value passed to the method. Methods can take one or more parameters. If methods work with data, we must pass the data to the methods. This is done by specifying them inside the parentheses. In the method definition, we must provide a name and type for each parameter. com/zetcode/Addition.java
package com.zetcode;
class AddValues {
public int addTwoValues(int x, int y) {
return x + y;
}
public int addThreeValues(int x, int y, int z) {
return x + y + z;
}
}
public class Addition {
public static void main(String[] args) {
AddValues a = new AddValues();
int x = a.addTwoValues(12, 13);
int y = a.addThreeValues(12, 13, 14);
System.out.println(x);
System.out.println(y);
}
}
In the above example, we have an public int addTwoValues(int x, int y) {
return x + y;
}
The public int addThreeValues(int x, int y, int z) {
return x + y + z;
}
The int x = a.addTwoValues(12, 13);
We call the Variable number of argumentsA method can take variable number of arguments. For this we use ellipsis. com/zetcode/SumOfValues.java
package com.zetcode;
public class SumOfValues {
public static int sum(int...vals) {
int sum = 0;
for (int val : vals) {
sum += val;
}
return sum;
}
public static void main(String[] args) {
int s1 = sum(1, 2, 3);
int s2 = sum(1, 2, 3, 4, 5);
int s3 = sum(1, 2, 3, 4, 5, 6, 7);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
We create a int s1 = sum(1, 2, 3); int s2 = sum(1, 2, 3, 4, 5); int s3 = sum(1, 2, 3, 4, 5, 6, 7);
We call the public static int sum(int...vals) {
...
}
The int sum = 0;
for (int val : vals) {
sum += val;
}
return sum;
We compute the sum of the values and return the calculated sum. $ java com.zetcode.SumOfValues 6 15 28 Passing arguments by valueIn Java, arguments are always passed by value to methods. When we pass primitive types, the copies of the values are sent to the methods. In case of objects, copies of the references are handed to the methods. Java does not support passing arguments by reference, like C# or C++. com/zetcode/PassByValue.java
package com.zetcode;
class Cat {}
class Dog {}
public class PassByValue {
private static void tryChangeInteger(int x) {
x = 15;
}
private static void tryChangeObject(Object o) {
Dog d = new Dog();
o = d;
}
public static void main(String[] args) {
int n = 10;
tryChangeInteger(n);
System.out.println(n);
Cat c = new Cat();
tryChangeObject(c);
System.out.println(c.getClass());
}
}
The example shows that it is not possible to change the values of primitive types and the references to objects inside methods. private static void tryChangeInteger(int x) {
x = 15;
}
The value of the passed variable is copied to the local variable private static void tryChangeObject(Object o) {
Dog d = new Dog();
o = d;
}
The same applies for objects. We are passing copies of references to methods.
The o is a local variable that refers to the int n = 10; tryChangeInteger(n); System.out.println(n);
We define the Cat c = new Cat(); tryChangeObject(c); System.out.println(c.getClass());
We define a $ java com.zetcode.PassByValue 10 class com.zetcode.Cat From the output we can see that neither the primitive value nor the object were modified. Method overloading in JavaMethod overloading allows the creation of several methods with the same name which differ in the type of the input.
What is method overloading good for? Qt library gives a nice example for the
usage. The
If the C++ language, which is the language in which Qt is developed, didn't have
method overloading, the creators of the library would have to name the methods
like com/zetcode/Overloading.java
package com.zetcode;
class Sum {
public int getSum() {
return 0;
}
public int getSum(int x) {
return x;
}
public int getSum(int x, int y) {
return x + y;
}
}
public class Overloading {
public static void main(String[] args) {
Sum s = new Sum();
System.out.println(s.getSum());
System.out.println(s.getSum(5));
System.out.println(s.getSum(5, 10));
}
}
We have three methods called public int getSum(int x) {
return x;
}
This one takes one parameter. System.out.println(s.getSum()); System.out.println(s.getSum(5)); System.out.println(s.getSum(5, 10)); We call all three methods. All methods have the same name. The compiler knows which method to call on the grounds of the method input. $ java com.zetcode.Overloading 0 5 15 And this is what we get when we run the example. Java recursionRecursion, in mathematics and computer science, is a way of defining methods in which the method being defined is applied within its own definition. In other words, a recursive method calls itself to do its job. Recursion is a widely used approach to solve many programming tasks. Every problem solved using a recursion can be worked out with the iteration too. A typical example is the calculation of a factorial. com/zetcode/Recursion.java
package com.zetcode;
public class Recursion {
static int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
public static void main(String[] args) {
System.out.println(factorial(6));
System.out.println(factorial(15));
}
}
In this code example, we calculate the factorial of two numbers. return n * factorial(n - 1); Inside the body of the factorial method, we call the factorial method with a modified argument. The function calls itself. This is the essence of the recursion algorithm. $ java com.zetcode.Recursion 720 2004310016 These are the results. Java method scopeA variable declared inside a method has a method scope. The scope of a name is the region of program within which it is possible to refer to the entity declared by the name without the qualification of the name. A variable which is declared inside a method has a method scope. It is also called a local scope. The variable is valid only in this particular method. com/zetcode/MethodScope.java
package com.zetcode;
class Test {
int x = 1;
public void exec1() {
System.out.println(this.x);
System.out.println(x);
}
public void exec2() {
int z = 5;
System.out.println(x);
System.out.println(z);
}
}
public class MethodScope {
public static void main(String[] args) {
Test ts = new Test();
ts.exec1();
ts.exec2();
}
}
In this example, we have the public void exec1() {
System.out.println(this.x);
System.out.println(x);
}
The x variable, also called the x field, is an instance variable. It is accessible
through public void exec2() {
int z = 5;
System.out.println(x);
System.out.println(z);
}
The x variable can be accessed also in the $ java com.zetcode.MethodScope 1 1 1 5
A variable defined inside a method has a local/method scope. If a local variable
has the same name as an instance variable, it shadows the instance
variable. The instance variable is still accessible inside the method with
com/zetcode/Shadowing.java
package com.zetcode;
class Test {
int x = 1;
public void exec() {
int x = 3;
System.out.println(this.x);
System.out.println(x);
}
}
public class Shadowing {
public static void main(String[] args) {
Test t = new Test();
t.exec();
}
}
We declare an instance variable System.out.println(this.x); System.out.println(x);
The variables are accessed differently. The $ java com.zetcode.Shadowing 1 3 Java static methodsStatic methods are called without an instance of the object. To call a static method, we use the name of the class and the dot operator. Static methods can only work with static variables. Static methods are often used to represent data or calculations that do not change in response to object state. An example is a math library which contains static methods for various calculations.
We use the com/zetcode/StaticMethod.java
package com.zetcode;
class Basic {
static int id = 2321;
public static void showInfo() {
System.out.println("This is Basic class");
System.out.format("The Id is: %d%n", id);
}
}
public class StaticMethod {
public static void main(String[] args) {
Basic.showInfo();
}
}
In our code example, we define a static static int id = 2321; A static method can only work with static variables. Static variables are not available to instance methods. public static void showInfo() {
System.out.println("This is Basic class");
System.out.format("The Id is: %d%n", id);
}
This is our static Basic.showInfo(); To invoke a static method, we do not need an object instance. We call the method by using the name of the class and the dot operator. $ java com.zetcode.StaticMethod This is Basic class The Id is: 2321 Java hiding methodsIn case of static methods, a method in the derived class with the same signature as in the base class hides the one in the base class. The method to be called is determined at compile time. This process is referred to as early or static binding. com/zetcode/Hiding.java
package com.zetcode;
class Base {
public static void showInfo() {
System.out.println("This is Base class");
}
}
class Derived extends Base {
public static void showInfo() {
System.out.println("This is Derived class");
}
}
public class Hiding {
public static void main(String[] args) {
Base.showInfo();
Derived.showInfo();
}
}
We have two classes: class Derived extends Base {
public static void showInfo() {
System.out.println("This is Derived class");
}
}
The static class method Base.showInfo(); Derived.showInfo();
We call the $ java com.zetcode.Hiding This is Base class This is Derived class Java overriding methodsOverriding happens when we create an instance method of a derived class with the same signature and return type as an instance method in the base class. The method to be executed is determined at runtime. The determining of the method to be executed at runtime is called late or dynamic binding.
We might want to use the com/zetcode/Overriding.java
package com.zetcode;
class Base {
public void showInfo() {
System.out.println("This is Base class");
}
}
class Derived extends Base {
@Override
public void showInfo() {
System.out.println("This is Derived class");
}
}
public class Overriding {
public static void main(String[] args) {
Base[] objs = { new Base(), new Derived(), new Base(),
new Base(), new Base(), new Derived() };
for (Base obj : objs) {
obj.showInfo();
}
}
}
We create an array of the @Override
public void showInfo() {
System.out.println("This is Derived class");
}
Here we are overriding the Base[] objs = { new Base(), new Derived(), new Base(),
new Base(), new Base(), new Derived() };
Here we create an array of for (Base obj : objs) {
obj.showInfo();
}
We traverse the array and call $ java com.zetcode.Overriding This is Base class This is Derived class This is Base class This is Base class This is Base class This is Derived class
With the com/zetcode/Overriding2.java
package com.zetcode;
class Base {
public void showInfo() {
System.out.println("This is Base class");
}
}
class Derived extends Base {
@Override
public void showInfo() {
System.out.println("This is Derived class");
}
public void showBaseInfo() {
super.showInfo();
}
}
public class Overriding2 {
public static void main(String[] args) {
Derived d = new Derived();
d.showBaseInfo();
}
}
In the example, we call the public void showBaseInfo() {
super.showInfo();
}
Here we call the immediate parent's Java final methods
A com/zetcode/FinalMethods.java
package com.zetcode;
class Base {
public void f1() {
System.out.println("f1 of the Base");
}
public final void f2() {
System.out.println("f2 of the Base");
}
}
class Derived extends Base {
@Override
public void f1() {
System.out.println("f1 of the Derived");
}
// @Override
// public void f2() {
//
// System.out.println("f2 of the Derived");
// }
}
public class FinalMethods {
public static void main(String[] args) {
Base b = new Base();
b.f1();
b.f2();
Derived d = new Derived();
d.f1();
d.f2();
}
}
In this example we have a final method public final void f2() {
System.out.println("f2 of the Base");
}
The @Override
public void f1() {
System.out.println("f1 of the Derived");
}
In the // @Override
// public void f2() {
//
// System.out.println("f2 of the Derived");
// }
These lines are commented, because otherwise the code example would not compile. The compiler would give the following error: Exception in thread "main" java.lang.VerifyError: class com.zetcode.Derived overrides final method f2. d.f2();
Since it is not possible to override a final method, the above line will
call the $ java com.zetcode.FinalMethods f1 of the Base f2 of the Base f1 of the Derived f2 of the Base SourceJava defining methods - tutorial In this article we have covered Java methods. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Java tutorials. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |