- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to ensure that child class overrides a super class method 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 (body) to it. If a class contains at least one abstract method, you must declare it abstract.
In other words, if you extend an abstract class it is mandatory to implement (override) all the abstract methods in it or, declare it abstract else 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("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.
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
Therefore, if you need to make sure that the sub class overrides a particular method of the super class method, you just need to declare the required method abstract.
To make the above program work you need to implement all the abstract methods as −
Example
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("subclass implementation of the display method "); } public void setName(String name){ System.out.println("Name: "+name); } public void setAge(int age){ System.out.println("Age: "+age); } public static void main(String args[]) { AbstractClassExample obj = new AbstractClassExample(); obj.display(); obj.setName("Krishna"); obj.setAge(20); } }
Output
subclass implementation of the display method Name: Krishna Age: 20
- Related Articles
- How to convert a sub class variable into a super class type in Java?
- How to convert a super class variable into a sub class type in Java
- What is the super class of every class in Java?
- Why Object class is the super class for all classes in Java?
- Get super class of an object in Java
- Instance child class in abstract static method PHP?
- Can super class reference variable hold sub class's object in java?
- Java Program to create String to super class type mapping
- How to explicitly call base class constructor from child class in C#?
- Why static methods of parent class gets hidden in child class in java?
- Class declaration with a method that has a parameter in Java
- How do we check if a class is a subclass of the given super class in Python?
- How do I make a subclass from a super class in Python?
- Variable in subclass hides the variable in the super class in Java
- How to call parent constructor in child class in PHP?
