
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What happens if the subclass does not override abstract methods in java?
A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.
public abstract myMethod();
To use an abstract method, you need to inherit it by extending its class and provide implementation to it.
A class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract.
Extending an abstract class
Once you extend an abstract class in Java you need to override all the abstractmethods in it or, declare it abstract. If you don’t, a compile time error will be generated for each abstract method (that you don’t override) saying “subclass_name is not abstract and does not override abstract method abstractmethod_name in classname”.
Example
Following Java example contains two abstract classes: One is an abstract class (MyClass) that contains 3 abstract methods and the other is a class with name AbstractClassExample that extends the earlier one.
In the subclass we are overriding only one abstract method (display()).
import java.io.IOException; abstract class MyClass { public abstract void display(); public abstract void setName(String name); public abstract void setAge(int age); } public class AbstractClassExample extends MyClass{ public void display(){ System.out.println("This is the subclass implementation of the display method "); } public static void main(String args[]) { new AbstractClassExample().display(); } }
Compile time error
On compiling, the above method generates the following compile time error
Output
AbstractClassExample.java:9: error: AbstractClassExample is not abstract and does not override abstract method setAge(int) in MyClass public class AbstractClassExample extends MyClass{ ^ 1 error
To make this program work you either need to override all the abstract methods of the super-class or, make the subclass abstract as shown below −
abstract class MyClass { public abstract void display(); public abstract void setName(String name); public abstract void setAge(int age); } public abstract class AbstractClassExample extends MyClass{ public void display(){ System.out.println("This is the subclass implementation of the displaymethod "); } }
- Related Questions & Answers
- What happens if a class does not implement all the abstract methods of an interface in java?
- What happens if we does not initialize variables of an interface in java?
- what are abstract methods in Java?
- What happens if we overload default methods of an interface in java?
- What does abstract modifier in Java do?
- What is the difference between non-static methods and abstract methods in Java?
- What happens if an exception is not handled in a java program?
- What happens if NullPointerException is not handled in main method of java?
- Can we override private methods in Java
- Can we override final methods in Java?
- Can we override default methods in Java?
- What happens when a subclass object is assigned to a superclass object in Java?
- Can we synchronize abstract methods in Java?
- What happens if the specified index is not present in the series Python Pandas?
- Can we define an abstract class with no abstract methods in Java?