-2

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.

Tindona
  • 430
  • 1
  • 16
  • 2
    This is what interfaces are for. Declare the interface and then have each class implement the interface. It might also make sense to make the fruit classes subclasses of some common base class. [What is a Java interface?](https://stackoverflow.com/q/1321122/238704) – President James K. Polk Dec 04 '22 at 23:11
  • actually, each class is an interface but I don't know how to do this @PresidentJamesK.Polk – Tindona Dec 04 '22 at 23:13
  • That doesn't make sense, they do not implement any interfaces according to the code you posted. – President James K. Polk Dec 04 '22 at 23:13
  • @PresidentJamesK.Polk I edited the code, Apple orange and kiwi are interfaces, but I cant understand where to put the interface that you mentioned – Tindona Dec 04 '22 at 23:16
  • you note that you do calculations in the showSpecification method, however default methods in interfaces need a default modifier in front of the return type. Are you sure these are interfaces? – SilicDev Dec 04 '22 at 23:23
  • @SilicDev yes they are interface for a data table, I tried to simplify the codes – Tindona Dec 04 '22 at 23:25

3 Answers3

1

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
    }
}
SilicDev
  • 137
  • 1
  • 9
0

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.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
0

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());
}
said
  • 19
  • 5