A Closer Look at Methods and Classes: Deependra Rastogi
A Closer Look at Methods and Classes: Deependra Rastogi
and Classes
Deependra Rastogi
Assistant Professor
Galgotias University, Greater Noida
Overloading Methods
In Java it is possible to define two or more methods within the same
class that share the same name, as long as their parameter
declarations are different. When this is the case, the methods are said
to be overloaded, and the process is referred to as method
overloading. Method overloading is one of the ways that Java supports
polymorphism.
class OverloadDemo
{
void test()
{
System.out.println("No parameters");
}
void test(int a, int b)
{
System.out.println("a and b: " + a + " " + b);
}
void test(double a)
{
System.out.println("Inside test(double) a: " + a);
}
}
Overloading Methods
class Overload
{
public static void main(String args[])
{
OverloadDemo ob = new OverloadDemo();
int i = 88;
ob.test();
ob.test(10, 20);
A class may be declared with the modifier public, in which case that class
is visible to all classes everywhere. If a class has no modifier (the default,
also known as package-private), it is visible only within its own package.
At the member level, you can also use the public modifier or no modifier
(package-private) just as with top-level classes, and with the same
meaning.
Access Levels
Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N
The first data column indicates whether the class itself has access to the
member defined by the access level. As you can see, a class always has
access to its own members. The second column indicates whether classes
in the same package as the class (regardless of their parentage) have
access to the member. The third column indicates whether subclasses of
the class declared outside this package have access to the member. The
fourth column indicates whether all classes have access to the member.
Introducing Access Control
Access levels affect you in two ways. First, when you use classes that
come from another source, such as the classes in the Java platform,
access levels determine which members of those classes your own classes
can use. Second, when you write a class, you need to decide what access
level every member variable and every method in your class should have.
Let's look at a collection of classes and see how access levels affect
visibility. The following figure shows the four classes in this example and
how they are related.
Introducing Access Control
The following table shows where the members of the Alpha class are
visible for each of the access modifiers that can be applied to them.
Visibility
Modifier Alpha Beta Alphasub Gamma
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N
Introducing Access Control
/* This program demonstrates the difference between public and
private.*/
class Test
{
int a; // default access
public int b; // public access
private int c; // private access
// methods to access c
void setc(int i) // set c's value
{
c = i;
}
int getc()
{ // get c's value
return c;
}
}
Introducing Access Control
class AccessTest
{
public static void main(String args[])
{
Test ob = new Test();
ob.setc(100); // OK
System.out.println("a, b, and c: " + ob.a + " " +
ob.b + " " + ob.getc());
}
}
Understanding static
There will be times when you will want to define a class member that will
be used independently of any object of that class. Normally, a class
member must be accessed only in conjunction with an object of its class.
However, it is possible to create a member that can be used by itself,
without reference to a specific instance. To create such a member,
precede its declaration with the keyword static. When a member is
declared static, it can be accessed before any objects of its class are
created, and without reference to any object. You can declare both
methods and variables to be static. The most common example of a static
member is main( ). main( ) is declared as static because it must be called
before any objects exist.
classname.method( )
Here, classname is the name of the class in which the static method is
declared. As you can see, this format is similar to that used to call non-
static methods through object-reference variables. A static variable can be
accessed in the same way—by use of the dot operator on the name of the
class. This is how Java implements a controlled version of global methods
and global variables.
Understanding static
class StaticDemo
{
static int a = 42;
static int b = 99;
static void callme()
{
System.out.println("a = " + a);
}
}
class StaticByName
{
public static void main(String args[])
{
StaticDemo.callme();
System.out.println("b = " + StaticDemo.b);
}
}