Home > Mobile >  flexible input for java function
flexible input for java function

Time:12-05

I have three classes that they have a function with the same name. In another class I want to take the class as input and use that function, but I don't know how to do it. for example:

public interface Apple {

String showSpecifications(){

// some calculations
}
}
public interface Orange {

String showSpecifications(){

// some calculations
}
}
public interface Kiwi {

String showSpecifications(){

// some calculations
}
}

In my fourth class I want a function like this:

public String showObjSpec(Object fruit) {
fruit.showSpecifications();
}

The problem is I don't know how to define the showObjSpec input type to be able to accept Apple, Orange, And Kiwi objects. Currently, it is obvious that I am getting an error that says class Object does not support showSpecifications() function.

CodePudding user response:

Interfaces can extend each other as such you could add a common interface Fruit to group them together.

Example interface:

public interface Fruit {
    String showSpecifications();
}

Each fruit would then need to add extends Fruit and showObjSpeccould then just accept a Fruit object as parameter.

public interface Apple extends Fruit {

    default String showSpecifications(){

    // some calculations
    }
}
public interface Orange extends Fruit {

    default String showSpecifications(){

    // some calculations
    }
}
public interface Kiwi extends Fruit {

    default String showSpecifications(){

    // some calculations
    }
}

CodePudding user response:

Java is strictly type based; it has no functionality whatsoever to refer to the idea of 'I accept any type, as long as it has a showSpcifications method'. See below the fold for why that is.

Hence, you.. make a type!

public interface Specified {
  // NB: 'showSpecifications' is a bad name;
  // it suggests calling that method shows specs.
  // it doesn't - it returns them. hence, getSpecifications is better.
  String getSpecifications();
}

Then have each type implement that interface:

public class Kiwi {
  @Override public String getSpecifications() {
    // some calculations
  }
}

Now you can write a method that takes anything that implements this:

public void showObjSpec(Specified s) {
  System.out.println(s.getSpecifications());
}

Depending on the behaviour your new interface actually described, perhaps 'Fruit' would be a better name.


If java did do 'just accept anything with an x method in it', you could say: "Anything with a shoot(Person p) method in it", and then given:

class Camera {
  void shoot(Person p) { ... }
}

class Gun {
  void shoot(Person p) { ... }
}

very, very bad things happen. Types are namespaced (they get a package description that serves to avoid collisions) but method names do not. hence, java does not let you create type-esque constructs that let you refer to 'anything with an x method'. Some languages do (this is called 'structural typing'). Java is not one of those languages that lets you accidentally murder your friends when all you intended to do, was make a picture.

CodePudding user response:

you should implement the inheritance in your code so that all subclasses get the method name from the superclass your code should be like that :

public interface Fruit {
 String showSpecifications()
}
public class Apple implements Fruit{

 String showSpecifications(){

// some calculations
 }
}
public class Orange implements Fruit{

 String showSpecifications(){

 // some calculations
 }
}
public class Kiwi implements Fruit {

 String showSpecifications(){

 // some calculations
 }
}

then you could execute your main method :

public void showObjSpec(Fruit fruit) {
  System.out.println(fruit.showSpecifications());
}
  •  Tags:  
  • java
  • Related