static interface factory methods [Java 8] [duplicate] - java

This question already has answers here:
Java - Alternatives to forcing subclass to have a static method
(5 answers)
Is there a way to make sure classes implementing an Interface implement static methods?
(9 answers)
Closed 5 years ago.
I want each class that implements an interface to have a static factory method, as defined by the interface. I.e:
public interface Handle {
public static Handle GetHandle() {
return null;
}
public void DoThings();
}
public class HandleA implements Handle {
private HandleA();
public static HandleA GetHandle() {
return new HandleA();
}
public void DoThings() {
return;
}
}
// This is allowed even though HandleB doesn't provide a GetHandle()
public class HandleB implements Handle {
private HandleB();
public void DoThings() {
return;
}
}
Reading past questions, it seems like the static method GetHandle() isn't enforced in concrete classes of Handle because that's not what the intended design behavior of static interface method is. Is there another way to do what I want? (i.e enforce all implementations of Handle to provide a "factory" GetHandle() method).

You cant enforce a class to have a static method in Java.

Related

Java Interface two method with optional parameters [duplicate]

This question already has answers here:
Why should I ever overload methods?
(6 answers)
Closed 2 years ago.
I want to have an interface that allows me to use methods with optional parameters. Suppose I have an interface:
public interface Stuff {
public int Add();
}
And I have two classes A and B who implement the interface. One method needs parameter, but the other one doesn't.
public class CLASS A implements Stuff{
public int Add();
}
public class CLASS B implements Stuff{
public int Add(String name);
}
How can I achieve this?
I think You have to override function.
You can read about that here => https://www.geeksforgeeks.org/overriding-in-java/

How to inherit from a base class but make some of public methods from the base class private in the derived class? [duplicate]

This question already has answers here:
When overriding a method, why can I increase access but not decrease it?
(8 answers)
Closed 4 years ago.
For example, class Base has two public methods: foo() and bar(). Class Derived is inherited from class Base (I cannot modify this class, as its a in a library I use).
In class Derived (Its in my application), I want to make foo() public but bar() private. Is the following code the correct and natural way to do this? Instead of extending it, I am creating a object and accessing only the required methods.
class Base {
public void foo();
public void bar();
};
public class Derived {
private Base base;
public void bar() {
base.bar();
}
};
You cannot reduce the visibility of a method you inherit from
So if the super method is public you cannot reduce to protected or private
This question already covers it : Cannot reduce visibility of method inherited method from parent
You cannot inherit from a class and reduce the visibility of the inherited methods. On the other hand, the code you are showing is not making use of inheritance. If you need to extend a class (so that you inherit all state and behavior from the base class), but also don't want to expose all inherited methods as public, here's a way to do it:
public final class Derived {
private final Base base = new Base() { // extending base class here
#Override
public void foo() {
Derived.this.foo();
}
#Override
public void bar() {
Derived.this.bar();
}
};
public void foo() {
// Implement foo here
}
private void bar() {
// Implement bar here
}
}
So the idea is to encapsulate the implementation of a class that extends Base in a private final field of a new Derived class that only exposes the foo method, while keeping the bar method private. This anonymous inner class just delegates to methods of the Derived class. As an extra safety measure, we are also making the Derived class final.

Static methods in Interfaces in Java [duplicate]

This question already has answers here:
What is the difference between static and default methods in a Java interface?
(12 answers)
Closed 5 years ago.
interface TestInterface
{
public static void square(int a)
{
System.out.println("Square is "+a*a);
}
public static void show()
{
System.out.println("Default Method Executed");
}
}
class TestClass implements TestInterface
{
public void square(int a)
{
System.out.println(a*a);
}
public void show()
{
System.out.println("Overridden Method");
}
public static void main(String args[])
{
TestClass d=new TestClass();
d.square(4);
TestInterface.square(4);
TestInterface.show();
d.show();
}
}
I have a doubt in my code. I learnt that static methods cannot be overridden in JAVA, but it seems to be working fine here.
When i give both default and static keywords together, like this
interface TestInterface
{
default static void square(int a)
{
System.out.println("Square is "+a*a);
}
public static void show()
{
System.out.println("Default Method Executed");
}
}
An error crops up as follows:
illegal combination of modifiers: static and default
What is the reason for JAVA treating this as an error?
A static method is meant to be called without an instance of the class/interface concerned. Usually they are meant to be utility methods.
A default method is meant to be called on an instance of the interface concerned. All implementations of this interface will have this method definition, unless it is overridden.
The reason these two terms are not allowed together is simply because they contradict each other: default requires an object, static requires no object.
TestClass.show() and TestClass.square() are not static and therefore do not override the static methods in the interface. They are member methods and require an object to call them. On the other hand, the methods with the same name in the interface are static and so you can call them with the interface name or class name without an object.

why implementation is allowed with in interface [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why no static methods in Interfaces, but static fields and inner classes OK?
I want to know that why interface do not allow static block, but they allow to declare static variable.
What if i want to intialize a static variable on some logic.
edit: Earlier i did not post my query in better form but this is my query with sample code. please look into it.
interface A {
static class XYZ {
public static void methodA() {
// some implementation
System.out.println("methodA");
}
public static void methodB() {
// some more implementation
System.out.println("methodB");
}
}
void methodC();
}
public class ABC implements A {
public static void main(String[] args) {
A.XYZ.methodA();
}
#Override
public void methodC() {
// TODO Auto-generated method stub
}
}
Since purpose of interface is to provide a mechanism where users/implementors of the interface can implement the properties( methods) according to their needs.
But if i am allowed to add implementation in interface then some how that purpose of interface is defeated, please make me clear why this implementation in the interface is permitted, there must be something that is why and what is that fact, that is what i want to know
They designed interfaces to not allow implementations; a static block would constitute an implementation, so it is not allowed.

Java interfaces reduction in visibility, only NOT [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Reduce visibility when implementing interface in Java
I must be missing something obvious, but i am getting:
Cannot reduce the visibility of the inherited method
And I don't see how. This is my interface:
interface QueryBuilderPart {
StringBuilder toStringBuilder();
}
And this is my implementation:
public class Stupid implements QueryBuilderPart {
#Override
StringBuilder toStringBuilder() {
return null;
}
}
Both the class and the implementation are in the same package. Any Ideas?
By default interface's method is public, but you reduce it to default visibility, which is package level visibility.
So the following two block of code are the same:
interface QueryBuilderPart {
StringBuilder toStringBuilder();
}
interface QueryBuilderPart {
public abstract StringBuilder toStringBuilder();
}
Note that interface's method is abstract as well
So you should do as following:
public class Stupid implements QueryBuilderPart {
#Override
public StringBuilder toStringBuilder() {
return null;
}
}

Categories

Resources