ComputersProgramming

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 a Java abstract 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

Suppose you need to develop several graphic elements, for example, geometric shapes: circle, rectangle, 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. But what about the paint method? After all, the parent class has no visual representation. If you declare the paint method in each class independently, you will need to analyze which component is being processed and then perform the cast. Therefore, the method should be declared abstract in the parent class: set the method header without the body. And each derived class will describe its body. In Java, an abstract class can encapsulate abstract methods.

If a class has abstract methods, then the 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 the 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 refer to 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.

Interfaces and abstract classes Java - the concepts are similar, but not the same. 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 keyword abstract is not required, since the interface is already abstract, but you can specify it.

Interface declaration

It starts with a header and can first be followed by the keyword public, then the word interface. Then the word extends and the enumeration of the interfaces from which this is inherited can go. Here, repetitions are not allowed, and it is also impossible for the inheritance relation to be cyclically dependent. 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 quite enough is said about the difference between the abstract class and 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 reference 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.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.atomiyme.com. Theme powered by WordPress.