Java abstract classes tutorial
We cannot use abstract classes to instantiate objects. An abstract class is used to declare some properties and behaviors which will inherit to its child classes.
Behaviors of the Java abstract classes
-
We cannot use abstract classes to instantiate objects
-
To declare an abstract class we need to use abstract keyword
public abstract class Bird { }
-
An abstract class can have both abstract and non abstract methods
public abstract class Bird { protected abstract void fly(); //abstract method private void sing() { //concrete class System.out.println("Bird is singing"); } }
An abstract method
abstract void fly();
-
We need to use the extends keyword to inherit the parent's properties into the child class
public class HummingBird extends Bird { @Override protected void fly() { System.out.println("Humming bird bird flying"); } }
Possible interview question about Java abstract classes
-
Can we have constructors in a Java abstract classes ?
Yes, we can have constructors in an abstract class. Normally we can use a constructor in an abstract class to enforce some property values in the child class. Because child class must call (if there is a constructor with arguments in the abstract class) parent abstract class's constructor using the super()
public abstract class Bird { private String name; public Bird(String name) { this.name = name; } protected abstract void fly(); private void sing() { System.out.println("Bird " + name + " is singing"); } } public class HummingBird extends Bird { public HummingBird(String name) { super(name); } @Override protected void fly() { System.out.println("Humming bird bird flying"); } }
If there is a constructor with arguments in the abstract class and we are not going to override it, we will get below compilation error.
error: method does not override or implement a method from a supertype
-
Can we have final concrete methods in Java abstract classes ?
Yes, We can have final concrete methods in an abstract class like in normal classes. But child classes cannot override it. This will help to create a fixed logic to share among all the child classes.
public abstract class Bird { private String name; public Bird(String name) { this.name = name; } protected abstract void fly(); protected final void sing() { System.out.println("Bird " + name + " is singing"); } }
We will get a similar error like below during the compilation if we try to override the final method in a child class.
HummingBird.java:17: error: sing() in HummingBird cannot override sing() in Bird protected void sing() { ^ overridden method is final 1 error
-
Can we have final abstract classes in Java ?
No, In java, we cannot create final abstract classes. Because it doesn't make any sense to declare a class which cannot create objects and the child classes also cannot create objects. If you try to declare a abstract class with final you will get below error.
error: illegal combination of modifiers: abstract and final
-
Can we declare static methods in Java abstract classes ?
Yes, We can have static methods in an abstract class. To use static methods, we don't need object creations. So it will act like normal static methods.
public abstract class Bird { private String name; public Bird(String name) { this.name = name; } protected abstract void fly(); public static void staticMethod() { System.out.println("This is a static method in a Bird abstract class"); } }
-
Can we extend multiple abstract classes into one concrete class ?
No, In java, we cannot extends multiple abstract classes into a single concrete class. Refer to this page to find all the possibilities in one place as a summary.
-
Can we extends an concrete class into a abstract class ?
Yes, In java, we can extends a concrete class in to a abstract class. Refer to this page to find all the possibilities in one place as a summary.
public class HummingBird { public String getAnimalType() { return "Bird"; } } public abstract class Bird extends HummingBird { protected abstract void fly(); }
-
Can we extend abstract class into a abstract class ?
Yes, In java, we can extends abstract class into another abstract class. Refer to this page to find all the possibilities in one place as a summary.
public abstract class Animal { protected abstract String getAnimalType(); } public abstract class Bird extends Animal { protected abstract void fly(); }
-
Can we implement interface into a abstract class ?
Yes, In java, we can implement as an interface into an abstract class. Refer to this page to find all the possibilities in one place as a summary.
public interface Animal { String getAnimalType(); } public abstract class Bird implements Animal { protected abstract void fly(); }
-
Can we implement multiple interfaces into a abstract class ?
Yes, In java, we can implement multiple interfaces into an abstract class. Refer to this page to find all the possibilities in one place as a summary.
public interface Animal { String getAnimalType(); } public interface Carnivore { boolean isCarnivore(); } public abstract class Bird implements Animal, Carnivore { protected abstract void fly(); }
<< Java Objects and Classes Java Interfaces >>