Java Programming/Classes
< Java Programming
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.
Objects in Java have four access levels:
Creating objects
An 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.
A 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 classes
Java 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 classes
Discussed 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 file
Under normal java programming, classes are normally located within a file of their own. For example, the Employee class should be found in, and the manager class should be found in 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.

Project: Introduction to Programming in Java
Previous: Boolean variables — Java Programming/Classes — Next: Inheritance

Last edited on 24 September 2020, at 02:27
Content is available under CC BY-SA 3.0 unless otherwise noted.
Privacy policy
Terms of Use
HomeRandomLog inSettingsDonateAbout WikiversityDisclaimers