Java abstract Keyword
abstract keyword can be used on Java classes and methods.
abstract class Thing{
abstract void roll();
}
abstract is used to indicate that a class is abstract and hence cannot be used directly or cannot be instantiated
abstract classes and abstract methods are always used togetherAbstract Classes and Methods
abstract keyword is used to mark classes to indicate that:
- the class cannot be instantiated directly
- the class needs to be inherited by a subclass for object creation
abstract keyword is used to mark method to indicate that:
- method cannot be called unless method definition is provided by a child class or anyonymous class
Method Declaration vs Definition
- Method declarations are methods with no body.
void roll();
- Method definitions are just methods with body or block of statements.
void roll() { System.out.println("I am doing something"); }
Examples for Abstract Classes and Methods
abstract class Thing{
}
abstract class Thing{
abstract void roll();
}
abstract class Thing{
void roll(){
System.out.println("I can roll");
}
}
abstract class Thing{
void roll(){
System.out.println("I can roll");
}
abstract void anotherRoll();
}
Abstract classes and methods creation
From the above, you can understand that abstract class can be simply created
- with no method declarations or definitions.
- with just one or more abstract method declaration
- with no abstract method declaration but other regular method definitions
- with mix of both abstract method declarations and definitions
Note: if abstract class have method declarations, they should always be marked abstract
Abstract Nested Classes
Nested class can be abstract.
public class Corporate {
abstract class Company{
abstract void publishResults();
}
}
Abstract Interfaces
Top level interface can be abstract but there is no need to mention interface explicitly as abstract, as they are implicitly abstract.
abstract interface Employee{
abstract void actOnResponsibility();
}
Above is same as:
interface Employee{
void actOnResponsibility();
}
Abstract Nested Interfaces
Nested interface can be abstract but there is no need to mention interface as abstract, as they are implicitly abstract.
package kriv;
public class Corporate {
abstract interface Employee{
abstract void actOnResponsibility();
}
}
Abstract class vs Interface
Interfaces can have static methods, default methods (from Java 8) and abstract methods only, it cannot have other concrete methods. However, default methods in interfaces are very similar to what we can achieve with concrete methods. abstract classes can have static, abstract and concrete methods.
Common Issues that Developers make
Example class below is Invalid and wont compile. This is because class must be abstract to declare abstract methods
class InvalidThing2 {
abstract void roll();
}
Example class below is Invalid and wont compile. This is because abstract methods should not have a body
abstract class InvalidThing3 {
abstract void roll() {
}
}
How to create an Object for an abstract class
Method 1: by using Concrete class
abstract class ThingA{
abstract void roll();
}
class SomeThing extends ThingA{
void roll(){
System.out.println("Yes. I roll");
}
}
class MyMain{
public static void main(String args[]){
ThingA t= new SomeThing();
t.roll();
}
}
Method 2: by using anonymous class
abstract class Thing0{
abstract void roll();
}
class MyMain2{
public static void main(String args[]){
Thing0 t= new Thing0(){
void roll(){
System.out.println("Hi I am inside Anonymous");
}
};
t.roll();
}
}
Another method 2 variant
abstract class Thing4 {
void roll() {
System.out.println("Hello");
}
}
class MyMain{
public static void main(String args[]) {
Thing4 t= new Thing4() {};
}
}