No,
you can’t extend a class that has only private constructors. Let’s see it with
an example.
Shape.java
public class Shape { private Shape() { System.out.println("Inside Shape"); } }
Circle.java
public class Circle extends Shape { }
Try
to compile 'Circle.java', Java compiler throws following errors.
C:\Users\Documents>javac Circle.java Circle.java:1: error: Shape() has private access in Shape public class Circle extends Shape { ^ 1 error
This
is one of the good interview questions, usually interviewers checks your
understanding on OOPS concepts. Same question can be asked like 'How can you make a class as non-extendable
without using final keyword'.
You may like
No comments:
Post a Comment