Sie sind auf Seite 1von 3

1 JAVA CLASSES A class is defined in java using the class kwyword and a name by which it can be identified.

Syntax: class classname { // class signature } Example: The following block of code defines an Employee class. The class has a constructor called employee() and three properties, Name, Department and Designation. The class will be created using any editor (Eg. Notepad) and the filename will be same as the classname i.e employee.java class employee { String Name, Department, Designation; employee() { // method signature } } TYPES OF CLASSES IN JAVA 1. 2. 3. 4. Java has defined the following types of classes: Public class Private class Final class Abstract class

A class can be assigned a type, by including the type of modifies, just before the class keyword at the time of creating a class. Public class: The public modifies specifies that other objects outside the current package can use the class. By default when no access modifier is specified, classes can only be used within that package in which they are declared. By default, classes are public in the same package and private if a class is accessed from another package. A class can be declared as public by qualifying it with the public keyword. Syntax: public class <classname> { // class signature }

2 Private class: The private modifier specifies that other objects outside the current package cannot use the class. A private class has to be defined within another class. A private class has to be declared as private by qualifying it with the private keyword. Syntax: private class <classname> { // class signature } Final class: The final modifier specifies a class that can have no subclasses. Since final classes cant be subclassed, additional variables and methods cant be added, and more importantly, methods cant be overridden and implemented differently from the way the author of the class intended. To specify a class as final, the final keyword is used just before the keyword class. Syntax: final class <classname> { // class signature } Abstract class: An abstract modifier specifies a class that has atleast one abstract method in it. An abstract method is one that has no implementation. An abstract class cannot be instantiated. The purpose of having an abstract class is to declare methods, yet leave them unimplemented. The subclasses of an abstract class are required to provide implementation for the abstract methods. To specify a class as abstract, the abstract keyword is used just before the keyword class. Syntax: abstract class <classname> { // No class signature } Example: abstract class MyGraphics { public abstract void draw(); } If a class is declared as abstract, atleast one of the methods in that class must be abstract. The compiler sends out an error message and refuses to compile an abstract class that has no abstract methods in it.

3 SCOPE RULES The rules that dictate which parts of the program can see which variable are called scope rules. ACCESS MODIFIER Java has three access modifiers, which defines the scope of classes, variables and methods. 1. public 2. private 3. protected Public Access: The public access specifies that the variables of a class can be freely accessed from outside the class of the current package. By default when there is no access specifier, the class variables can only be used within that package in which they are declared. This can be done by using the public statement before declaring a variable or a method as follows: public <datatype> <variablename> = [value]; public <datatype> methodname(); By default the scope of a variable is public in the same package and private if variable is accessed from another package. Private Access: To completely hide a method or variable from being used by any other class, the private modifier is used. The only place these variables or methods can be seen is from within their own class. A variable can be declared as private by using the private statement before declaring a variable or a method as follows: private <datatype> <variablename> = [value]; private <datatype> methodname(); A private instance variable, for example, can be used by methods in its own class but not by methods of any other class. Private methods can be called by other methods in their own class but by no others. Protected Access: Another level of access control is to limit a method and variable to be used by subclasses of a class. This can be done by using the protected statement before declaring a variable or a method as follows: protected <datatype> <variablename> = [value]; protected <datatype> methodname(); Protected variable or methods can be freely accessed either in a subclass or any other class in the same package.

Das könnte Ihnen auch gefallen