How nested interface in a class behave like? [duplicate] - java

This question already has answers here:
Why would a static nested interface be used in Java?
(11 answers)
Closed 7 years ago.
For below code,
interface SuperInterface{
void f();
interface StaticNestedInterface{
}
}
class Sub implements SuperInterface{
public void f(){
}
}
public class Dummy {
public static void main(String[] args) {
Sub x = new Sub();
}
}
Compiler does not ask class Sub to implement interface StaticNestedInterface.
Is class Sub a valid java code?

Yes, its a valid Java code.
You are just declaring StaticNestedInterface in SuperInterface. One way to use nested interface would be
interface SuperInterface {
void f();
StaticNestedInterface sni();
interface StaticNestedInterface {
void a();
}
}
class Sub implements SuperInterface{
public void f(){
}
#Override
public StaticNestedInterface sni() {
return null;
}
}

Yes its a valid java code. Java compiler will create public static SuperInterface$StaticNestedInterface{}
To use the nested interface class Sub implements SuperInterface.StaticNestedInterface then compiler will ask Sub class to implement the method declared on StaticNestedInterface

interface StaticNestedInterface{
void a();
void b();
}
interface SuperInterface extends StaticNestedInterface{
void f();
}
class Sub implements SuperInterface{
//now compiler require all methods(a, b and f)
}

Related

How prevent a method of super in subclass (java) [duplicate]

This question already has answers here:
Disabling inherited method on derived class
(11 answers)
Closed 5 years ago.
please note to this.
I have a super class
class A
{
public void update(int num)
{...}
}
and have a other class. inherit of class A
class B extends A
{
public void update(string str)
{...}
}
B obj = new B();
obj.update(50); // oh no, bad
obj.update(""); // yes
now, i want that when i create new from class B the update() of class A not access. -in other words i want access to the update() method of B only
You can prevent that only when you have the same method name with same arguments in super type and sub type effectively overriding it. In your case, the method the signatues are different.. they take different arguments, hence based on what you pass as argument, it calls the respective method.
Actually you can override that method and throw an exception is a solution.
class A
{
public void update(int num)
{...}
}
class B extends A
{
public void update(int i) {
throw new UnsupprotedOperationException();
}
public void update(string str)
{...}
}
Or you can implement a class with generics. This may be not working but idea works.
class A<T>
{
public void update(T num)
{...}
}
class B extends A<String>
{
public void update(String str)
{...}
}
Make your base class method private.
class A
{
private void update(int num)
{
}
}

Can an abstract class be instantiated? [duplicate]

This question already has answers here:
Can we instantiate an abstract class?
(16 answers)
Closed 7 years ago.
abstract class A {
public void disp() {
System.out.print("Abstract");
}
}
public class B {
public static void main(String args[]) {
A object = new A(){ };
object.disp();
}
}
I am aware that Abstract classes cannot be instantiated, but confused on this code.
Actually what this code mean ?
The subtlety here is in the "{}". It means you explicitly provide an anonymous implementation for the missing parts (the missing parts are abstract methods) of the abstract class A allowing you to instantiate it.
But there's no abstract method in A, therefore the anonymous implementation is empty.
Example showing the behaviour with at least one abstract method:
public abstract class A {
public abstract void bar();
public void disp() { System.out.print("Abstract"); }
}
public class B {
public static void main(String args[]) {
A object = new A() {
#Override public void bar() { System.out.print("bar"); }
};
object.disp(); //prints "Abstract"
object.bar(); //prints "bar"
}
}
This is called an anonymous inner class. You are not instantiating the abstract class, you are instantiating the concrete anonymous inner class which extends the abstract class. Of course, in order for this to be allowed, the anonymous inner class must provide implementations for all the abstract members of the abstract superclass … which it does in this case, because the abstract superclass has no abstract members.

Calling class's methods into an interface

I have created some interface such that:
public interface A{
}
and i would like to call the method a that I have already implemented in class B in interface A such that:
public class B{
public boolean a(){
return true;
}
}
public interface A{
public void call {
a();
}
}
without any errors, any help please?
What you want to do is strictly speaking impossible, as you cannot define method implementations in an interface. You can get something similar by defining an implementation of the interface that extends B. Hopefully that is close enough.
public class AImplementation extends B implements A{
public void call(){
a();
}
}
If you are using any java version before 8, then stick with the answers of #tinker and #Davis Broda. They provide better design since they do not couple your interface to the B class. If you insist however, in java 8 you can have default method implementations as well as static methods in an interface.
If your method is for inheritance then you have to use a default method. Add the default keyword:
default void call() {
...
}
Now the problem is how to get a reference to the class in order to call the method since you cannot have instance fields in interfaces. You have two choices:
Pass the object of B as a method parameter:
public interface A{
default void call(B b) {
b.a();
}
}
or make the method in B static
public interface A{
default void call() {
B.a();
}
}
If your method is not for inheritance but just a utility than you can make it static as :
public interface A{
public static void call() {
B.a();
}
}
I agree with #Davis Broda's answer, there is no way to have a method definition in an interface. But I have another way to address this.
You can have the interface and then have an abstract class implement this interface, and then have all other classes extend the abstract class. The abstract class doesn't have to extend the class from where you want to call the method, you could call it from an instance of that class too.
public interface A {
void caller();
}
public class B {
public void callMe() {
}
}
public class AbstractA implements A {
private B b;
public AbstractA(B b) {
this.b = b;
}
#Override
public void caller() {
b.callMe();
}
}
This way, all implementations of AbstractA will be able to call B's callMe method. And you can access this directly from the interface using this code:
A anInstance = someInstance;
anInstance.caller();
Your question is not very clear, but if I'm guessing right, you want interface A to be kind of a generic caller.
If you're using Java 8, you can achive that using a method reference:
public class B {
public boolean a() {
return true;
}
}
public interface A<T> {
default T call(Supplier<T> s) {
return s.get();
}
}
public class AImpl
implements A<Boolean> {
}
public class Sample {
public static void main(String[] args) {
AImpl a = new AImpl();
B b = new B();
boolean result = a.call(b::a);
System.out.println(result); // true
}
}
This uses Supplier<T> because your method a() in class B returns a boolean and does not receive any arguments.

Any way to specify a java generic type variable that extends any one of multiple classes?

So suppose I have 2 classes:
public class A
{
public void
f()
{
}
}
public class B
{
public void
f()
{
}
}
I would like to write a generic static method that could call f when passed an instance of A or B. I tried:
public class C
{
public static <T extends A & B> void
g(T t)
{
t.f();
}
public static void main(String[] args)
{
A a = new A();
g(a);
}
}
But the compiler claims A is not a valid substitute for "T extends A & B", which I assume is because T must extend BOTH A and B, which obviously A does not. I could not find a way to specify something like "T extends A OR B". Is something like this not achievable? I am a java neophyte, so any help with this would be appreciated.
You can only specify one generic type. Use interfaces instead.
An interface specifies a certain set of methods, each member of it has to have. A class can implement multiple interfaces.
In your example, I would define an interface with the method f():
public interface MyInterface {
void f();
}
Let A and B implement the interface:
public class A implements MyInterface
{
#Override
public void f() {
// ...
}
}
public class B implements MyInterface
{
#Override
public void f() {
// ...
}
}
Then you can just specify the interface as type of the argument for your method:
public static void g(MyInterface obj)
{
obj.f();
}
For more detail on interfaces, check the Java documentation: What Is an Interface?

A class implements two interfaces. Which interface does a method belong to? [duplicate]

This question already has answers here:
Implementing multiple interfaces having same method
(5 answers)
Implementing two interfaces in a class with same method. Which interface method is overridden?
(8 answers)
Closed 9 years ago.
There are two interfaces B and C each having the same method public m1()
class A implements B and C
If class A has to implement method m1(), the implemented method would be of which interface?
I think we can say that A.m1 implements both B.m1 and C.m1. Because both
B b = new A();
b.m1();
and
C c = new A();
c.m1();
will work
This is a common problem, this is why having clear instructional method names is important. And good OOP design that will make same methods be abstract.
It is also the reason things are separated out in to classes.
Animal.eat()
Fish extends Animal
Fish.eat()
Dog extends Animal
Dog.eat()
Interface have does not have method body,So it hardly matters which method is implemented
See the following example
package test;
public class a implements i,j{
#Override
public void run() {
// TODO Auto-generated method stub
}
}
package test;
public interface i {
public void run();
}
package test;
public interface j {
public void run();
}
In the class a run() is overridden but does it matter if it is from interface i or j
Since interfaces do not have the implementation, it doesn't matter. There are no deadly diamond of death sort of issues here.
There will be no problem as long as declarations of m1 in B and C are "compatible", i.e. have the same return value.
E.g.
public interface B {
void doit();
}
public interface C {
void doit();
}
public class A implements B, C {
#Override
public void doit() {
// TODO Auto-generated method stub
}
}
but if the return type differ then it's not clear which is to be called and that will result in compile error like "The return type is incompatible with B.doit()"
You have to add only one public m1() method. It will be for both the intefaces. And if both the interfaces have the same parameters,the method declaration will be public m1().

Categories

Resources