Different Definitions of Classes in Java

Different Definitions of Classes in Java thumbnail
The definitions of different Java classes help make code more modular.

Object oriented languages, such as Java, are based around classes. Classes are a kind of blue print the Java Virtual Machine uses to create data structures called objects. A programmer can create several objects, also called instances, from the code for a single class. When a programmer is typing out the code for a given class, there are a number of different definitions he can use for a variety of purposes.

  1. Constructors

    • A constructor is a method that Java runs when a new instance of a class is created. Programmers can use these to process the data the programmer passed into the class when creating a new instance object of the class, also known as parameters. Programmers can create a number of different constructors for the same class, each with a different set of parameters that the program might pass into the class when creating a new object. Java will automatically execute the constructor whose parameter scheme matches the data being passed in during the creation of a new object, allowing for a number of slightly different definitions for the same class.

    Sub-Classes

    • Java's object oriented structure allows classes to "inherit" data from other classes. This means that Java will automatically transfer the methods, variables, and other data from one class to another class that "extends" it without the programmer having to type out this information again. This is useful when the programmer wants to create a more specialized version, or subclass, of the class from which it will inherit data -- also called the superclass -- without rewriting the superclass. Java developers can define a class as a subclass of a superclass.

    Abstract Class

    • When a programmer is writing a superclass that will have a number of subclasses, he might define the object as "abstract." This means that the object is only there to provide the data for inheritance to the subclasses that extend it. When a programmer declares a class as abstract, he can create instances of the classes' subclasses but cannot create an instance of the abstract class itself. Programmers can define a class as abstract by withholding the term "public" from the class declaration line.

    Interfaces

    • When a class is going to be interacting with another software product through its application programming interface, or the functionality of another library, interfaces define the method to do that. This acts as a kind of agreement between the class and the API or library that they know how to communicate with one another. Class definitions use interfaces by including the term "implements (interface name)" in the declaration line. This tells Java to check the classes' methods to ensure that all the necessary ones are present, and the definition is necessary for Java to allow the class to interact with the library or API without producing a compilation error.

Related Searches:

References

  • Photo Credit Comstock Images/Comstock/Getty Images

Comments

Related Ads

Featured