An abstract class is permitted to have both concrete and abstract methods. In abstract class, no method (either concrete or abstract) can be private. The reasons are very simple and when known looks as of common sense.
Let us make a list to remember.
- Abstract methods in an abstract class cannot be private but can be of specifiers public, protected or default.
- Similarly, any class cannot be protected or private (can be only public or default). So by this rule, an abstract class cannot be private.
- Abstract method or concrete method in abstract class can be protected (a program is available at the end).
- As a general rule of Java, the private methods cannot be inherited by subclass. When not inherited, how the abstract methods can be given body (or overridden or implemented) by subclass.
- When a method is private, the sub classes can’t access it, hence they can’t override it.
- An abstract method cannot be private and also cannot be static, final, native, strictfp, or synchronized.
So, private abstract Method is not possible.
Can abstract method be protected?
Yes, except private, an abstract method can be public, protected or default (in interface all methods should be abstract and public).
1. Following code is possible
Here, the extra condition is private method should be static. But remember, static methods cannot be overridden. The code is just of interest but practically not of usage.
public abstract class Demo
{
private static void display()
{
System.out.println("I am private");
}
public static void main(String args[])
{
display();
}
}
2. Following is also possible. Here both concrete method and abstract method are protected. "protected" method can be used by subclasses only.
abstract class Test
{
protected void display()
{
System.out.println("Yes, I am concrete protected");
}
protected abstract void show(); // it is also possible with default and public
}
public class Demo extends Test
{
protected void show()
{
System.out.println("Yes, I am abstract protected");
}
public static void main(String args[])
{
Demo d1 = new Demo();
d1.display();
d1.show();
}
}
Hello Rao,
I tested your statement “Abstract methods or concrete methods in an abstract class cannot be private” with code and got contradictory result.
I request you to reconsider whether concrete methods can be declared private. My answer to this question is “Yes, they can be private”.
If concrete method is private, call and print. Get me code.
Declare private method as static and call in the main(). A program already given in the same posting.
It is corrected the statement. Observe, in the same posting, I gave a program with private static concrete method which works.
Statement is corrected. Anyhow, in the same posting, I gave private static concrete method in a program which works.