Java Programming Language Class, Interface (Class vs Interface)

Java Programming Language:

Classes vs. Interfaces


  • What is a class?Classes in Java are almost the same as C++ classes, in that they define an abstract datatype, with its particular fields and methods. Each object is an instance of a class, and follows the class prototype that defines the variables and methods common to all objects of a certain kind. Each instance of a class must be instantiated, after it is declared, unlike in C++. This is usually done with the keyword "new".
  • Classes may have inheritance from other classes, as they do in C++, meaning they incherit all the properties of the parent class. This is usually done with the keyword "extends". So for example, a simple sub-class declaration with inheritance from another class might look like this:
  • class Window extends Frame {}
    The new class is window which contains all the properties of a Frame. Mind you, sub-classes are not limited to the properties they inherit, they may add variables and methods of their own, and can even override inherited methods as well. Each class must have its own header file, which is included at the beginning of the code with an "import". Classes are what provide Java its modularity, in that multiple objects may be created as instances of the same class.
  • What is an abstract class?An abstract class is like a class, but differs in that it only defines "generic" methods in the class, but does not actually implement them. Those methods will be implemented later by specialized sub-classes. This is how we get interfaces - interfaces are the implementation of the abstract class.
  • What is an interface?An interface, or protocol as it is sometimes called, is a device that is used to allow unrelated objects to interact with one another, by implementing an agreed upon system of behavior. When a class implements an interface, the class agrees to implement all of the methods defined in the interface. Interfaces are useful since they capture similiarity between unrelated objects without forcing a class relationship. Furthermore, interfaces may consist of either abstract methods or entire abstract classes. One class uses an interface by using the "implements" keyword, and example might look like this:
    class Window implements ActionListener {}
    Furthermore, a class may have both inheritance and an interface. For instance, a window may be both a Frame, and an interface to ActionListener. Java supports multiple interfaces, so long as the class implements all of the functions defined in the interface. Unfortunately, because of this rule, interfaces are very diificult to expand, since it requires implementing each new function in every instance of the interface.

Comments

Popular posts from this blog

How to Create an Issue in JIRA (2)

Fundamentals of Accessibility Testing ... 2