Java can abstract classes have static methods

Abstract Methods and Classes

An abstract class is a class that is declared abstract —it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

abstract void moveTo(double deltaX, double deltaY);

If a class includes abstract methods, then the class itself must be declared abstract , as in:

public abstract class GraphicObject < // declare fields // declare nonabstract methods abstract void draw(); >

When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract .

Note: Methods in an interface (see the Interfaces section) that are not declared as default or static are implicitly abstract, so the abstract modifier is not used with interface methods. (It can be used, but it is unnecessary.)

Abstract Classes Compared to Interfaces

Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods. With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public. In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.

Which should you use, abstract classes or interfaces?

  • Consider using abstract classes if any of these statements apply to your situation:
    • You want to share code among several closely related classes.
    • You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
    • You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
    • You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
    • You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
    • You want to take advantage of multiple inheritance of type.

    An example of an abstract class in the JDK is AbstractMap , which is part of the Collections Framework. Its subclasses (which include HashMap , TreeMap , and ConcurrentHashMap ) share many methods (including get , put , isEmpty , containsKey , and containsValue ) that AbstractMap defines.

    An example of a class in the JDK that implements several interfaces is HashMap , which implements the interfaces Serializable , Cloneable , and Map . By reading this list of interfaces, you can infer that an instance of HashMap (regardless of the developer or company who implemented the class) can be cloned, is serializable (which means that it can be converted into a byte stream; see the section Serializable Objects), and has the functionality of a map. In addition, the Map interface has been enhanced with many default methods such as merge and forEach that older classes that have implemented this interface do not have to define.

    Note that many software libraries use both abstract classes and interfaces; the HashMap class implements several interfaces and also extends the abstract class AbstractMap .

    An Abstract Class Example

    In an object-oriented drawing application, you can draw circles, rectangles, lines, Bezier curves, and many other graphic objects. These objects all have certain states (for example: position, orientation, line color, fill color) and behaviors (for example: moveTo, rotate, resize, draw) in common. Some of these states and behaviors are the same for all graphic objects (for example: position, fill color, and moveTo). Others require different implementations (for example, resize or draw). All GraphicObject s must be able to draw or resize themselves; they just differ in how they do it. This is a perfect situation for an abstract superclass. You can take advantage of the similarities and declare all the graphic objects to inherit from the same abstract parent object (for example, GraphicObject ) as shown in the following figure.

    Classes Rectangle, Line, Bezier, and Circle Inherit from GraphicObject

    First, you declare an abstract class, GraphicObject , to provide member variables and methods that are wholly shared by all subclasses, such as the current position and the moveTo method. GraphicObject also declares abstract methods for methods, such as draw or resize , that need to be implemented by all subclasses but must be implemented in different ways. The GraphicObject class can look something like this:

    abstract class GraphicObject < int x, y; . void moveTo(int newX, int newY) < . >abstract void draw(); abstract void resize(); >

    Each nonabstract subclass of GraphicObject , such as Circle and Rectangle , must provide implementations for the draw and resize methods:

    class Circle extends GraphicObject < void draw() < . >void resize() < . >> class Rectangle extends GraphicObject < void draw() < . >void resize() < . >>

    When an Abstract Class Implements an Interface

    In the section on Interfaces , it was noted that a class that implements an interface must implement all of the interface’s methods. It is possible, however, to define a class that does not implement all of the interface’s methods, provided that the class is declared to be abstract . For example,

    abstract class X implements Y < // implements all but one method of Y >class XX extends X < // implements the remaining method in Y >

    In this case, class X must be abstract because it does not fully implement Y , but class XX does, in fact, implement Y .

    Class Members

    An abstract class may have static fields and static methods. You can use these static members with a class reference (for example, AbstractClass.staticMethod() ) as you would with any other class.

    Источник

    Can Abstract Classes have Static Methods in Java

    An abstract class in Java is one that explicitly uses the keyword «abstract» in its declaration. There are options for both non-abstract and abstract techniques (method with the body). A class that has been designated as being abstract is one that is such. Approaches can be both non-abstract and abstract. It needs to grow and have its plan implemented. It is not instantiable.

    Points to Remember:

    • The keyword abstract must be used when defining an abstract class.
    • It is capable of both abstract and non-abstract methods, but it cannot be instantiated.
    • It may also have static methods and constructors.
    • It may have final methods, which would prevent the subclass from changing the method body.

    Static Methods

    Methods that will be present whether or not any instances of the class are formed are created using the static keyword. A static method is one that makes use of the static keyword. Methods that are linked to a class rather than an object are known as static methods. The keyword static is used to declare them.

    Without generating a class instance, we can call static methods. Static methods are frequently used for tasks that are not specific to any one object, including validating an object’s type or performing mathematical calculations.

    Features of static method:

    • In Java, a static method is one that is a component of a class rather than an instance of that class.
    • The method is accessible to each instance of a class.
    • Without requiring the class’s object, static methods have access to class variables (static variables) (instance).
    • A static method can only access static data. It cannot access data that is dynamic (instance variables).
    • Static methods can be accessed directly in non-static methods as well as static methods.
    • A static method in Java cannot be abstract. Compilation errors will result from doing this.

    Example program to determine whether the abstract classes have static methods in java

    Java example application for an abstract static method

    import java.io. *; // A is super class abstract class A < // abstract static method func () has no body abstract static void func (); >//Bclass is sub class class B extends A < // func () method must override the class B static void func () < System.out.println("Static abstract"+ " method implemented."); >> // class is a driver class public class Demo < public static void main (String s[]) < // abstract class is called // static method func () B. func (); >> 

    Because static methods cannot be abstract, the code above is wrong. The Compilation Error that happens during execution is:

    Compilation Error: prog.java:12: error: illegal combination of modifiers: abstract and static abstract static void func(); ^ 1 error 

    What will happen if a static method is made abstract?

    Illustrating that we abstract a static method. The procedure will then be expressed as:

    public abstract static void func ();

    Step 1: Because the super-class does not specify how to implement a method when it is described as abstract using the abstract type of modifier, it falls to the subclass to do so. Therefore, in order to give method definition, a subclass must override them.

    Step 2: Since static members are compile-time components and overriding them turns them into runtime elements, it is now evident when a method is declared as static that it cannot be overridden by any subclass (making the static method hidden) (Runtime Polymorphism).

    Let’s now examine the first stage or process. The subclass is required to give a definition of the func method if it is referred to be abstract. The static func method, however, cannot be overridden in any subclass, according to step 2 of the procedure, hence it is unable to have a definition. Thus, the situations appear to be at odds with one another. Thus, our presumption that a static func method is abstract is incorrect. Consequently, a static method is unable to be abstract.

    This procedure will then be coded as:

    // Programming in Java to show an abstract static method import java.io. *; // A is a super class abstract class A < // func () is a static method static void func () < System.out.println("Static method implemented."); >//func1 is a abstarct method it has no body abstract void func1(); > //class B is sub class class B extends A < // class B must override func1() method void func1() < System.out.println("implemention of the abstract class is done."); >> // Driver class public class Demo < public static void main (String s[]) < // abstract class is called // static method func () B. func(); B b = new B (); b. func1(); >> 
    Static method implemented. implementation of the abstract class is done 

    Источник

    Читайте также:  Php check if can write file
Оцените статью