- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Can we declare an abstract method, private, protected, public or default 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.
Declaring an abstract method private
If a method of a class is private, you cannot access it outside the current class, not even from the child classes of it.
But, incase of an abstract method, you cannot use it from the same class, you need to override it from subclass and use.
Therefore, the abstract method cannot be private.
If, you still try to declare an abstract method final a compile time error is generated saying “illegal combination of modifiers − abstract and private”.
Example
In the following Java program, we are trying to declare an abstract method private.
abstract class AbstractClassExample { private static abstract void display(); }
Compile time error
On compiling, the above program generates the following error.
AbstractClassExample.java:2: error: illegal combination of modifiers: abstract and private private static abstract void display(); ^ 1 error
Declaring an abstract method protected
Yes, you can declare an abstract method protected. If you do so you can access it from the classes in the same package or from its subclasses.
(Any you must to override an abstract method from the subclass and invoke it.)
Example
In the following Java program, we are trying to declare an abstract method protected.
abstract class MyClass { protected abstract void display(); } 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(); } }
Output
This is the subclass implementation of the display method
- Related Articles
- Can we declare main() method as private or protected or with no access modifier in java?
- Can we declare an abstract method final or static in java?
- Can we declare interface members as private or protected in java8?
- Can we declare a top level class as protected or private in Java?\n
- What are private, public, default and protected access Java modifiers?
- Can we declare the variables of a Java interface private and protected?
- Can we declare a main method as private in Java?\n
- Can we have a private method or private static method in an interface in Java 9?\n
- Can we declare a constructor as private in Java?
- Can we override a private or static method in Java
- Can we define an abstract class without abstract method in java?\n\n
- Difference between private, public, and protected modifiers in C++
- Difference between private, public, and protected inheritance in C++
- Can we override a protected method in Java?
- Can we declare the method of an Interface final in java?
