---
title: "Method argument of different types"  
description: "Method argument of different types"  
author: "Lady Bird Johnson"  
published: 2013-10-14  
updated: 2013-10-14  
canonical: https://www.mindstick.com/forum/1653/method-argument-of-different-types  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Method argument of different types

I want to write method that would [accept](https://yourviews.mindstick.com/view/81278/we-have-to-accept-that-roger-federer-is-the-richest-one) the [parameter](https://www.mindstick.com/blog/450/parameter-class-in-c-sharp) of types a.A or b.B. Currently it's implemented:

\

```
import a.A;
import b.B;
public void doSth(A arg) {
      SpecificClass.specificMethod(arg);
}
public void doSth(B arg)
{
      SpecificClass.specificMethod(arg);
}
```

\

I want to have one [generic method](https://www.mindstick.com/forum/160393/how-to-define-a-generic-method-in-c-sharp) "doSth" that use wildcards and accepts only a.A or b.B. Important information a.A and b.B aren't subtypes of each other. The only [common](https://www.mindstick.com/articles/23170/10-most-common-accounting-mistakes-of-small-business) type is java.lang.Object.

Any help?

## Replies

### Reply by Kate Smith

You may wrap both A and B extending a common interface, just like:

\

```
interface CommonWrapper {
  public void doSth();
}

public class AWrapper implements CommonWrapper {
  private A wrapped;
  public AWrapper(A a) {
    this.wrapped = a;
  }

  public void doSth() {
    // implement the actual logic using a
  }
}

public class BWrapper implements CommonWrapper {
  private B wrapped;
  public BWrapper(B b) {
    this.wrapped = b;
  }

  public void doSth() {
    // implement the actual logic using b
  }
}
```

\

Then modify your [method](https://www.mindstick.com/forum/166/webservice-method) doSth to accept a CommonWrapper object as parameter:

\

```
public void doSth(CommonWrapper c) {
  c.doSth();
}
```

\

\


---

Original Source: https://www.mindstick.com/forum/1653/method-argument-of-different-types

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
