In the scenario of method overloading. The access modifier of methods can be less restrictive or more accessible. There are two methods eat() and sleep(). One is public and other is protected in class P. You see there are restrictive and more accessible version of methods.
class P
{
public void eat() {}
protected void eat(int a) {}
protected void sleep() {}
private void sleep(String s) {}
public void sleep(String s, float x) {}
}
public class OverloadingAccessTest
{
public static void main(String args[])
{
}
}
Showing posts with label overloading. Show all posts
Showing posts with label overloading. Show all posts
Sunday, April 9, 2017
Saturday, April 8, 2017
Overloading - Argument Lists
In this section we will talk about the argument lists in method overloading. Method overloading can be achieved if method signature is different. It can be different number of parameters, data types or sequence but return type doesn't matter. In our example we created a program for some simple calculation.
method 2: three parameters (three int)
method 3: two parameters (one int, one float)
method 4: two parameters (one int, one float) but in different sequence
class A
{
public static int method1(int x, int y) {return x+y;}
public static int method2(int x, int y, int z) {return x+y+z;}
public static float method3(int x, float y) {return y-x;}
public static float method4(float x, int y) {return x-y;}
}
public class OverloadingTest1
{
public static void main(String args[])
{
System.out.println("method 1: "+A.method1(5,9));
System.out.println("method 2: "+A.method2(4,7,2));
System.out.println("method 3: "+A.method3(5,14.2f));
System.out.println("method 4: "+A.method4(7.8f,2));
}
}
Result:
method 1: 14
method 2: 13
method 3: 9.2
method 4: 5.8
Subscribe to:
Posts (Atom)
Abstraction vs Interface
Abstract Interface method abstract and concrete abstract only, support default and static method ...
-
Single inheritance is the most simple form among five types of inheritance. It requires one superclass and one subclass. Examples: Dog I...
-
In this section we will talk about the argument lists in method overloading. Method overloading can be achieved if method signature is diff...