Java / Abstract Class
Refers to the class with complete implementation i.e. no abstract methods and only concrete methods.
No, abstract keyword is applicable only for classes and methods.
A class that has only partial implementation and has abstract methods.
No. Abstract classes cannot be instantiated since it is abstract and not concrete.
Yes. It cannot be instantiated since declaring a class as abstract restrict it to be not instantiated on its own.
- All methods in an interface are implicitly abstract. On the other hand, an abstract class may contain both abstract and non-abstract methods.
- A class may implement a number of Interfaces, but can extend only one abstract class.
- In order for a class to implement an interface, it must implement all its declared methods. However, a class may not implement all declared methods of an abstract class. Though, in this case, the sub-class must also be declared as abstract.
- Abstract classes can implement interfaces without even providing the implementation of interface methods.
- Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
- Members of a Java interface are public by default. A member of an abstract class can either be private, protected or public.
- An interface is absolutely abstract and cannot be instantiated. An abstract class also cannot be instantiated, but can be invoked if it contains a main method.
No. An abstract method cannot be static. You cannot override a static method, so making it abstract would not make any sense.
Both abstract class and interface are used for abstraction by hiding the internal implementation of the feature and only showing the functionality to the users.
method types: Interface can have only abstract methods. From Java 8, interface can have default and static methods also. Abstract class can have abstract and non-abstract methods.
Final Variables: Variables declared in a Java interface are by default final. An abstract class may contain non-final variables.
Type of variables: Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
Accessibility of Data Members: Members of a Java interface are public by default. A Java abstract class can have class members like private, protected, etc.
Implementation: Abstract class can provide the implementation of interface while Interface cannot provide the implementation of abstract class.
Inheritance vs Abstraction: A Java interface can be implemented using keyword "implements" and abstract class can be extended using keyword "extends".
Multiple implementation: An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
Yes, but its object cannot be created by calling the constructor instead it is invoked during constructor chaining.
Yes, an abstract class can implement the interface by using implements keyword. Since they are abstract, they dont need to implement all methods.
No.
Yes, the abstract class can declare and define static methods. It is not recommended though as per design principles.
An abstract method is a method without a body. You just declare method, without defining it and use abstract
keyword in method declaration. All method declared inside Java Interface are by default abstract. Here is an example of an abstract method in Java.
public void abstract anExampleAbstractMethod();
Yes, it is also a static method and it can execute the abstract class.
public abstract class AbstractClassMain { public static void main(String[] args) { System.out.println("Hello from abstract class"); } }
Yes.
Yes.
public class OuterClass { public void instanceMethod(){ abstract class InnerClass{ } } }
Below is an example that illustrates the usage of local inner abstract class.
public class SendNotification { public enum NotifyBy { MESSAGE, EMAIL, PHONECALL } public Runnable createNotification(NotifyBy type) { abstract class ProtoType implements Runnable { public void run() { prepareContent(); sendNotification(); } abstract void sendNotification(); private void prepareContent() { // notification message content } } switch (type) { case EMAIL: { return new ProtoType() { void sendNotification() { System.out.println("Sent using email."); } }; } case MESSAGE: { return new ProtoType() { void sendNotification() { System.out.println("communicated by text message."); } }; } } return new ProtoType() { void sendNotification() { System.out.println("communicated by phone call."); } }; } public static void main(String[] args) { new Thread(new SendNotification().createNotification(NotifyBy.MESSAGE)).start(); ; } }
Yes. Abstract methods can throw exceptions.
Yes. An abstract class can have the final concrete method. See the below example.
public class AbstractClassFinalConcreteMethod { public static void main(String[] args) { Parent p = new Child(); p.parentMethod(); } } abstract class Parent { final void parentMethod() { System.out.println("Final Method in a abstract class"); } } class Child extends Parent { }
Output: Final Method in a abstract class
In the above example, the parent abstract class has a final concrete method 'parentMethod' which is allowed and also called directly from the parent reference.
Double brace initialization creates an anonymous class derived from the specified class and provides an initializer block.
new ArrayList<String>() {{ add("ONE"); add("TWO"); }};
The outer brace creates the anonymous inner class while the inner represents the initializer block.