Elements of the Java object model: abstract class, interface
The main idea of the abstract class lies in the following thesis: sometimes not ready classes are required, but in a "raw" form. Such blanks can not be used directly (create instances).
What is the abstract class in Java?
Let's consider another example. There is in Java an abstract Calendar class in the Java.util package. It does not implement a specific calendar that is used, for example, in Western and Eastern Europe, China, North Korea, Thailand, etc. But it has many useful functions, for example, adding a few days to a specific date: these functions are required for any implementation of the calendar. You can not spawn an instance from an abstract class.
Abstract classes, Java abstract methods
Let's say you need to develop several graphicelements, for example, geometric figures: a circle, a rectangle, a star, etc. And there is a container that draws them. Each component has a different appearance, so the corresponding method (let it be called paint) is implemented differently. However, each component has many common features: the figures must be inscribed in a rectangle, can have a color, be visible and invisible, etc. That is, you need to create a parent class for all these shapes, where each component will inherit common properties.
If a class has abstract methods, thenthe class is abstract. Before the word class is put the keyword abstract, in the header of the method - too. After the head of this method, you need to put a semicolon. In Java, an abstract class can not spawn instances. If we want to prohibit their creation, even if the class does not have abstract methods, then the class can be declared abstract. But if a class has at least one abstract method, then the class must be abstract. It is impossible for the class to be abstract, final, and method, too. The method can not be abstract, private, static, native. In order for the heir classes to be declared non-abstract and create their instances, they must implement all of the parent's abstract methods. The class itself can use its abstract methods.
Example:
- abstract class AClass {
- public abstract void method (int a);
- }
- class BClass extends AClass {
- public void method (int a) {
- // body
- }
Variables of the abstract class type are allowed. They can reference the non-abstract descendant of this class or be null.
Interfaces in Java - an alternative to multiple inheritance
Java does not have multiple inheritance, because then there are certain problems. A class can not inherit from several classes. But it can implement several interfaces.
Java Interfaces and Abstract Classessimilar but not identical. The interface can be declared as public, then it is accessible to everyone, or you can not specify the modifier public, then the interface is available only within its package. The abstract keyword is not required, since the interface is already abstract, but you can specify it.
Interface declaration
It starts with a header and can go firstthe keyword public, then the word interface. Then there can be the word extends and the enumeration of the interfaces from which this is inherited. There are no repetitions allowed, and it is also impossible for the inheritance relation to be cyclical. Then comes the interface body, enclosed in braces. Element fields are declared in the interface body: constant fields and abstract methods. All fields are public final static - all these modifiers are optional. All methods are considered public abstract - these modifiers can also be specified. Now it is enough enough said about the difference of the abstract class from the Java interface.
- public interface AI extends B, C, D {
- // body
- }
To declare a class as the heir of the interface, you need to use the keyword implements:
- class AClass implements BI, CI, DI {}
That is, if the name of the interface is specified in the class declaration after implements, then the class implements it. The heirs of this class inherit its elements, so they also implement it.
Variables of the interface type are also allowed. They can refer to the type of the class that implements this interface, or null. Such variables have all the elements of the Object class, because objects are generated from classes, and those in turn are inherited from the Object class.
In this article, we looked at some elements of the Java object model - abstract classes, abstract methods, interfaces.