PAGE-6

CHAPTER-2(Classes and Objects)                                   BOTTOM

 

CLASSES

Class :-In JAVA class is a special type of data type and is the basis of object oriented programming . .Class is used to create objects of its type. Class is a template of an object and object is an instance of the class. A typical structure of the class is as follows.

class classname
{
type instance variable1 ;
type instance variable 2 ;
type instance variable n ;


type methodname1(parameter list)
{
body of method
}
type methodname2(parameter list)
{
body of method
}
}

Explaination:-

In the above structure of the Class. The class is a keyword which is used to define a class and classname is any qualified name or identifier .The body of the class is defined within curly braces ,within the body of the class we declare the members of the class in the form of variables and methods.



Types of members:-


1. Instance members

2. Static members

Instance members:-

Instance members are those members of the class which are accessed through the object of the class. Each object has its own copy of the field and the methods defined in the class. The fields of an object an object are called instance variables which comprise the state of the class. The methods defined within the class comprise the behavior of the class. Instance variables and Instance methods collectively are called Instance members.

Example:-


class First {
int variable1=100 ;
int varible2= 200 ;
int varible3 = varible1+varible2;

void method sum()
{
System.out.println(“The value of varible3 is” +varible3);
}
First obj = new First();
obj.sum();
}


Output:- The value of varible3 is 300

In the above code obj is an object of First class and is created using the new operator.


Accessing the instance members:-


object.sum();

object.varible1;

Above we have used the object to access the instance variable and instance method using the "." operator.

 

Static members:-

In some situations members should only belong to the class and not be part of any object.An example of such a situation is that when we want to count the number of visitors accessing our website,in that case each object will have its own counter variable ,so which object’s counter varible should be updated.The solution of this problem is to declare that variable as static.

Static variables are initialized when the class is loaded at runtime.We can also declare methods as static,static variables and methods collectively are collectively known as static members.

As in the previous example we can declare variable1 and avariable2 varibles as static.

Example :-
class First {
static int variable1=100 ;
static int varible2= 200 ;
static int varible3 = varible1+varible2;
static void method sum()
{
System.out.println(“The value of varible3 is” +varible3);
}
// First object = new First();
First.sum();
First.variable;
}

Accssing the static members.

Fisrt.sum() ;

First.varible1;

We access the static members of the class through the name of the class .As it is clear from the above example that the class First is used to access the variable variable1 and method sum() .

 

Methods(Functions)

A method in java is a function which is used to define the behaviour of the class.A function uses the varibles defined in the class and can change the state maintained by the variables.Objects communicate by message passing. This means that an class can provide a particular behavior by invoking the appropriate operation on the object. In Java, this is done by calling a method on the object using the binary infix dot '.' operator. A method call spells out the complete message: the object that is the receiver of the message, the method to be invoked, and the arguments to the method, if any. The method invoked on the receiver can also send information back to the sender, via a return value. The method called must be one that is defined for the object.

CharStack stack = new CharStack(5); // Create a stack
stack.push('J'); // (1) Character 'J' pushed
char c = stack.pop(); // One character popped and returned: 'J'
stack.printStackElements();

Methods could be instance methods as well as static methods.In java we use two types of method

1.System defined methods:-

2.User defined methods:-

System defined methods:-System defined methods are those methods which are already defined in java library and we do not need to define them in our program,we only call them through objects of the class if they are instanse methods , if the method is defined as static in the inbuilt class then we will call it through class name or object reference variable.

User defined methods:-User defined methods are those methods which are not defined in the java library and we define them in our program, and then we call them through objects of the user defined class if they are instanse methods , if the method is defined as static in the user defined class then we will call it through class name or object reference variable.

 

 

Constructor:-

Contructor is a special type of function, which is defined within class. The purpose of constructor is set the initial state of an object (eg. initialising the variables) when the object is created by using the new operator.

Features of contructor:-


1.Constructor is used to initialize the state of the class.
2. Construtor doesn’t return any value not even void.
3.Constructor has the same name as class in which it is defined.
4.Constructor is called when the object is created using the new operator.


Example:-


class ConstructorDemo{

int a , b;
ContructorDemo()     //Constructor {
a =100;
b =200;

}

Calling of constructor:-


ConstrutorDemo demo = new ConstrutorDemo();


We have called the constructor by calling the constructor name after the new operator and the values of a and b are initialized with 100 and 200 respectively.
In the above code we have called default constructor,which is called without parameter.


Default constructor can be of two types.


1.Implicit default constructor.
2.Explicit default constructor.


Implicit default constructor is called when we do not declare it in our class and call it.

Explicit default constructor is called when we declare it in out class and then call it.

Constructors can also be parameterized like any common method. When we define multiple constructors in our class this is called constructor overloading.

 

 


Accessibility modifiers:--


By specifying accessibility modiier a class can control what information is accessible to client. Accessibility modifiers are used to define the scope of the members of the class Accessibility modifiers used with the members of the class are as follows.

Public modifier:-
Public modifier is the least restrictive of all the accessibility modifiers.A public member is accessible from anywhere in the application.It means that is accessible both in the package containing its class and in other packages where this class is visible.

Protected members:-
A protected member is accessible in all classes in the package containing its class and by all subclasses of its class in any package .It means that non subclasses in other packages cannot access protected members in other packages.

Default accessibility modifiers:-
When no members accessibility modifier is specified,the member is only accessible by other class itself in which it is defined and the other class within the same package in which it’s class.It will not be accessible in other packages even if it’s class is accessible in those packages.Dafault modifier is also called package accessibility modifier and I more restrictive than protected accessibility modifier.

Private members:-
This is the most restrictive of all the accessibility modifiers. Private members are only accessible within the class in which they are defined. They are not accessible in any other class and this is also applicable to subclasses.

An Access Example

The following example shows all combinations of the access control modifiers. This example has two packages and five classes. Remember that the classes for the two different packages need to be stored in directories named after their respective packages—in this case, p1 and p2.

The source for the first package defines three classes: Protection, Derived, and SamePackage.

The first class defines four int variables in each of the legal protection
modes. The variable n is declared with the default protection, n_pri is private, n_pro is protected, and n_pub is public.

Each subsequent class in this example will try to access the variables as an instance of this class. The lines that will not compile due to access restrictions are commented out by use of the single-line comment //. Before each of these lines is a comment listing the places from which this level of protection would allow access.

The second class, Derived, is a subclass of Protection in the same package, p1. This grants Derived access to every variable in Protection except for n_pri, the private one.

The third class, SamePackage, is not a subclass of Protection, but is in the same package and also has access to all but n_pri.

This is file Protection.java:

CODE:-

package p1;
public class Protection {
int n = 1;
private int n_pri = 2;
protected int n_pro = 3;
public int n_pub = 4;
public Protection() {
System.out.println("base constructor");
System.out.println("n = " + n);
System.out.println("n_pri = " + n_pri);
System.out.println("n_pro = " + n_pro);
System.out.println("n_pub = " + n_pub);
}
}


This is file Derived.java:

package p1;
class Derived extends Protection {
Derived() {
System.out.println("derived constructor");
System.out.println("n = " + n);
// class only
// System.out.println("n_pri = " + n_pri);
System.out.println("n_pro = " + n_pro);
System.out.println("n_pub = " + n_pub);
} }


This is file SamePackage.java:
package p1;
class SamePackage {
SamePackage() {
Protection p = new Protection();
System.out.println("same package constructor");
System.out.println("n = " + p.n);
// class only
// System.out.println("n_pri = " + p.n_pri);
System.out.println("n_pro = " + p.n_pro);
System.out.println("n_pub = " + p.n_pub);
}
}


Following is the source code for the other package, p2. The two classes defined in p2 cover the other two conditions which are affected by access control. The first class, Protection2, is a subclass of p1.Protection. This grants access to all of p1.Protection’s variables except for n_pri (because it is private) and n, the variable declared with the default protection. Remember, the default only allows access from within the class or the package, not extra-package subclasses.

Finally, the class Other Package has access to only one variable, n_pub, which was declared public.


This is file Protection2.java:

package p2;
class Protection2 extends p1.Protection {

Protection2() {
System.out.println("derived other package constructor");
// class or package only
// System.out.println("n = " + n);
// class only
// System.out.println("n_pri = " + n_pri);
System.out.println("n_pro = " + n_pro);
System.out.println("n_pub = " + n_pub);
}
}

This is file OtherPackage.java:

package p2;
class OtherPackage {
OtherPackage() {
p1.Protection p = new p1.Protection();
System.out.println("other package constructor");
// class or package only
// System.out.println("n = " + p.n);
// class only
// System.out.println("n_pri = " + p.n_pri);
// class, subclass or package only
// System.out.println("n_pro = " + p.n_pro);
System.out.println("n_pub = " + p.n_pub);
}
}

If you wish to try these two packages, here are two test files you can use. The one for package p1 is shown here:

Demo.java in package p2

// Demo package p1.
package p1;
// Instantiate the various classes in p1.
public class Demo {
public static void main(String args[]) {
Protection ob1 = new Protection();
Derived ob2 = new Derived();
SamePackage ob3 = new SamePackage();
}
}

Demo.java in package p2
The test file for p2 is shown next:
// Demo package p2.
package p2;
// Instantiate the various classes in p2.
public class Demo {
public static void main(String args[]) {
Protection2 ob1 = new Protection2();
OtherPackage ob2 = new OtherPackage();
}
}


 


Note :
After Successful completion of Training Candidate will be provided with Project Report and Training Certificate.

Home  |   FeedBack  |   Terms of Use  |   Contact Us  |   Report Error
                                                                            Copyright 2009 R.M Infotech (P) Ltd.                                             Designed by: Raman