The member interface can only be defined inside a top-level class or
interface or in a static context.
Case A: Interface within a top-level class works perfectly
package multiplei.interfaces.test;
public class InterfaceBetweenClass {
interface Foo {
void show();
}
class InnerClass implements Foo{
public void show(){
System.out.println("Inner Class implements Foo");
}
}
public static void main(String[] args) {
new InterfaceBetweenClass().new InnerClass().show();
}
}
Case B: Interface within an interface works good.
public interface Creatable {
interface Foo{
void show();
}}
Case C: I know it sounds stupid that why would anyone define an interface in a static context. But it give me the same error message when i try to define the interface in static context.
package multiplei.interfaces.test;
public class InterfaceBetweenClass {
public static void main(String[] args) {
interface Foo { //Line 5
void show();
}
}
}}
But line number 5 gives me the following error message "The member interface Foo can only be defined inside a top-level class or interface or in a static context." Please help me out with this If an interface can be defined in static context then how?
You cannot define interfaces within methods.
I think the scenario the error message is referring to is defining an interface inside an inner class (which can be done, but only if that is a static inner class):
class A{
static class X{
interface Y{}
}
}
For interface, both nested-interface and member-interface are the same thing (#opposite to member-class). Moreover, member-interface is an interface which is DIRECTLY enclosed by another class or another interface. Therefore, local interface DOES NOT exist.
Related
I was testing a program in which i was trying to implement a interface on abstract class . as given below
interface Inf{
void display();
}
abstract class InfTst implements Inf{
}
class InterfaceTest extends InfTst{
void display(){
System.out.println("Hello");
}
}
but it is shows error
error: display() in InterfaceTest cannot implement display() in Inf
void display(){
what is this error means and how resolve it, please help me.
When you omit access modifier on an interface it defaults to public, but on concrete classes it defaults to package-private.
Change your method signature to below in concrete class InterfaceTest
public void display(){..}
In concrete classes by default package is private. A concrete method means, the method have complete definition so must be your method modifier are always public.
package domain;
interface Inf {
void display();
}
abstract class InfTst implements Inf {
}
class InterfaceTest extends InfTst {
public void display() {
System.out.println("Hello");
}
}
public class StackOverFlow extends InterfaceTest {
public static void main(String[] args) {
StackOverFlow sof = new StackOverFlow();
sof.display();
}
}
output : - Hello
This is a bit weird and might ring of iffy syntax but hold with me. I've been trying for three months and I'm convinced that I need a way to do this:
public abstract class Sup{
...
//This is implemented here because I cannot create an abstract static
//only implemented by the children but called statically by methods in
//the parent (more info later on in the post):
protected static Class<? extends Sup> getTypeClass(){ return Sup.class };
public static void init(){
...
alreadyDeclaredHashMap.put(getTypeClass(), hashMapOfOtherStuff);
}
}
public class A extends Sup{
static{
init();
}
protected static void getTypeClass(){ return A.class };
}
public class B extends Sup{
static{
init();
}
protected static void getTypeClass(){ return B.class };
}
... and so on.
So that if I were to print out alreadyDeclaredHashMap, it would look like:
class A -> hashMapOfOtherStuff
class B -> hashMapOfOtherStuff
class C -> hashMapOfOtherStuff
...
But instead it prints:
class Sup -> hashMapOfOtherStuff
class Sup -> hashMapOfOtherStuff
class Sup -> hashMapOfOtherStuff
...
Because the extending classes hide getTypeClass() but can't override it. This is just an example. In reality, I am making a Units system and I have a lot of methods depending on getTypeClass() and would really love to not have to rewrite them in every extending class (of which there are an indefinite number) with the only difference in implementation being the class name.
Many thanks!
P.S. These methods do have to be static because they are being called statically (and I would rather not have to create a dummy instance or reflection just to call them).
There is no way to get that to work. The static code in class sup has no knowledge of class A and class B, even when the init method is invoked from one of them.
Static methods are not "virtual", so calling getTypeClass() from the static code in Sup will call that implementation, not any of the subclass implementation.
Now, if you want to reuse the init method from A and B, you'll have to pass as parameters.
public abstract class Sup{
...
public static void init(Class<? extends Sup> typeClass) {
...
alreadyDeclaredHashMap.put(typeClass, hashMapOfOtherStuff);
}
}
public class A extends Sup {
static {
init(A.class);
}
}
public class B extends Sup {
static {
init(B.class);
}
}
I have these 2 classes
class A {
public void foo1() {
...;
foo2();
...;
}
protected abstract foo2();
}
class B extends A {
public foo2() {
......
}
I need foo2 to be static so I can do B.foo2() but I also want the functionality in class A to remain.n
Any suggestions?
}
You can't override static methods or implement abstract methods as static.
Static methods are defined on a class definition, not on a class instance. Abstract methods are defined on a class instance.
What you said doesn't make sense in fact.
Although I don't quite get why you need to do it, there is a workaround:
class B {
#Override
public void foo() {
fooUtil();
}
public static void fooUtil() {
// your impl here
}
}
Then you can do B.fooUtil() instead, and using its behavior to override A.foo().
This is more of a puzzle than question. I have the following code:
public class PrivateBaseConstructor {
public static class BaseClass {
private BaseClass() {
}
}
public static class DerivedClass extends BaseClass {
public DerivedClass() {
super(); // 1*
}
}
}
Here the call for super(); at 1* is allowed even though the base class constructor is private. If we write the classes as separate classes in same package:
BClass.java
public class BClass {
private BClass() {
}
}
DClass.java
public class DClass extends BClass {
public DClass() {
super(); // 2*
}
The compiler rightly gives an error at 2* since the base class constructor is not visible.
Why doesn't the compiler throw an error in my first scenario when both the classes are declared static within one class?
if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (ยง7.6) that encloses the declaration of the member or constructor.
http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.6.1
Because nested classes can see each others members. This has nothing to do with the static declarations. See the following example of your code with just nested inner classes (not static).
public class PrivateBaseConstructor {
public class BaseClass {
private BaseClass() {}
}
public class DerivedClass extends BaseClass {
public DerivedClass() {
super(); // 1*
}
}
public static void main(String[] args)
{
new PrivateBaseConstructor(). new DerivedClass();
}
}
Read more about nested classes here: http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
Because anything declared inside a class can access its private members, including inner classes. However, if you run PMD on your class, you'll find it suggests you change the visibility of the constructor to not-private.
Assume there is a code as such:
package com.ps.Sample;
public interface Sample
{
public void Method1();
}
public abstract class AbstractSample implements Sample
{
public void Method1()
{
System.out.println("Hello World");
}
}
public class MySample extends AbstractSample
{
}
public class TestSample
{
public static void main(String[] args)
{
Sample my = new MySample();
my.Method1();
}
}
My question is:
Is there any benefit to declaring the concrete class as
public class MySample extends AbstractSample implements Sample
instead of
public class MySample extends AbstractSample
No, there is not. It's redundant. AbstractSample is a Sample, and MySample is a AbstractSample. So MySample is a Sample.
The javadoc displays all the implemented interfaces anyway, whether you add the implements Sample or not.
One benefit would be that if AbstractSample was changed to not implement Sample, the first declaration would still allow you to pass instances of MySample to methods expecting a Sample.