Thursday, April 2, 2009

Class Declaration

Class Declaration

Source File Declaration Rules

1. There can be only one public class per source code file.
2. Comments can appear at the beginning or end of any line in the source code
file.They are independent of any of the rules explained here.
3. If there is a public class in a file, the name of the file must be same as the name
of the public class. For example, a class declared as public class Cat { }
must be in a source code file named Cat.java.
4. If the class is part of a package, then package statement must be the first line
in the source code file, before any import statements that may be present.
5.If there are import statements, they must go between the package statement
and the class declaration. If there isn't a package statement,
then the import statement(s) must be the first line(s) in the source code file.
If there are no package or import statements, the class declaration must be
the first line in the source code file.
6. import and package statements apply to all classes within a source code file.
In other words, there's no way to declare multiple classes in a file and have
them in different packages, or use different imports.
7.A file may contain more than one nonpublic class.
8. Files with no public classes can have a name that does not match any of the
classes in the file.

Class Declarations and Modifiers

The class can be declared by the following code

class MyFirstClass { }
{}
This code compiles fine, but you can also add modifiers before the class
declaration. Modifiers fall into two categories:
1. Access modifiers: public, protected, private.
2. Non-access modifiers (including strictfp, final, and abstract).

Class Access
When we say code from one class (class A) has access to another class (class B), it means class A can do one of three things:
1. Create an instance of class B.
2. Extend class B (in other words, become a subclass of class B).
3. Access certain methods and variables within class B, depending on the access
control of those methods and variables.

Default Access A class with default access has no modifier preceding it in the
Declaration.A class with default access can be seen only by classes within the same package.
For example, if class X and class Y are in different packages, and class X has default access, class Y
won't be able to create an instance of class , or even declare a variable or return
type of class X.


package drink;
class Cold { }
Now look at the second source file:
package exam.stuff;
import drink.Cold;
class Tea extends Cold { }

The superclass (Cold) is in a different package from the
subclass (Tea). The import statement at the top of the Tea file is trying (fingers
crossed) to import the Cold class. The Cold file compiles fine, but when we
try to compile the Tea file we get something like:

Can't access class cert.Beverage. Class or interface must be
public, in same package, or an accessible member class.
import drink.Cold;

Public Access A class declaration with the public keyword gives all classes
from all packages access to the public class. In other words, all classes in the Java
have access to a public class.

package cert;
public class Beverage { }

Other (Nonaccess) Class Modifiers
You can modify a class declaration using the keyword final, abstract, or
strictfp. These modifiers are in addition to whatever access control is on the class,
so you could, for example, declare a class as both public and final. But you can't
always mix nonaccess modifiers. strictfp can be used in combination with
final, for example, but you must never, ever, ever mark a class as both final and
abstract.

Final Classes When used in a class declaration, the final keyword means
the class can't be subclassed. In other words, no other class can ever extend (inherit
from) a final class, and any attempts to do so will give you a compilation error. Use this final for safety purpose. Marking a class final means, in essence, your class can't ever be Extended , or even specialized, by another programmer.


Let's modify our Beverage example by placing the keyword final in the
declaration:
package cert;
public final class Beverage {
public void importantMethod() { }
}

Now, if we try to compile the Tea subclass:
package exam.stuff;
import cert.Beverage;
class Tea extends Beverage { }

We get an error something like
Can't subclass final classes: class
cert.Beverage class Tea extends Beverage{
1 error

Abstract Classes An abstract class can never be instantiated. It is always extended.

Take a look at the following abstract class:
abstract class Bike {
private double price;
private String model;
private String year;
public abstract void goFast();
public abstract void goUpHill();
public abstract void impressNeighbors();
}

The preceding code will compile fine. However, if you try to instantiate a Bike in
another body of code, you'll get a compiler error something like this:
AnotherClass.java:7: class Bike is an abstract
class. It can't be instantiated.
Bike x = new Bike();
1 error

Notice that the methods marked abstract end in a semicolon rather than
curly braces.

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP