No, a class cannot extend itself in java.
Example
public class Main extends Main { String name = "Jai"; int rollNo = 11; void show(){ System.out.println(name); System.out.println(rollNo); } public static void main(String[] args) { Main object = new Main(); object.show(); } } |
Output
Main.java:8: error: cyclic inheritance involving Main public class Main extends Main ^ 1 error |
Java interview questions on Inheritance
- Why multiple inheritance is not supported in java?
- How to implement multiple inheritance in java?
- Are interfaces also inherited from Object class?
- Why an interface cannot have constructor in java?
- How do you restrict a member of a class from inheriting to it’s sub classes?
- Can a class extend itself in java?
- Are constructors inherited in java?
- What happens if both superclass and subclass have a field with same name?
- Are static members inherited to subclasses in java?