Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Abstract Class

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can "protected" be used in the declaration of an abstract method in an abstract class?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
protected methods are possible inside abstract class

//class AbstractTest
public abstract class AbstractTest {
private void test() {
System.out.println("Inside test private");
}
protected void test1() {
System.out.println("Inside test protected");
}
void test2() {
System.out.println("Inside test default");
}
public void test3() {
System.out.println("Inside test public");
}
}

//class AbstractImpl

public class AbstractImpl extends AbstractTest {
public static void main(String[] args) {
AbstractImpl ob = new AbstractImpl();
// ob.test();//not possible to instansiate abstract classes
ob.test1();
ob.test2();
ob.test3();
}
}
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For an abstract method, only public or protected modifiers can be set.
If this abstract class is extended by another class of different package then that class should able to access this abstract method to override.

Only public, protected members can be visible to all its subclasses in any packages.

We can also leave a abstract method declaration with default access.
But the problem arises when it is extended by a class of other package.
In that case, that second class cannot implement this abstract method as the method is not visible to other packages. The only solution to this make the second class type abstract as below.

package com.nanna.abs.one;

public abstract class AbstTest {

abstract void test();

}

package com.nanna.abs.two;

import com.nanna.abs.one.AbstTest;

public abstract class TestOnAbstract extends AbstTest{


public void test2(){


}

}


We can conclude that chain of subclasses of AbstTest cannot be instantiated as they must declare abstract.
 
Everybody's invited. Even this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!