public class Test { public static void test() { print(); } public static void print() { System.out.println("Test"); } public void print() { System.out.println("Another Test"); } }
A java blog with a collection of examples and tutorials on Java and related technologies. (Under maintenance with continuous updates) Be in touch with java jazzle or k2java.blogspot.com.
Showing posts with label overloading. Show all posts
Showing posts with label overloading. Show all posts
Friday, February 18, 2011
Method overloading,overriding and static
Overloading vs Overriding
In case of overloading in java, it doesn't matter what is the return type of the method. What matters is the name of the method should be same, and their signatures should be different. But in case of overriding even there return types must match.
Which of the following is a legal return type of a method overloading the following
method:
public void add(int a) {…}
A. void
B. int
C. Can be anything
Select the most appropriate answer.(Answer you know is C)
method:
A. the overriding method must return voidpublic void add(int a) {…}
B. the overriding method must return int
C. the overriding method can return whatever it likes
Select the most appropriate answer.(answer you know is A)
Sunday, December 13, 2009
Functions or Methods in java
Class behaviour are represented in Java by methods. To declare a method use the following syntax:
static methods are shared by all members and exist for all runtime. Static methods can be referenced without creating an instance of the class. abstract methods must be redefined on inheritance. native methods are written in C but accessible from Java.
The return_data_type defines the type of value that the calling routine receives from the object (the reply message in object terminology). It can be any of the primitive types or the reserved word void (default value) if no message is to be returned. The statement return varName; is used to declare the value to be returned to the calling routine.
The parameter_list can contain from zero to many entries of datatype varName pairs. Entries are separated by commas. Parameters are passed by value, thus upholding the encapsulation principle by not allowing unexpected changes or side effects. Object references (such as arrays) can also be passed.
Some examples of method header parameter lists are:
Note: Differences from cpp
Types of methods
Pass by value or reference
[ "public" | "private" | "protected" ] [ "final" ] [ "static" | "abstract" | "native" ] return_data_type method_name "(" parameter_list ")" "{" // some defining actions "}"Accessibility keywords are the same as for properties. The default (ie. omitted) is package (aka friendly) or visible within the current package only.
static methods are shared by all members and exist for all runtime. Static methods can be referenced without creating an instance of the class. abstract methods must be redefined on inheritance. native methods are written in C but accessible from Java.
The return_data_type defines the type of value that the calling routine receives from the object (the reply message in object terminology). It can be any of the primitive types or the reserved word void (default value) if no message is to be returned. The statement return varName; is used to declare the value to be returned to the calling routine.
The parameter_list can contain from zero to many entries of datatype varName pairs. Entries are separated by commas. Parameters are passed by value, thus upholding the encapsulation principle by not allowing unexpected changes or side effects. Object references (such as arrays) can also be passed.
Some examples of method header parameter lists are:
public static void example1() {} public static int add2(int x) {x+=2; return x;} public static double example3(int x, double d) {return x*d;} public static void example4(int x, int y, boolean flag) {} public static void example5(int arr[]) {} // note: this is object
Note: Differences from cpp
Types of methods
Pass by value or reference
Labels:
core-java,
cpp vs java,
encapsulation_classes,
functions,
java,
Method (computer science),
native method,
OOP / Object Oriented Programming,
overloading,
recursion,
templates,
varargs
Subscribe to:
Posts (Atom)