GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||
GeezerVison is showing en.wikiversity.org — Java Programming/Classes - Wikiversity Programs in Java are composed of Classes. A class is an object with a specified set of fields, methods, static functions organized for modular use. AccessObjects in Java have four access levels:
Creating objectsAn object within Java is an instance of a class. To create an object, you use the new operator: { Employee em = new Employee(); } An object declaration, as shown above, is a reference to an existing object. If the reference changes, it doesn't directly change or destroy the object it is pointing to. ConstructorsA constructor is a function that executes on creation of a given class. It is responsible for initializing variables to the starting state, if required. public class Employee { /* A default constructor */ public Employee() { } /* Constructor with parameters */ public Employee(String name) { } } Constructors are usually declared as public. If they are protected or private, they cannot be directly created outside the class unless you provide a factory method, or allow developers to use a different constructor. Inheriting from classesJava classes can inherit features from a parent class by using the extends keyword. A class can only inherit from one parent class. As a classical example, you may see the class Employee: public class Employee { } In another file: public class Manager extends Employee { } If a child class uses a constructor, it can choose any constructor in the parent class by using the super keyword. This must be done within the first statement of the constructor. Child classes have full access to the parent's public and protected fields and methods. Anything private is exclusive to the parent class and cannot be removed directly. Abstract classesDiscussed later are abstract classes: public abstract class Employee { } Abstract classes are declared similarly to regular classes, but unlike normal classes, cannot be instantiated directly. They are only created through subclasses that implement all abstract methods. Multiple classes in a fileUnder normal java programming, classes are normally located within a file of their own. For example, the Employee class should be found in Employee.java, and the manager class should be found in Manager.java. In rare cases, you may have a reason to include both classes into a single file. There are two ways to do so. The first involves listing the classes one after another: public class Employee { } class Manager extends Employee { } In this style, only the first class file may be declared public. All following classes must use default access (where only classes in the same package may access it.) Nested classes have one class contained entirely in another, and may either be static or inner. A static class is not associated with the outer class instance, while an inner class maintains this association. public class Outer { private class Inner { } } In this context, the inner class is declared private, but it can still be accessed by the Outer class. Ouside the Outer class, it is not accessible.
| ||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |