A
simple introduction of Class
A
class in java is nothing but a blueprint or a template for creating
different objects which defines its properties and behaviors. Java class
objects exhibit the properties and behaviors defined by its class. A class can
contain fields and methods to describe the behavior of an object. Generally the
fields are data member and methods are function member. So in a single formula.
Class
= data + methods.
Everything (data and methods) in Java is contained in classes, Even the
main method in java is defined within some class. Classes can also be used to
store data. Historically, programming languages had something similar to
classes for grouping data, usually called structs, Union or records in c
and C++ family languages. These were used for storing only data, not
methods.(This is the main disadvantage of structure and union).We can derive
one class from another class.
Diagram
of simple class and subclass.
Example
of a simple class
class
Bicycle {
int cadence = 0;
int speed = 0;
int gear = 1;
void changeCadence(int newValue) {
cadence = newValue;
}
void changeGear(int newValue) {
gear = newValue;
}
void speedUp(int increment) {
speed = speed + increment;
}
void applyBrakes(int decrement) {
speed = speed - decrement;
}
void printStates() {
System.out.println("cadence:" +
cadence + " speed:" +
speed + " gear:" + gear);
}
}
Here
bicycle is a class, Having three data member and five function member. In java
by default the data member are public .Here public is nothing but access
specifier , and control the access mechanism and invocation operation. In this
above example as the data and methods are declared both within same class then
method can access these data easily.
Access
Specifiers of class
Generally
there are three types of access specifier in java class
1)Public
2)Protected
3)Private
Public
mode
public
classes, methods, and fields can be accessed from everywhere. The only
constraint is that a file with Java source code can only contain one public
class whose name must also match with the filename. If it exists, this public
class represents the application or the applet, in which case the public
keyword is necessary to enable your Web browser or appletviewer to show the
applet. You use public classes, methods, or fields only if you explicitly want
to offer access to these entities and if this access cannot do any harm. An
example of a square determined by the position of its upper-left corner and its
size:
public
class Square { // public class
public x, y, size; // public instance variables
}
Generally
the methods are declared as publicly, So that from any class it’s possible to
invoke.
Protected
mode
protected methods and fields can only be
accessed within the same class to which the methods and fields belong, within
its subclasses, and within classes of the same package, but not from anywhere
else. You use the protected access level when it is appropriate
for a class's subclasses to have access to the method or field, but not for
unrelated classes. Example of a protected data member is.
Class
hello
{
Protected void fun()
{
System.Out.Println(“Hello function”);
}
}
This
protected method only accessible from it’s subclass.
Private
mode
private
methods and fields can only be accessed within the same class to which the
methods and fields belong. private methods and fields are not visible within
subclasses and are not inherited by subclasses. So, the private access
specifier is opposite to the public access specifier. It is mostly used for
encapsulation: data are hidden within the class and accessor methods are
provided. An example, in which the position of the upper-left corner of a
square can be set or obtained by accessor methods, but individual coordinates
are not accessible to the user.
public
class Square { // public class
private double x, y // private (encapsulated) instance variables
public setCorner(int x, int y) { // setting values of private fields
this.x = x;
this.y = y;
}
public getCorner() { // setting values of
Access Specifiers for Class
Variables and Class Methods
Below
is a table showing the effects of access specifiers for class members
(i.e. class variable and class methods).
◆ = Can Access. ◇ = No Access.
Specifier
|
class
|
subclass
|
package
|
world
|
private
|
◆
|
◇
|
◇
|
◇
|
protected
|
◆
|
◆
|
◆
|
◇
|
public
|
◆
|
◆
|
◆
|
◆
|
(none)
|
◆
|
◇
|
◆
|
◇
|
No comments:
Post a Comment