• You can declare some or all of a class's methods final.


  • You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses.


  • You might wish to make a method final if it has an implementation that should not be changed and it is critical to the consistent state of the object.


  • For example, you might want to make the getName method in this Sample class final:


  •  
    class Sample {
      
        String name;
        final String getName(String input) {
            name=input;
            return name;
        }
    
    
    }
    
    
    
  • Methods called from constructors should generally be declared final.


  • If a constructor calls a non-final method, a subclass may redefine that method with surprising or undesirable results.


  • Note that you can also declare an entire class final.


  • A class that is declared final cannot be subclassed.


  • This is particularly useful, for example, when creating an immutable class like the String class.


  • 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 {
       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 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.


  • Consider using abstract classes if any of these statements apply to your situation:


    1. You want to share code among several closely related classes.


    2. 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).


    3. 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.


  • Consider using interfaces if any of these statements apply to your situation:


  • You expect that unrelated classes would implement your interface.


  • 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.


  • 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 GraphicObjects 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


  • 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.