Yes, abstract class have constructors in java. But it is not used to instantiate abstract class. It is used in constructor chaining or to initialize abstract class common variables.
Example:
abstract class DisplayTest { protected final String website; abstract void display(String str); DisplayTest(String website){ this.website = website; } } public class Main extends DisplayTest { public void display(String str) { System.out.println(str + " " + website); } Main(String website){ super(website); } public static void main(String[] args) { DisplayTest displayTest = new Main("w3schools.com"); displayTest.display("Hello"); } } |
Example:
Hello w3schools.com |
Java interview questions on interface and abstract class
- What is interface in java?
- Can we declare an interface method static in java?
- Can an interface be declared final in java?
- What is marker interface and how we can create it?
- What is difference between abstract class and interface in java?
- What is abstract class in java?
- Why abstract class is used in java?
- Can abstract class have constructors in java?
- Can abstract class be final in java?
- Can we declare local inner class as abstract?