
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Why can't a Java class be both abstract and final?
Abstract class
A class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract.
If you want to use the concrete method in an abstract class you need to inherit the class, provide implementation to the abstract methods (if any) and then, you using the subclass object you can invoke the required methods.
Example
In the following Java example, the abstract class MyClass contains a concrete method with name display.
From another class (AbstractClassExample) we are inheriting the class MyClass and invoking the its concrete method display using the subclass object.
abstract class MyClass { public void display() { System.out.println("This is a method of abstract class"); } } public class AbstractClassExample extends MyClass { public static void main(String args[]) { new AbstractClassExample().display(); } }
Output
This is a method of abstract class
Final class
If you declare a class final you cannot extend if you try to extend a final class, a compile time error will be generated saying “cannot inherit from final SuperClass”
Example
In the following Java program, we have a final class with name SuperClass and we are trying to inherent it from another class (SubClass).
final class SuperClass { public void display() { System.out.println("This is a method of the superclass"); } }
Compile time error
On compiling, this program generates a compilation error as shown below −
SubClass.java:7: error: cannot inherit from final SuperClass public class SubClass extends SuperClass{ ^ 1 error
Both abstract and final
If you declare a class abstract, to use it, you must extend it and if you declare a class final you cannot extend it, since both contradict with each other you cannot declare a class both abstract and final if you do so a compile time error will be generated.
Example
public final abstract class Example { public void sample() { } }
Compile time error
Example.java:1: error: illegal combination of modifiers: abstract and final public final abstract class Example { ^ 1 error
- Related Articles
- Can a class in Java be both final and abstract?
- Why can't static method be abstract in Java?
- Can constructors be marked final, abstract or static in Java?
- Can a final class be subclassed in Java?
- Why Abstract Class is used in Java?
- Why Final Class used in Java?
- Why an interface doesn't have a constructor whereas an abstract class have a constructor in Java?
- Can we declare an abstract method final or static in java?
- Can we define an abstract class with no abstract methods in Java?
- Why a constructor cannot be final in Java?
- Can we define an abstract class without abstract method in java?\n\n
- Why we can't initialize static final variable in try/catch block in java?
- Abstract class in Java
- Why constructor cannot be final in Java
- Can a constructor be made final in Java?
